Removing an element from Map.

Hi all,

I have a map as below. MoTopImpl is a class.

Map<MoTopImpl*,os_Reference_protected<MoTopImpl> > map_;

The map is populated as below:

1
2
3
4
void setMoInMap(MoTopImpl* mo,MoTopImpl* me)
{
	map_[mo] = me;
}


Now, I want to remove a specific element from this map. I am passing the element to be removed to the remove function as below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void Asap::removeMoFromMap(MoTopImpl* mo)
{
        // First solution
        if (mo != 0)
        {
            cout << "Before map size = " << map_.size() << endl;
            map_.remove(mo);
            cout << "After map size = " << map_.size() << endl;
        }
        // Enters the if statememt. Map size before and after remains same. Never decremented.
        // First solutions end


        // Second solution
        if (Mapiter<MoTopImpl*,os_Reference_protected<MoTopImpl> >&elm = map_.element(mo))
        {
            cout << "Before map size = " << map_.size() << endl;
            elm.remove();
            cout << "After map size = " << map_.size() << endl;
        }
        // Here the if statement is never true. It is not returning me that element from the map.
        // Second solution end
}


And the function removeMoFromMap is called as below:

1
2
3
MoTopImpl* moTop = getMoTopImpl();
if (moTop != 0)
    removeMoFromListMosSuspended(moTop);


But I am able to empty the map by iterating through the complete map as below:

1
2
3
4
5
6
7
Mapiter<MoTopImpl*,os_Reference_protected<MoTopImpl> > moIter(map_);

for (moIter = map_.first(); moIter; moIter.next())
{
      moIter.remove();
}
cout << "Map zise = " << map_.size() << endl;  // Prints zero 


Would be very grateful for any help.
What is this Map class you are using?
@Peter87
Some third party libraries. But they work similar to STL.
Just to add to the original post, if I have a below map, then it works fine.

1
2
3
4
5
6
Map<int, int> tempMap_;
int key = 4;
 if (Mapiter<int, int> &elm = tempMap_.element(key))
{
            elm.remove();
}


The above code works perfectly fine.

I suspect since the key in the first map is a pointer, I have to use differently. But, I don't know how to use it.

Please help.
I don't see how anyone can help you unless they know exactly what library it is that you are using. Stating that it is similar to the STL, doesn't help.

I find the following statement to be very strange looking. It looks like you are creating a reference to an iterator instead of an iterator. I have no idea what the element member function returns. The if statement above also uses assignment so I'm not sure how it would ever be false.
Mapiter<int, int> &elm
Here are the details of Map.h file. Will post the Map.cc details in next post immediately.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
template <class K, class V> class AVL_Node{
	friend class Map<K,V>;
	friend class Mapiter<K,V>;
	friend class ConstMapiter<K,V>;

public:
  // Persistency with Object Store
  COS_TYPESPEC_DECLARE

private:
   Map_pair<K,V> map_data; // Data
	int balance; // Balance
   AVL_Node *father;
	AVL_Node *leftChild;
	AVL_Node *rightChild;

	AVL_Node(const K&, const V&);
	~AVL_Node();
};
		
// forward declaration
template <class K, class V> class Mapiter;


template <class K, class V> class Map 
{
  friend class Mapiter<K,V>;
private:
  AVL_Node<K,V> *head;
  Mapiter<K,V> *root_mapiter;

  V def_val; // default Value

  // SC: a static function to provide default value for the key.
  static const K& default_key ();

  int sz; // current size
  void init();

  AVL_Node<K,V>* succ(AVL_Node<K,V>*); // succ for a node
  AVL_Node<K,V>* pred(AVL_Node<K,V>*); // pred for a node
  AVL_Node<K,V>* find(const K&, AVL_Node<K,V>*) const; // find a node

  void updateIterators(AVL_Node<K,V>*);

public:
  // Persistency with Object Store
  COS_TYPESPEC_DECLARE

  //  Constructors / destructor
  Map();
  Map(const V&);
  ~Map();

  // Copy and assign
  Map(const Map<K,V>&);
  Map<K,V>& operator= (const Map<K,V>&);

  // Insert and remove elements
  V& operator[] (const K&);
  const V& operator[] (const K&) const;
  int remove(const K&);
  void make_empty();

  // Length
  int size() const;

  // Iterating
  Mapiter<K,V> element (const K&) const;
  Mapiter<K,V> first() const;
  Mapiter<K,V> last() const;
};


