Dict.hx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 python;
  23. import python.internal.UBuiltins;
  24. import python.Tuple;
  25. import python.NativeIterator;
  26. import python.Syntax;
  27. @:native("dict")
  28. extern class Dict<K, V> {
  29. function new():Void;
  30. var length(get, never):Int;
  31. private inline function get_length():Int {
  32. return UBuiltins.len(this);
  33. }
  34. function get(key:K, ?def:V):V;
  35. inline function getSafe(key:K):V {
  36. return Syntax.arrayAccess(this, key);
  37. }
  38. inline function set(key:K, val:V):Void {
  39. Syntax.arraySet(this, key, val);
  40. }
  41. inline function remove(key:K):Void {
  42. Syntax.delete(python.Syntax.arrayAccess(this, key));
  43. }
  44. inline function hasKey(k:K):Bool {
  45. return Syntax.isIn(k, this);
  46. }
  47. function clear():Void;
  48. function copy():Dict<K, V>;
  49. function items():DictView<Tuple2<K, V>>;
  50. function keys():DictView<K>;
  51. function pop(key:K, ?def:V):V;
  52. function popitem():Tuple2<K, V>;
  53. function setdefault(key:K, ?def:V):V;
  54. function update(d:Dict<K, V>):Void;
  55. function values():DictView<V>;
  56. inline function iter():NativeIterator<K> {
  57. return UBuiltins.iter(this);
  58. }
  59. inline function iterator():Iterator<V> {
  60. return values().iter();
  61. }
  62. private function __iter__():NativeIterator<K>;
  63. }
  64. extern class DictView<T> {
  65. var length(get, never):Int;
  66. private inline function get_length():Int {
  67. return UBuiltins.len(this);
  68. }
  69. inline function iter():NativeIterator<T> {
  70. return UBuiltins.iter(this);
  71. }
  72. inline function iterator():Iterator<T> {
  73. return iter();
  74. }
  75. private function __iter__():NativeIterator<T>;
  76. }