1 // --------------------
2
3 template <class K, class T, class V>
4 template <class KK>
FindAdd_(KK && k)5 int AMap<K, T, V>::FindAdd_(KK&& k) {
6 return key.FindAdd(std::forward<KK>(k), [&] { value.Add(); });
7 }
8
9 template <class K, class T, class V>
10 template <class KK, class TT>
FindAdd_(KK && k,TT && x)11 int AMap<K, T, V>::FindAdd_(KK&& k, TT&& x) {
12 return key.FindAdd(std::forward<KK>(k), [&] { value.Add(std::forward<TT>(x)); });
13 }
14
15
16 template <class K, class T, class V>
17 template <class KK>
Put_(KK && k)18 T& AMap<K, T, V>::Put_(KK&& k)
19 {
20 int i = key.Put(std::forward<KK>(k));
21 if(i < value.GetCount()) {
22 Destroy(&value[i], &value[i] + 1);
23 Construct(&value[i], &value[i] + 1);
24 return value[i];
25 }
26 else {
27 ASSERT(i == value.GetCount());
28 return value.Add();
29 }
30 }
31
32 template <class K, class T, class V>
33 template <class KK, class TT>
Put_(KK && k,TT && x)34 int AMap<K, T, V>::Put_(KK&& k, TT&& x)
35 {
36 int i = key.Put(std::forward<KK>(k));
37 if(i < value.GetCount())
38 value[i] = std::forward<TT>(x);
39 else {
40 ASSERT(i == value.GetCount());
41 value.Add(std::forward<TT>(x));
42 }
43 return i;
44 }
45
46 template <class K, class T, class V>
47 template <class KK>
PutDefault_(KK && k)48 int AMap<K, T, V>::PutDefault_(KK&& k)
49 {
50 int i = key.Put(std::forward<KK>(k));
51 if(i >= value.GetCount()) {
52 ASSERT(i == value.GetCount());
53 value.Add();
54 }
55 else {
56 Destroy(&value[i], &value[i] + 1);
57 Construct(&value[i], &value[i] + 1);
58 }
59 return i;
60 }
61
62 template <class K, class T, class V>
63 template <class KK>
FindPut_(KK && k)64 int AMap<K, T, V>::FindPut_(KK&& k)
65 {
66 int i = key.FindPut(std::forward<KK>(k));
67 if(i >= value.GetCount())
68 value.Add();
69 return i;
70 }
71
72 template <class K, class T, class V>
73 template <class KK, class TT>
FindPut_(KK && k,TT && init)74 int AMap<K, T, V>::FindPut_(KK&& k, TT&& init)
75 {
76 int i = key.FindPut(std::forward<KK>(k));
77 if(i >= value.GetCount())
78 value.Add(std::forward<TT>(init));
79 return i;
80 }
81
82 template <class K, class T, class V>
83 template <class KK>
GetAdd_(KK && k)84 T& AMap<K, T, V>::GetAdd_(KK&& k)
85 {
86 return value[FindAdd(std::forward<KK>(k))];
87 }
88
89 template <class K, class T, class V>
90 template <class KK, class TT>
GetAdd_(KK && k,TT && x)91 T& AMap<K, T, V>::GetAdd_(KK&& k, TT&& x)
92 {
93 return value[FindAdd(std::forward<KK>(k), std::forward<TT>(x))];
94 }
95
96 template <class K, class T, class V>
97 template <class KK>
GetPut_(KK && k)98 T& AMap<K, T, V>::GetPut_(KK&& k)
99 {
100 int i = key.FindPut(std::forward<KK>(k));
101 return i < value.GetCount() ? value[i] : value.Add();
102 }
103
104 template <class K, class T, class V>
105 template <class KK, class TT>
GetPut_(KK && k,TT && x)106 T& AMap<K, T, V>::GetPut_(KK&& k, TT&& x)
107 {
108 int i = key.FindPut(std::forward<KK>(k));
109 return i < value.GetCount() ? value[i] : value.Add(std::forward<TT>(x));
110 }
111
112 template <class K, class T, class V>
Serialize(Stream & s)113 void AMap<K, T, V>::Serialize(Stream& s) {
114 int version = 0;
115 s / version % key % value;
116 }
117
118 template <class K, class T, class V>
Xmlize(XmlIO & xio)119 void AMap<K, T, V>::Xmlize(XmlIO& xio)
120 {
121 XmlizeMap<K, T, AMap<K, T, V> >(xio, "key", "value", *this);
122 }
123
124 template <class K, class T, class V>
Jsonize(JsonIO & jio)125 void AMap<K, T, V>::Jsonize(JsonIO& jio)
126 {
127 JsonizeMap<AMap<K, T, V>, K, T>(jio, *this, "key", "value");
128 }
129
130 template <class K, class T, class V>
ToString() const131 String AMap<K, T, V>::ToString() const
132 {
133 String r;
134 r = "{";
135 for(int i = 0; i < GetCount(); i++) {
136 if(i)
137 r << ", ";
138 if(IsUnlinked(i))
139 r << "UNLINKED ";
140 r << GetKey(i) << ": " << (*this)[i];
141 }
142 r << '}';
143 return r;
144 }
145
146 template <class K, class T, class V>
RemoveKey(const K & k)147 int AMap<K, T, V>::RemoveKey(const K& k)
148 {
149 Vector<int> rk;
150 int q = Find(k);
151 while(q >= 0) {
152 rk.Add(q);
153 q = FindNext(q);
154 }
155 Remove(rk);
156 return rk.GetCount();
157 }
158
159 template <class K, class T, class V>
Sweep()160 void AMap<K, T, V>::Sweep()
161 {
162 value.RemoveIf([&](int i) { return key.IsUnlinked(i); });
163 key.Sweep();
164 }
165
166 #ifdef UPP
167 template <class K, class T, class V, class Less>
Serialize(Stream & s)168 void FixedAMap<K, T, V, Less>::Serialize(Stream& s) {
169 s % key % value;
170 }
171
172 template <class K, class T, class V, class Less>
Xmlize(XmlIO & xio)173 void FixedAMap<K, T, V, Less>::Xmlize(XmlIO& xio)
174 {
175 XmlizeSortedMap<K, T, FixedAMap<K, T, V, Less> >(xio, "key", "value", *this);
176 }
177
178 template <class K, class T, class V, class Less>
Jsonize(JsonIO & jio)179 void FixedAMap<K, T, V, Less>::Jsonize(JsonIO& jio)
180 {
181 JsonizeSortedMap<FixedAMap<K, T, V, Less>, K, T>(jio, *this, "key", "value");
182 }
183
184 template <class K, class T, class V, class Less>
ToString() const185 String FixedAMap<K, T, V, Less>::ToString() const
186 {
187 String r;
188 r = "{";
189 for(int i = 0; i < GetCount(); i++) {
190 if(i)
191 r << ", ";
192 r << GetKey(i) << ": " << (*this)[i];
193 }
194 r << '}';
195 return r;
196 }
197
198 #endif
199