template <class K, class V>
class Mapiter {
  friend class Map<K,V>;
  friend class ConstMapiter<K,V>;
private:
  Map<K,V>* m;
  AVL_Node<K,V>* p;
  Mapiter<K,V>* next_mapiter;

  void updateIterators(AVL_Node<K,V>*);

  Mapiter(const Map<K,V>*, AVL_Node<K,V>*);

public:

  //  Constructors / destructor
  Mapiter(const Map<K,V>&);
  ~Mapiter();

  // Copy and assign
  Mapiter(const Mapiter<K,V>&);
  Mapiter& operator=(const Mapiter<K,V>&);

  // Test for vacuity
  operator void*() const;

  // Remove elements
  void remove();
  
  // get the Key and Value
  const K& key();
  V& value();

  // Increment - Decrement
  Mapiter& operator--(); // prefix
  Mapiter& operator++(); // prefix

  Map_pair<K,V>* next();
  Map_pair<K,V>* prev();
  Map_pair<K,V>* curr();
};
Here are the details of the Map. Please let me know if you need definitions of any other methods.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// AVL_Node
//
template <class K, class V>
AVL_Node<K,V>::AVL_Node(const K& k, const V& v): map_data(k)
{
	map_data.value = v;
}


// SC: A static function to provide default key value
template <class K, class V>
const K& Map<K, V>::default_key ()
{
  static K default_key;
  return default_key;
}


template <class K, class V>
void Map<K,V>::init()
{
   head = 0;
   sz = 0;
}

template <class K, class V>
Map<K,V>::Map()
{
  // Use a static data to be sure that the value is initialized
  // But don't use default_value() because we aren't sure of its life time in
  // case of static Map
  static V initialized_val;
  def_val = initialized_val;
  init();
  root_mapiter = 0;
}

template <class K, class V>
Map<K,V>::Map(const V& v) : def_val(v)
{
  init();
  root_mapiter = 0;
}

// Size of the Map
template <class K, class V>
int Map<K,V>::size() const
{
  return sz;
}

// Empty the map
template <class K, class V>
void Map<K,V>::make_empty()
{
  COS_DELETE(head);
  init();
}

// Find a node in the Map using a Key as input
template <class K, class V>
AVL_Node<K,V>* Map<K,V>::find(const K& k, AVL_Node<K,V>* ptr) const
{
  AVL_Node<K,V>* res = ptr;
  while (res != 0)
    {
      if (k < res->map_data.key)
	{
	  res = res->leftChild;
	}
      else if (res->map_data.key < k)
	{
	  res = res->rightChild;
	}
      else
	{
	  break;
	}
    }
  return res;
}	

// Node Deletion
template <class K, class V>
int Map<K,V>::remove(const K& k) {

	// find the node to delete
	AVL_Node<K,V> *delNode = find(k,head);

	if (delNode != 0) {

		// Node found - update iterators
		updateIterators(delNode);

        	// delete it
		delete_node(delNode);

		// free the memory & return
		COS_DELETE(delNode);
		sz--;

		return 1;
	}

        // Node not found
        return 0;
}

// Get the first element of the Map
template <class K, class V>
Mapiter<K,V> Map<K,V>::first() const
{
	return ++Mapiter<K,V>(*this);
}

// Get the last element of the Map
template <class K, class V>
Mapiter<K,V> Map<K,V>::last() const
{
	return --Mapiter<K,V>(*this);
}

// Get one element of the Map using a Key as input
template <class K, class V>
Mapiter<K,V> Map<K,V>::element(const K& k) const
{
	// Find the node
	AVL_Node<K,V> *p = find(k,head);    
	return Mapiter<K,V>(this,p);
}

Topic archived. No new replies allowed.