IntMap.hx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package haxe.ds;
  23. @:headerClassCode("
  24. inline void set(int key, ::null value) { __int_hash_set(HX_MAP_THIS,key,value); }
  25. inline void set(int key, bool value) { __int_hash_set(HX_MAP_THIS,key,value); }
  26. inline void set(int key, char value) { __int_hash_set_int(HX_MAP_THIS,key,value); }
  27. inline void set(int key, unsigned char value) { __int_hash_set_int(HX_MAP_THIS,key,value); }
  28. inline void set(int key, signed char value) { __int_hash_set_int(HX_MAP_THIS,key,value); }
  29. inline void set(int key, short value) { __int_hash_set_int(HX_MAP_THIS,key,value); }
  30. inline void set(int key, unsigned short value) { __int_hash_set_int(HX_MAP_THIS,key,value); }
  31. inline void set(int key, int value) { __int_hash_set_int(HX_MAP_THIS,key,value); }
  32. inline void set(int key, unsigned int value) { __int_hash_set_int(HX_MAP_THIS,key,value); }
  33. inline void set(int key, float value) { __int_hash_set_float(HX_MAP_THIS,key,value); }
  34. inline void set(int key, double value) { __int_hash_set_float(HX_MAP_THIS,key,value); }
  35. inline void set(int key, ::String value) { __int_hash_set_string(HX_MAP_THIS,key,value); }
  36. inline void set(int key, cpp::Int64 value) { __int_hash_set_int64(HX_MAP_THIS,key,value); }
  37. template<typename V, typename H>
  38. inline void set(int key, const ::cpp::Struct<V,H> &value) {__int_hash_set(HX_MAP_THIS,key,value); }
  39. template<typename F>
  40. inline void set(int key, const ::cpp::Function<F> &value) {__int_hash_set(HX_MAP_THIS,key,value); }
  41. template<typename V>
  42. inline void set(int key, const ::cpp::Pointer<V> &value) {__int_hash_set(HX_MAP_THIS,key,(Dynamic)value ); }
  43. template<typename VALUE>
  44. inline void set(Dynamic &key, const VALUE &value) { set( (int)key, value ); }
  45. inline bool get_bool(int key) { return __int_hash_get_bool(h,key); }
  46. inline int get_int(int key) { return __int_hash_get_int(h,key); }
  47. inline Float get_float(int key) { return __int_hash_get_float(h,key); }
  48. inline String get_string(int key) { return __int_hash_get_string(h,key); }
  49. inline cpp::Int64 get_int64(int key) { return __int_hash_get_int64(h,key); }
  50. ")
  51. @:coreApi class IntMap<T> implements haxe.Constraints.IMap<Int, T> {
  52. @:ifFeature("haxe.ds.IntMap.*")
  53. private var h:Dynamic;
  54. public function new():Void {}
  55. public function set(key:Int, value:T):Void {
  56. untyped __global__.__int_hash_set(__cpp__("HX_MAP_THIS"), key, value);
  57. }
  58. public function get(key:Int):Null<T> {
  59. return untyped __global__.__int_hash_get(h, key);
  60. }
  61. public function exists(key:Int):Bool {
  62. return untyped __global__.__int_hash_exists(h, key);
  63. }
  64. public function remove(key:Int):Bool {
  65. return untyped __global__.__int_hash_remove(h, key);
  66. }
  67. public function keys():Iterator<Int> {
  68. var a:Array<Int> = untyped __global__.__int_hash_keys(h);
  69. return a.iterator();
  70. }
  71. public function iterator():Iterator<T> {
  72. var a:Array<Dynamic> = untyped __global__.__int_hash_values(h);
  73. return a.iterator();
  74. }
  75. @:runtime public inline function keyValueIterator():KeyValueIterator<Int, T> {
  76. return new haxe.iterators.MapKeyValueIterator(this);
  77. }
  78. public function copy():IntMap<T> {
  79. var copied = new IntMap();
  80. for (key in keys())
  81. copied.set(key, get(key));
  82. return copied;
  83. }
  84. public function toString():String {
  85. return untyped __global__.__int_hash_to_string(h);
  86. }
  87. public function clear():Void {
  88. #if (hxcpp_api_level >= 400)
  89. return untyped __global__.__int_hash_clear(h);
  90. #else
  91. h = null;
  92. #end
  93. }
  94. #if (scriptable)
  95. private function setString(key:Int, val:String):Void {
  96. untyped __int_hash_set_string(__cpp__("HX_MAP_THIS"), key, val);
  97. }
  98. private function setInt(key:Int, val:Int):Void {
  99. untyped __int_hash_set_int(__cpp__("HX_MAP_THIS"), key, val);
  100. }
  101. private function setBool(key:Int, val:Bool):Void {
  102. untyped __int_hash_set_int(__cpp__("HX_MAP_THIS"), key, val);
  103. }
  104. private function setFloat(key:Int, val:Float):Void {
  105. untyped __int_hash_set_float(__cpp__("HX_MAP_THIS"), key, val);
  106. }
  107. private function setInt64(key:Int, val:haxe.Int64):Void {
  108. untyped __int_hash_set_int64(__cpp__("HX_MAP_THIS"), key, val);
  109. }
  110. private function getString(key:Int):String {
  111. return untyped __int_hash_get_string(h, key);
  112. }
  113. private function getInt(key:Int):Int {
  114. return untyped __int_hash_get_int(h, key);
  115. }
  116. private function getBool(key:Int):Bool {
  117. return untyped __int_hash_get_bool(h, key);
  118. }
  119. private function getFloat(key:Int):Float {
  120. return untyped __int_hash_get_float(h, key);
  121. }
  122. private function getInt64(key:Int):haxe.Int64 {
  123. return untyped __int_hash_get_int64(h, key);
  124. }
  125. #end
  126. }