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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
20 package org.apache.thrift.helper;
24 import haxe.ds.IntMap;
27 // Int64Map allows mapping of Int64 keys to arbitrary values.
28 // ObjectMap<> cannot be used, since we want to compare by value, not address
30 class Int64Map<T> implements IMap< Int64, T> {
32 private var SubMaps : IntMap< IntMap< T>>; // Hi -> Lo -> Value
34 public function new() : Void {
35 SubMaps = new IntMap< IntMap< T>>();
38 private function GetSubMap( hi : haxe.Int32, canCreate : Bool) : IntMap< T> {
39 if( SubMaps.exists(hi)) {
40 return SubMaps.get(hi);
47 var lomap = new IntMap< T>();
48 SubMaps.set( hi, lomap);
53 private function GetLowMap( key : haxe.Int64, canCreate : Bool) : IntMap< T> {
55 return GetSubMap( Int64.getHigh(key), canCreate);
57 return GetSubMap( key.high, canCreate);
62 private function GetLowIndex( key : haxe.Int64) : haxe.Int32 {
64 return Int64.getLow(key);
71 private function NullCheck( key : haxe.Int64) : Bool {
75 return true; // Int64 is not nullable anymore (it never really was)
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.
86 public function set( key : Int64, value : T ) : Void {
87 if( ! NullCheck(key)) {
91 var lomap = GetLowMap( key, true);
92 lomap.set( GetLowIndex(key), value);
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.
101 Note that a check like `map.get(key) == null` can hold for two reasons:
103 1. the map has no mapping for `key`
104 2. the map has a mapping with a value of `null`
106 If it is important to distinguish these cases, `exists()` should be
110 public function get( key : Int64) : Null<T> {
111 if( ! NullCheck(key)) {
115 var lomap = GetLowMap( key, true);
120 return lomap.get( GetLowIndex(key));
124 Returns true if `key` has a mapping, false otherwise.
125 If `key` is null, the result is unspecified.
127 public function exists( key : Int64) : Bool {
128 if( ! NullCheck(key)) {
132 var lomap = GetLowMap( key, true);
137 return lomap.exists( GetLowIndex(key));
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.
144 public function remove( key : Int64) : Bool {
145 if( ! NullCheck(key)) {
149 var lomap = GetLowMap( key, true);
154 return lomap.remove( GetLowIndex(key));
159 Returns an Iterator over the keys of `this` Map.
160 The order of keys is undefined.
162 public function keys() : Iterator<Int64> {
163 return new Int64KeyIterator<T>(SubMaps);
167 Returns an Iterator over the values of `this` Map.
168 The order of values is undefined.
170 public function iterator() : Iterator<T> {
171 return new Int64ValueIterator<T>(SubMaps);
175 Returns a String representation of `this` Map.
176 The exact representation depends on the platform and key-type.
178 public function toString() : String {
179 var result : String = "{";
182 for( key in this.keys()) {
190 var value = this.get(key);
191 result += Int64.toStr(key) + ' => $value';
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> {
204 private var SubMaps : IntMap< IntMap< T>>; // Hi -> Lo -> Value
206 private var HiIterator : Iterator< Int> = null;
207 private var LoIterator : Iterator< Int> = null;
208 private var CurrentHi : Int = 0;
210 public function new( data : IntMap< IntMap< T>>) : Void {
212 HiIterator = SubMaps.keys();
218 Returns false if the iteration is complete, true otherwise.
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
225 public function hasNext() : Bool {
227 if( (LoIterator != null) && LoIterator.hasNext()) {
231 while( (HiIterator != null) && HiIterator.hasNext()) {
232 CurrentHi = HiIterator.next();
233 LoIterator = SubMaps.get(CurrentHi).keys();
234 if( (LoIterator != null) && LoIterator.hasNext()) {
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> {
251 public function new( data : IntMap< IntMap< T>>) : Void {
256 Returns the current item of the Iterator and advances to the next one.
258 This method is not required to check hasNext() first. A call to this
259 method while hasNext() is false yields unspecified behavior.
261 public function next() : Int64 {
263 return Int64.make( CurrentHi, LoIterator.next());
265 throw "no more elements";
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> {
275 public function new( data : IntMap< IntMap< T>>) : Void {
280 Returns the current item of the Iterator and advances to the next one.
282 This method is not required to check hasNext() first. A call to this
283 method while hasNext() is false yields unspecified behavior.
285 public function next() : T {
287 return SubMaps.get(CurrentHi).get(LoIterator.next());
289 throw "no more elements";