1 | ////////////////////////////////////////////////////////////////////////////////
|
---|
2 | // NTL Nonstandard Template Library for C++
|
---|
3 | // Copyright (c) 2003 by Miroslav Fidler, Tomas Rylek
|
---|
4 | //
|
---|
5 | // Created by Miroslav Fidler, Tomas Rylek, cxl@volny.cz
|
---|
6 | //
|
---|
7 | // Permission to use, copy, modify, distribute and sell this software for any
|
---|
8 | // purpose is hereby granted without fee, provided that the above copyright
|
---|
9 | // notice appear in all copies and that both that copyright notice and this
|
---|
10 | // permission notice appear in supporting documentation.
|
---|
11 | // The author makes no representations about the suitability of this software
|
---|
12 | // for any purpose. It is provided "as is"
|
---|
13 | // without express or implied warranty.
|
---|
14 | ////////////////////////////////////////////////////////////////////////////////
|
---|
15 |
|
---|
16 | template <class K, class T, class V, class HashFn>
|
---|
17 | class AMap {
|
---|
18 | protected:
|
---|
19 | Index<K, HashFn> key;
|
---|
20 | V value;
|
---|
21 |
|
---|
22 | public:
|
---|
23 | void Add(const K& k, const T& x) { key.Add(k); value.Add(x); }
|
---|
24 | void AddPick(const K& k, pick_ T& x) { key.Add(k); value.AddPick(x); }
|
---|
25 | T& Add(const K& k) { key.Add(k); return value.Add(); }
|
---|
26 |
|
---|
27 | int Find(const K& k, unsigned h) const { return key.Find(k, h); }
|
---|
28 | int Find(const K& k) const { return key.Find(k); }
|
---|
29 | int FindNext(int i) const { return key.FindNext(i); }
|
---|
30 | int FindLast(const K& k, unsigned h) const { return key.FindLast(k, h); }
|
---|
31 | int FindLast(const K& k) const { return key.FindLast(k); }
|
---|
32 | int FindPrev(int i) const { return key.FindPrev(i); }
|
---|
33 |
|
---|
34 | int FindAdd(const K& k);
|
---|
35 | int FindAdd(const K& k, const T& init);
|
---|
36 | int FindAddPick(const K& k, pick_ T& init);
|
---|
37 |
|
---|
38 | void Put(const K& k, const T& x);
|
---|
39 | void PutPick(const K& k, pick_ T& x);
|
---|
40 | T& Put(const K& k);
|
---|
41 |
|
---|
42 | int FindPut(const K& k);
|
---|
43 | int FindPut(const K& k, const T& init);
|
---|
44 | int FindPutPick(const K& k, pick_ T& init);
|
---|
45 |
|
---|
46 | T& Get(const K& k) { return value[Find(k)]; }
|
---|
47 | const T& Get(const K& k) const { return value[Find(k)]; }
|
---|
48 | const T& Get(const K& k, const T& d) const { int i = Find(k); return i >= 0 ? value[i] : d; }
|
---|
49 |
|
---|
50 | T& GetAdd(const K& k);
|
---|
51 |
|
---|
52 | T& GetAdd(const K& k, const T& x);
|
---|
53 | T& GetAddPick(const K& k, pick_ T& x);
|
---|
54 |
|
---|
55 | T& GetPut(const K& k);
|
---|
56 |
|
---|
57 | T& GetPut(const K& k, const T& x);
|
---|
58 | T& GetPutPick(const K& k, pick_ T& x);
|
---|
59 |
|
---|
60 | void SetKey(int i, const K& k) { key.Set(i, k); }
|
---|
61 |
|
---|
62 | T *FindPtr(const K& k) { int i = Find(k); return i >= 0 ? &value[i] : NULL; }
|
---|
63 | const T *FindPtr(const K& k) const { int i = Find(k); return i >= 0 ? &value[i] : NULL; }
|
---|
64 |
|
---|
65 | T& operator()(const K& k) { return Get(k); }
|
---|
66 | const T& operator()(const K& k) const { return Get(k); }
|
---|
67 | const T& operator()(const K& k, const T& d) const { return Get(k, d); }
|
---|
68 |
|
---|
69 | void Unlink(int i) { key.Unlink(i); }
|
---|
70 | int UnlinkKey(const K& k, unsigned h) { return key.UnlinkKey(k, h); }
|
---|
71 | int UnlinkKey(const K& k) { return key.UnlinkKey(k); }
|
---|
72 | bool IsUnlinked(int i) const { return key.IsUnlinked(i); }
|
---|
73 |
|
---|
74 | T& Insert(int i, const K& k) { key.Insert(i, k); return value.Insert(i); }
|
---|
75 | void Insert(int i, const K& k, const T& x) { key.Insert(i, k); value.Insert(i, x); }
|
---|
76 | void Remove(int i) { key.Remove(i); value.Remove(i); }
|
---|
77 | void Remove(const int *sl, int n) { key.Remove(sl, n); value.Remove(sl, n); }
|
---|
78 | void Remove(const Vector<int>& sl) { Remove(sl, sl.GetCount()); }
|
---|
79 | int RemoveKey(const K& k);
|
---|
80 |
|
---|
81 | const T& operator[](int i) const { return value[i]; }
|
---|
82 | T& operator[](int i) { return value[i]; }
|
---|
83 | int GetCount() const { return value.GetCount(); }
|
---|
84 | bool IsEmpty() const { return value.IsEmpty(); }
|
---|
85 | void Clear() { key.Clear(); value.Clear(); }
|
---|
86 | void Shrink() { value.Shrink(); key.Shrink(); }
|
---|
87 | void Reserve(int xtra) { value.Reserve(xtra); key.Reserve(xtra); }
|
---|
88 | int GetAlloc() const { return value.GetAlloc(); }
|
---|
89 |
|
---|
90 | void Drop(int n = 1) { key.Drop(n); value.Drop(n); }
|
---|
91 | T& Top() { return value.Top(); }
|
---|
92 | const T& Top() const { return value.Top(); }
|
---|
93 | const K& TopKey() const { return key.Top(); }
|
---|
94 | // T Pop() { T h = Top(); Drop(); return h; }
|
---|
95 | K PopKey() { K h = TopKey(); Drop(); return h; }
|
---|
96 |
|
---|
97 | const K& GetKey(int i) const { return key[i]; }
|
---|
98 |
|
---|
99 | #ifdef UPP
|
---|
100 | void Serialize(Stream& s);
|
---|
101 | #endif
|
---|
102 |
|
---|
103 | void Swap(AMap& x) { ::Swap(value, x.value);
|
---|
104 | ::Swap(key, x.key); }
|
---|
105 | const Index<K>& GetIndex() const { return key; }
|
---|
106 | Index<K> PickIndex() { return key; }
|
---|
107 |
|
---|
108 | const Vector<K>& GetKeys() const { return key.GetKeys(); }
|
---|
109 | Vector<K> PickKeys() { return key.PickKeys(); }
|
---|
110 |
|
---|
111 | const V& GetValues() const { return value; }
|
---|
112 | V PickValues() { return value; }
|
---|
113 |
|
---|
114 | AMap() {}
|
---|
115 | AMap(const AMap& s, int) : value(s.value, 0), key(s.key, 0) {}
|
---|
116 | AMap(pick_ Index<K>& ndx, pick_ V& val) : key(ndx), value(val) {}
|
---|
117 | AMap(pick_ Vector<K>& ndx, pick_ V& val) : key(ndx), value(val) {}
|
---|
118 |
|
---|
119 | typedef K KeyType;
|
---|
120 | typedef typename Index<K>::ConstIterator KeyConstIterator;
|
---|
121 |
|
---|
122 | KeyConstIterator KeyBegin() const { return key.Begin(); }
|
---|
123 | KeyConstIterator KeyEnd() const { return key.End(); }
|
---|
124 | KeyConstIterator KeyGetIter(int pos) const { return key.GetIter(pos); }
|
---|
125 |
|
---|
126 | typedef T ValueType;
|
---|
127 | typedef typename V::ConstIterator ConstIterator;
|
---|
128 | typedef typename V::Iterator Iterator;
|
---|
129 |
|
---|
130 | Iterator Begin() { return value.Begin(); }
|
---|
131 | Iterator End() { return value.End(); }
|
---|
132 | Iterator GetIter(int pos) { return value.GetIter(pos); }
|
---|
133 | ConstIterator Begin() const { return value.Begin(); }
|
---|
134 | ConstIterator End() const { return value.End(); }
|
---|
135 | ConstIterator GetIter(int pos) const { return value.GetIter(pos); }
|
---|
136 |
|
---|
137 | friend int GetCount(const AMap& v) { return v.GetCount(); }
|
---|
138 | };
|
---|
139 |
|
---|
140 | template <class K, class T, class HashFn = StdHash<K> >
|
---|
141 | class VectorMap : public AMap< K, T, Vector<T>, HashFn >,
|
---|
142 | Moveable<VectorMap<K, T, HashFn> >,
|
---|
143 | DeepCopyOption<VectorMap<K, T, HashFn> > {
|
---|
144 | typedef AMap< K, T, Vector<T>, HashFn > B;
|
---|
145 | public:
|
---|
146 | T Pop() { T h = B::Top(); B::Drop(); return h; }
|
---|
147 |
|
---|
148 | VectorMap(const VectorMap& s, int) : AMap<K, T, Vector<T>, HashFn>(s, 1) {}
|
---|
149 | VectorMap(pick_ Index<K>& ndx, pick_ Vector<T>& val) : AMap<K, T, Vector<T>, HashFn>(ndx, val) {}
|
---|
150 | VectorMap(pick_ Vector<K>& ndx, pick_ Vector<T>& val) : AMap<K, T, Vector<T>, HashFn>(ndx, val) {}
|
---|
151 | VectorMap() {}
|
---|
152 |
|
---|
153 | friend void Swap(VectorMap& a, VectorMap& b) { a.B::Swap(b); }
|
---|
154 |
|
---|
155 | typedef typename AMap< K, T, Vector<T>, HashFn >::ConstIterator ConstIterator; // GCC bug (?)
|
---|
156 | typedef typename AMap< K, T, Vector<T>, HashFn >::Iterator Iterator; // GCC bug (?)
|
---|
157 | STL_MAP_COMPATIBILITY(VectorMap<K _cm_ T _cm_ HashFn>)
|
---|
158 | };
|
---|
159 |
|
---|
160 | template <class K, class T, class HashFn = StdHash<K> >
|
---|
161 | class ArrayMap : public AMap< K, T, Array<T>, HashFn >,
|
---|
162 | Moveable< ArrayMap<K, T, HashFn> >,
|
---|
163 | DeepCopyOption< ArrayMap<K, T, HashFn> > {
|
---|
164 | typedef AMap< K, T, Array<T>, HashFn > B;
|
---|
165 | public:
|
---|
166 | void Add(const K& k, const T& x) { B::Add(k, x); }
|
---|
167 | T& Add(const K& k) { return B::Add(k); }
|
---|
168 | void Add(const K& k, T *ptr) { B::key.Add(k); B::value.Add(ptr); }
|
---|
169 |
|
---|
170 | void Set(int i, T *ptr) { B::value.Set(i, ptr); }
|
---|
171 | T *PopDetach() { B::key.Drop(); return B::value.PopDetach(); }
|
---|
172 |
|
---|
173 | ArrayMap(const ArrayMap& s, int) : AMap<K, T, Array<T>, HashFn>(s, 1) {}
|
---|
174 | ArrayMap(pick_ Index<K>& ndx, pick_ Array<T>& val) : AMap<K, T, Array<T>, HashFn>(ndx, val) {}
|
---|
175 | ArrayMap(pick_ Vector<K>& ndx, pick_ Array<T>& val) : AMap<K, T, Array<T>, HashFn>(ndx, val) {}
|
---|
176 | ArrayMap() {}
|
---|
177 |
|
---|
178 | friend void Swap(ArrayMap& a, ArrayMap& b) { a.B::Swap(b); }
|
---|
179 |
|
---|
180 | typedef typename AMap< K, T, Array<T>, HashFn >::ConstIterator ConstIterator; // GCC bug (?)
|
---|
181 | typedef typename AMap< K, T, Array<T>, HashFn >::Iterator Iterator; // GCC bug (?)
|
---|
182 | STL_MAP_COMPATIBILITY(ArrayMap<K _cm_ T _cm_ HashFn>)
|
---|
183 | };
|
---|
184 |
|
---|
185 | template <class K, class T, int NBLK = 16, class HashFn = StdHash<K> >
|
---|
186 | class SegtorMap : public AMap< K, T, Segtor<T, NBLK>, HashFn >,
|
---|
187 | Moveable< SegtorMap<K, T, NBLK, HashFn> >,
|
---|
188 | DeepCopyOption< SegtorMap<K, T, NBLK, HashFn > > {
|
---|
189 | typedef AMap< K, T, Segtor<T, NBLK>, HashFn > B;
|
---|
190 | public:
|
---|
191 | SegtorMap(const SegtorMap& s, int) : AMap<K, T, Segtor<T, NBLK>, HashFn>(s, 1) {}
|
---|
192 | SegtorMap(pick_ Index<K>& ndx, pick_ Segtor<T>& val) : AMap<K, T, Segtor<T, NBLK>, HashFn>(ndx, val) {}
|
---|
193 | SegtorMap(pick_ Vector<K>& ndx, pick_ Segtor<T>& val) : AMap<K, T, Segtor<T, NBLK>, HashFn>(ndx, val) {}
|
---|
194 | SegtorMap() {}
|
---|
195 |
|
---|
196 | friend void Swap(SegtorMap& a, SegtorMap& b) { a.B::Swap(b); }
|
---|
197 |
|
---|
198 | typedef typename B::ConstIterator ConstIterator; // GCC bug (?)
|
---|
199 | typedef typename B::Iterator Iterator; // GCC bug (?)
|
---|
200 | STL_MAP_COMPATIBILITY(SegtorMap<K _cm_ T _cm_ NBLK _cm_ HashFn>)
|
---|
201 | };
|
---|