]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/helper/Int64Map.hx
8845fd0955903a4b22fe921d42d3e1d02eea2e39
[ceph.git] / ceph / src / jaegertracing / thrift / lib / haxe / src / org / apache / thrift / helper / Int64Map.hx
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package org.apache.thrift.helper;
21
22 import Map;
23 import haxe.Int64;
24 import haxe.ds.IntMap;
25
26
27 // Int64Map allows mapping of Int64 keys to arbitrary values.
28 // ObjectMap<> cannot be used, since we want to compare by value, not address
29
30 class Int64Map<T> implements IMap< Int64, T> {
31
32 private var SubMaps : IntMap< IntMap< T>>; // Hi -> Lo -> Value
33
34 public function new() : Void {
35 SubMaps = new IntMap< IntMap< T>>();
36 };
37
38 private function GetSubMap( hi : haxe.Int32, canCreate : Bool) : IntMap< T> {
39 if( SubMaps.exists(hi)) {
40 return SubMaps.get(hi);
41 }
42
43 if( ! canCreate) {
44 return null;
45 }
46
47 var lomap = new IntMap< T>();
48 SubMaps.set( hi, lomap);
49 return lomap;
50 }
51
52
53 private function GetLowMap( key : haxe.Int64, canCreate : Bool) : IntMap< T> {
54 #if( haxe_ver < 3.2)
55 return GetSubMap( Int64.getHigh(key), canCreate);
56 #else
57 return GetSubMap( key.high, canCreate);
58 #end
59 }
60
61
62 private function GetLowIndex( key : haxe.Int64) : haxe.Int32 {
63 #if( haxe_ver < 3.2)
64 return Int64.getLow(key);
65 #else
66 return key.low;
67 #end
68 }
69
70
71 private function NullCheck( key : haxe.Int64) : Bool {
72 #if( haxe_ver < 3.2)
73 return (key != null);
74 #else
75 return true; // Int64 is not nullable anymore (it never really was)
76 #end
77 };
78
79
80
81 /**
82 Maps `key` to `value`.
83 If `key` already has a mapping, the previous value disappears.
84 If `key` is null, the result is unspecified.
85 **/
86 public function set( key : Int64, value : T ) : Void {
87 if( ! NullCheck(key)) {
88 return;
89 }
90
91 var lomap = GetLowMap( key, true);
92 lomap.set( GetLowIndex(key), value);
93 }
94
95
96 /**
97 Returns the current mapping of `key`.
98 If no such mapping exists, null is returned.
99 If `key` is null, the result is unspecified.
100
101 Note that a check like `map.get(key) == null` can hold for two reasons:
102
103 1. the map has no mapping for `key`
104 2. the map has a mapping with a value of `null`
105
106 If it is important to distinguish these cases, `exists()` should be
107 used.
108
109 **/
110 public function get( key : Int64) : Null<T> {
111 if( ! NullCheck(key)) {
112 return null;
113 }
114
115 var lomap = GetLowMap( key, true);
116 if( lomap == null) {
117 return null;
118 }
119
120 return lomap.get( GetLowIndex(key));
121 }
122
123 /**
124 Returns true if `key` has a mapping, false otherwise.
125 If `key` is null, the result is unspecified.
126 **/
127 public function exists( key : Int64) : Bool {
128 if( ! NullCheck(key)) {
129 return false;
130 }
131
132 var lomap = GetLowMap( key, true);
133 if( lomap == null) {
134 return false;
135 }
136
137 return lomap.exists( GetLowIndex(key));
138 }
139
140 /**
141 Removes the mapping of `key` and returns true if such a mapping existed,
142 false otherwise. If `key` is null, the result is unspecified.
143 **/
144 public function remove( key : Int64) : Bool {
145 if( ! NullCheck(key)) {
146 return false;
147 }
148
149 var lomap = GetLowMap( key, true);
150 if( lomap == null) {
151 return false;
152 }
153
154 return lomap.remove( GetLowIndex(key));
155 }
156
157
158 /**
159 Returns an Iterator over the keys of `this` Map.
160 The order of keys is undefined.
161 **/
162 public function keys() : Iterator<Int64> {
163 return new Int64KeyIterator<T>(SubMaps);
164 }
165
166 /**
167 Returns an Iterator over the values of `this` Map.
168 The order of values is undefined.
169 **/
170 public function iterator() : Iterator<T> {
171 return new Int64ValueIterator<T>(SubMaps);
172 }
173
174 /**
175 Returns a String representation of `this` Map.
176 The exact representation depends on the platform and key-type.
177 **/
178 public function toString() : String {
179 var result : String = "{";
180
181 var first = true;
182 for( key in this.keys()) {
183 if( first) {
184 first = false;
185 } else {
186 result += ",";
187 }
188
189 result += " ";
190 var value = this.get(key);
191 result += Int64.toStr(key) + ' => $value';
192 }
193
194 return result + "}";
195 }
196
197 }
198
199
200 // internal helper class for Int64Map<T>
201 // all class with matching methods can be used as iterator (duck typing)
202 private class Int64MapIteratorBase<T> {
203
204 private var SubMaps : IntMap< IntMap< T>>; // Hi -> Lo -> Value
205
206 private var HiIterator : Iterator< Int> = null;
207 private var LoIterator : Iterator< Int> = null;
208 private var CurrentHi : Int = 0;
209
210 public function new( data : IntMap< IntMap< T>>) : Void {
211 SubMaps = data;
212 HiIterator = SubMaps.keys();
213 LoIterator = null;
214 CurrentHi = 0;
215 };
216
217 /**
218 Returns false if the iteration is complete, true otherwise.
219
220 Usually iteration is considered to be complete if all elements of the
221 underlying data structure were handled through calls to next(). However,
222 in custom iterators any logic may be used to determine the completion
223 state.
224 **/
225 public function hasNext() : Bool {
226
227 if( (LoIterator != null) && LoIterator.hasNext()) {
228 return true;
229 }
230
231 while( (HiIterator != null) && HiIterator.hasNext()) {
232 CurrentHi = HiIterator.next();
233 LoIterator = SubMaps.get(CurrentHi).keys();
234 if( (LoIterator != null) && LoIterator.hasNext()) {
235 return true;
236 }
237 }
238
239 HiIterator = null;
240 LoIterator = null;
241 return false;
242 }
243
244 }
245
246
247 // internal helper class for Int64Map<T>
248 // all class with matching methods can be used as iterator (duck typing)
249 private class Int64KeyIterator<T>extends Int64MapIteratorBase<T> {
250
251 public function new( data : IntMap< IntMap< T>>) : Void {
252 super(data);
253 };
254
255 /**
256 Returns the current item of the Iterator and advances to the next one.
257
258 This method is not required to check hasNext() first. A call to this
259 method while hasNext() is false yields unspecified behavior.
260 **/
261 public function next() : Int64 {
262 if( hasNext()) {
263 return Int64.make( CurrentHi, LoIterator.next());
264 } else {
265 throw "no more elements";
266 }
267 }
268 }
269
270
271 // internal helper class for Int64Map<T>
272 // all class with matching methods can be used as iterator (duck typing)
273 private class Int64ValueIterator<T> extends Int64MapIteratorBase<T> {
274
275 public function new( data : IntMap< IntMap< T>>) : Void {
276 super(data);
277 };
278
279 /**
280 Returns the current item of the Iterator and advances to the next one.
281
282 This method is not required to check hasNext() first. A call to this
283 method while hasNext() is false yields unspecified behavior.
284 **/
285 public function next() : T {
286 if( hasNext()) {
287 return SubMaps.get(CurrentHi).get(LoIterator.next());
288 } else {
289 throw "no more elements";
290 }
291 }
292 }
293
294
295 // EOF