1 template <class T, class Less>
FindAdd(const T & key)2 int SortedIndex<T, Less>::FindAdd(const T& key)
3 {
4 int i = FindLowerBound(key);
5 if(i == GetCount() || Less()(key, iv[i]))
6 iv.Insert(i, key);
7 return i;
8 }
9
10 template <class T, class Less>
FindNext(int i) const11 int SortedIndex<T, Less>::FindNext(int i) const
12 {
13 return i + 1 < iv.GetCount() && !Less()(iv[i], iv[i + 1]) ? i + 1 : -1;
14 }
15
16 template <class T, class Less>
FindLast(const T & x) const17 int SortedIndex<T, Less>::FindLast(const T& x) const
18 {
19 int i = iv.FindUpperBound(x, Less());
20 return i > 0 && !Less()(iv[i - 1], x) ? i - 1 : -1;
21 }
22
23 template <class T, class Less>
FindPrev(int i) const24 int SortedIndex<T, Less>::FindPrev(int i) const
25 {
26 return i > 0 && !Less()(iv[i - 1], iv[i]) ? i - 1 : -1;
27 }
28
29 template <class T, class Less>
RemoveKey(const T & x)30 int SortedIndex<T, Less>::RemoveKey(const T& x)
31 {
32 int l = FindLowerBound(x);
33 int count = FindUpperBound(x) - l;
34 Remove(l, count);
35 return count;
36 }
37
38 template <class T, class Less>
ToString() const39 String SortedIndex<T, Less>::ToString() const
40 {
41 return AsStringArray(*this);
42 }
43
44 template <class T>
Insert(int blki,int pos)45 void Slaved_InVector__<T>::Insert(int blki, int pos)
46 {
47 data.data[blki].Insert(pos);
48 res = &data.data[blki][pos];
49 }
50
51 template <class T>
Split(int blki,int nextsize)52 void Slaved_InVector__<T>::Split(int blki, int nextsize)
53 {
54 Vector<T>& x = data.data.Insert(blki + 1);
55 x.InsertSplit(0, data.data[blki], nextsize);
56 data.data[blki].Shrink();
57 }
58
59 template <class T>
AddFirst()60 void Slaved_InVector__<T>::AddFirst()
61 {
62 data.data.Add().Add();
63 res = &data.data[0][0];
64 }
65
66 template <class K, class T, class Less, class Data>
ToString() const67 String SortedAMap<K, T, Less, Data>::ToString() const
68 {
69 String r;
70 r = "{";
71 for(int i = 0; i < GetCount(); i++) {
72 if(i)
73 r << ", ";
74 r << GetKey(i) << ": " << (*this)[i];
75 }
76 r << '}';
77 return r;
78 }
79
80 template <class K, class T, class Less>
SortedVectorMap(SortedVectorMap && s)81 SortedVectorMap<K, T, Less>::SortedVectorMap(SortedVectorMap&& s)
82 {
83 B::key = pick(s.key);
84 B::value.data = pick(s.value.data);
85 B::SetSlave();
86 }
87
88 template <class K, class T, class Less>
operator =(SortedVectorMap && s)89 SortedVectorMap<K, T, Less>& SortedVectorMap<K, T, Less>::operator=(SortedVectorMap&& s)
90 {
91 B::key = pick(s.key);
92 B::value.data = pick(s.value.data);
93 B::SetSlave();
94 return *this;
95 }
96
97 template <class K, class T, class Less>
SortedVectorMap(const SortedVectorMap & s,int)98 SortedVectorMap<K, T, Less>::SortedVectorMap(const SortedVectorMap& s, int)
99 {
100 B::key = clone(s.key);
101 B::value.data = clone(s.value.data);
102 B::SetSlave();
103 }
104
105 template <class K, class T, class Less>
FindAdd(const K & k,const T & init)106 int SortedVectorMap<K, T, Less>::FindAdd(const K& k, const T& init)
107 {
108 B::value.res = NULL;
109 int q = B::key.FindAdd(k);
110 if(B::value.res)
111 *B::value.res = init;
112 return q;
113 }
114
115 template <class K, class T, class Less>
Swap(SortedVectorMap & x)116 void SortedVectorMap<K, T, Less>::Swap(SortedVectorMap& x)
117 {
118 B::value.data.Swap(x.value.data);
119 B::key.Swap(x.B::key);
120 B::SetSlave();
121 x.SetSlave();
122 }
123
124 template <class T>
Insert(int blki,int pos)125 void Slaved_InArray__<T>::Insert(int blki, int pos)
126 {
127 if(!res)
128 res = new T;
129 data.iv.data[blki].Insert(pos, res);
130 }
131
132 template <class T>
Split(int blki,int nextsize)133 void Slaved_InArray__<T>::Split(int blki, int nextsize)
134 {
135 Vector< typename InArray<T>::PointerType >& x = data.iv.data.Insert(blki + 1);
136 x.InsertSplit(0, data.iv.data[blki], nextsize);
137 }
138
139 template <class T>
Remove(int blki,int pos,int n)140 void Slaved_InArray__<T>::Remove(int blki, int pos, int n)
141 {
142 Vector< typename InArray<T>::PointerType >& b = data.iv.data[blki];
143 for(int i = 0; i < n; i++)
144 if(b[i + pos])
145 delete (T *)b[i + pos];
146 b.Remove(pos, n);
147 }
148
149 template <class T>
AddFirst()150 void Slaved_InArray__<T>::AddFirst()
151 {
152 if(!res)
153 res = new T;
154 data.iv.data.Add().Add(res);
155 }
156
157 template <class K, class T, class Less>
FindAdd(const K & k,const T & init)158 int SortedArrayMap<K, T, Less>::FindAdd(const K& k, const T& init)
159 {
160 B::value.res = NULL;
161 int q = B::key.FindAdd(k);
162 if(B::value.res)
163 *B::value.res = init;
164 return q;
165 }
166
167 template <class K, class T, class Less>
SortedArrayMap(SortedArrayMap && s)168 SortedArrayMap<K, T, Less>::SortedArrayMap(SortedArrayMap&& s)
169 {
170 B::key = pick(s.key);
171 B::value.data = pick(s.value.data);
172 B::SetSlave();
173 }
174
175 template <class K, class T, class Less>
operator =(SortedArrayMap && s)176 SortedArrayMap<K, T, Less>& SortedArrayMap<K, T, Less>::operator=(SortedArrayMap&& s)
177 {
178 B::key = pick(s.key);
179 B::value.data = pick(s.value.data);
180 B::SetSlave();
181 return *this;
182 }
183
184 template <class K, class T, class Less>
SortedArrayMap(const SortedArrayMap & s,int)185 SortedArrayMap<K, T, Less>::SortedArrayMap(const SortedArrayMap& s, int)
186 {
187 B::key = clone(s.key);
188 B::value.data = clone(s.value.data);
189 B::SetSlave();
190 }
191
192 template <class K, class T, class Less>
Swap(SortedArrayMap & x)193 void SortedArrayMap<K, T, Less>::Swap(SortedArrayMap& x)
194 {
195 B::value.data.Swap(x.value.data);
196 B::key.Swap(x.B::key);
197 B::SetSlave();
198 x.SetSlave();
199 }
200
201 #ifdef UPP
202 template <class K, class T>
StreamSortedMap(Stream & s,T & cont)203 void StreamSortedMap(Stream& s, T& cont)
204 {
205 int n = cont.GetCount();
206 s / n;
207 if(n < 0) {
208 s.LoadError();
209 return;
210 }
211 if(s.IsLoading()) {
212 cont.Clear();
213 while(n--) {
214 K key;
215 s % key;
216 s % cont.Add(key);
217 }
218 }
219 else
220 for(int i = 0; i < cont.GetCount(); i++) {
221 K key = cont.GetKey(i);
222 s % key;
223 s % cont[i];
224 }
225 }
226
227 template <class K, class T, class Less>
Serialize(Stream & s)228 void SortedVectorMap<K, T, Less>::Serialize(Stream& s) {
229 StreamSortedMap<K, SortedVectorMap<K, T, Less> >(s, *this);
230 }
231
232 template <class K, class T, class Less>
Xmlize(XmlIO & xio)233 void SortedVectorMap<K, T, Less>::Xmlize(XmlIO& xio)
234 {
235 XmlizeSortedMap<K, T, SortedVectorMap<K, T, Less> >(xio, "key", "value", *this);
236 }
237
238 template <class K, class T, class Less>
Jsonize(JsonIO & jio)239 void SortedVectorMap<K, T, Less>::Jsonize(JsonIO& jio)
240 {
241 JsonizeSortedMap<SortedVectorMap<K, T, Less>, K, T>(jio, *this, "key", "value");
242 }
243
244 template <class K, class T, class Less>
Serialize(Stream & s)245 void SortedArrayMap<K, T, Less>::Serialize(Stream& s) {
246 StreamSortedMap<K, SortedArrayMap<K, T, Less> >(s, *this);
247 }
248
249 template <class K, class T, class Less>
Xmlize(XmlIO & xio)250 void SortedArrayMap<K, T, Less>::Xmlize(XmlIO& xio)
251 {
252 XmlizeSortedMap<K, T, SortedArrayMap<K, T, Less> >(xio, "key", "value", *this);
253 }
254
255 template <class K, class T, class Less>
Jsonize(JsonIO & jio)256 void SortedArrayMap<K, T, Less>::Jsonize(JsonIO& jio)
257 {
258 JsonizeSortedMap<SortedArrayMap<K, T, Less>, K, T>(jio, *this, "key", "value");
259 }
260
261 #endif
262