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();
};
|