AVLMap.ts 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. import { JavaMapEntry, cast_java_util_Map_Entry } from '../../../java/util/JavaMapEntry';
  2. import { Comparable, cast_java_lang_Comparable } from '../../../java/lang/Comparable';
  3. import { NavigableSet, cast_java_util_NavigableSet } from '../../../java/util/NavigableSet';
  4. import { JavaSet, cast_java_util_Set } from '../../../java/util/JavaSet';
  5. import { NavigableMap, cast_java_util_NavigableMap } from '../../../java/util/NavigableMap';
  6. import { AVLMapIntervall, cast_de_nrw_schule_svws_core_adt_map_AVLMapIntervall } from '../../../core/adt/map/AVLMapIntervall';
  7. import { JavaString, cast_java_lang_String } from '../../../java/lang/JavaString';
  8. import { Comparator, cast_java_util_Comparator } from '../../../java/util/Comparator';
  9. import { NullPointerException, cast_java_lang_NullPointerException } from '../../../java/lang/NullPointerException';
  10. import { AVLMapNode, cast_de_nrw_schule_svws_core_adt_map_AVLMapNode } from '../../../core/adt/map/AVLMapNode';
  11. import { ClassCastException, cast_java_lang_ClassCastException } from '../../../java/lang/ClassCastException';
  12. import { SortedMap, cast_java_util_SortedMap } from '../../../java/util/SortedMap';
  13. import { Collection, cast_java_util_Collection } from '../../../java/util/Collection';
  14. import { JavaObject, cast_java_lang_Object } from '../../../java/lang/JavaObject';
  15. import { JavaMap, cast_java_util_Map } from '../../../java/util/JavaMap';
  16. import { IllegalArgumentException, cast_java_lang_IllegalArgumentException } from '../../../java/lang/IllegalArgumentException';
  17. import { NoSuchElementException, cast_java_util_NoSuchElementException } from '../../../java/util/NoSuchElementException';
  18. import { AVLMapSubMap, cast_de_nrw_schule_svws_core_adt_map_AVLMapSubMap } from '../../../core/adt/map/AVLMapSubMap';
  19. import { UnsupportedOperationException, cast_java_lang_UnsupportedOperationException } from '../../../java/lang/UnsupportedOperationException';
  20. export class AVLMap<K, V> extends JavaObject implements NavigableMap<K, V> {
  21. private readonly _infinityMinus : K = AVLMapIntervall._INFINITY_MINUS as unknown as K;
  22. private readonly _infinityPlus : K = AVLMapIntervall._INFINITY_PLUS as unknown as K;
  23. private readonly _dummyValue : V = new Object() as unknown as V;
  24. private readonly _sub : AVLMapSubMap<K, V> = new AVLMapSubMap(this, new AVLMapIntervall(this._infinityMinus, false, this._infinityPlus, false), true);
  25. private readonly _comparator : Comparator<K>;
  26. private readonly _comparatorNatural : Comparator<K> = { compare : (key1: K, key2: K) => {
  27. if ((key1 === null) || (key2 === null))
  28. throw new NullPointerException()
  29. if (!((((key1 instanceof JavaObject) && (key1.isTranspiledInstanceOf('java.lang.Comparable')))) && (((key2 instanceof JavaObject) && (key2.isTranspiledInstanceOf('java.lang.Comparable'))))))
  30. throw new ClassCastException()
  31. let k1 : Comparable<K> = cast_java_lang_Comparable(key1);
  32. return k1.compareTo(key2);
  33. } };
  34. private _root : AVLMapNode<K, V> | null = null;
  35. private _allowKeyAlone : boolean = false;
  36. /**
  37. * Erzeugt einen leere Map, welche bei den Schlüsselwerten die natürliche Ordnung des {@link Comparable} -
  38. * Interface nutzt.
  39. */
  40. public constructor();
  41. /**
  42. * Erstellt eine neue leere Map und nutzt dabei die angegeben Ordnung der Schlüssel.
  43. *
  44. * @param comparator Die Ordnung für die Schlüssel.
  45. */
  46. public constructor(comparator : Comparator<K>);
  47. /**
  48. * Erstellt eine neue Map mit den Daten aus der angegebenen Map und nutzt dabei die Ordnung dieser Map.
  49. *
  50. * @param map Die Map mit den Daten.
  51. */
  52. public constructor(map : SortedMap<K, V>);
  53. /**
  54. * Implementation for method overloads of 'constructor'
  55. */
  56. public constructor(__param0? : Comparator<K> | SortedMap<K, V>) {
  57. super();
  58. if ((typeof __param0 === "undefined")) {
  59. this._comparator = this._comparatorNatural;
  60. } else if (((typeof __param0 !== "undefined") && ((typeof __param0 !== 'undefined') && (__param0 instanceof Object) && (__param0 !== null) && ('compare' in __param0) && (typeof __param0.compare === 'function')) || (__param0 === null))) {
  61. let comparator : Comparator<K> = cast_java_util_Comparator(__param0);
  62. this._comparator = comparator;
  63. } else if (((typeof __param0 !== "undefined") && ((__param0 instanceof JavaObject) && (__param0.isTranspiledInstanceOf('java.util.SortedMap'))) || (__param0 === null))) {
  64. let map : SortedMap<K, V> = cast_java_util_SortedMap(__param0);
  65. this._comparator = cast_java_util_Comparator(map.comparator());
  66. this._sub.putAll(map);
  67. } else throw new Error('invalid method overload');
  68. }
  69. public toString() : String {
  70. return this._sub.toString();
  71. }
  72. /**
  73. * Bewirkt, dass das Hinzufügen von Keys ohne Value durch {@link AVLMapSubKeySet} erlaubt ist. Die Keys werden
  74. * auf einen Dummy-Wert gemapped.
  75. *
  76. * @param b Falls TRUE, dürfen KEYs ohne VALUE hinzugefügt werden.
  77. */
  78. public allowKeyAlone(b : boolean) : void {
  79. this._allowKeyAlone = b;
  80. }
  81. public equals(o : unknown) : boolean {
  82. return JavaObject.equalsTranspiler(this._sub, (o));
  83. }
  84. public hashCode() : number {
  85. return JavaObject.getTranspilerHashCode(this._sub);
  86. }
  87. public comparator() : Comparator<K> {
  88. return this._sub.comparator();
  89. }
  90. public firstKey() : K {
  91. return this._sub.firstKey();
  92. }
  93. public lastKey() : K {
  94. return this._sub.lastKey();
  95. }
  96. public keySet() : JavaSet<K> {
  97. return this._sub.keySet();
  98. }
  99. public values() : Collection<V> {
  100. return this._sub.values();
  101. }
  102. public entrySet() : JavaSet<JavaMapEntry<K, V>> {
  103. return this._sub.entrySet();
  104. }
  105. public size() : number {
  106. return this._sub.size();
  107. }
  108. public isEmpty() : boolean {
  109. return this._sub.isEmpty();
  110. }
  111. public containsKey(key : unknown) : boolean {
  112. return this._sub.containsKey(key);
  113. }
  114. public containsValue(value : unknown) : boolean {
  115. return this._sub.containsValue(value);
  116. }
  117. public get(key : unknown) : V | null {
  118. return this._sub.get(key);
  119. }
  120. public put(key : K, value : V) : V | null {
  121. return this._sub.put(key, value);
  122. }
  123. public remove(key : unknown) : V | null {
  124. return this._sub.remove(key);
  125. }
  126. public putAll(m : JavaMap<K, V>) : void {
  127. this._sub.putAll(m);
  128. }
  129. public clear() : void {
  130. this._sub.clear();
  131. }
  132. public lowerEntry(key : K) : JavaMapEntry<K, V> | null {
  133. return this._sub.lowerEntry(key);
  134. }
  135. public lowerKey(key : K) : K | null {
  136. return this._sub.lowerKey(key);
  137. }
  138. public floorEntry(key : K) : JavaMapEntry<K, V> | null {
  139. return this._sub.floorEntry(key);
  140. }
  141. public floorKey(key : K) : K | null {
  142. return this._sub.floorKey(key);
  143. }
  144. public ceilingEntry(key : K) : JavaMapEntry<K, V> | null {
  145. return this._sub.ceilingEntry(key);
  146. }
  147. public ceilingKey(key : K) : K | null {
  148. return this._sub.ceilingKey(key);
  149. }
  150. public higherEntry(key : K) : JavaMapEntry<K, V> | null {
  151. return this._sub.higherEntry(key);
  152. }
  153. public higherKey(key : K) : K | null {
  154. return this._sub.higherKey(key);
  155. }
  156. public firstEntry() : JavaMapEntry<K, V> | null {
  157. return this._sub.firstEntry();
  158. }
  159. public lastEntry() : JavaMapEntry<K, V> | null {
  160. return this._sub.lastEntry();
  161. }
  162. public pollFirstEntry() : JavaMapEntry<K, V> | null {
  163. return this._sub.pollFirstEntry();
  164. }
  165. public pollLastEntry() : JavaMapEntry<K, V> | null {
  166. return this._sub.pollLastEntry();
  167. }
  168. public descendingMap() : NavigableMap<K, V> {
  169. return this._sub.descendingMap();
  170. }
  171. public navigableKeySet() : NavigableSet<K> {
  172. return this._sub.navigableKeySet();
  173. }
  174. public descendingKeySet() : NavigableSet<K> {
  175. return this._sub.descendingKeySet();
  176. }
  177. public subMap(fromKey : K, fromInclusive : boolean, toKey : K, toInclusive : boolean) : NavigableMap<K, V>;
  178. public subMap(fromKey : K, toKey : K) : SortedMap<K, V>;
  179. /**
  180. * Implementation for method overloads of 'subMap'
  181. */
  182. public subMap(__param0 : K, __param1 : K | boolean, __param2? : K, __param3? : boolean) : NavigableMap<K, V> | SortedMap<K, V> {
  183. if (((typeof __param0 !== "undefined") && (typeof __param0 !== "undefined")) && ((typeof __param1 !== "undefined") && typeof __param1 === "boolean") && ((typeof __param2 !== "undefined") && (typeof __param2 !== "undefined")) && ((typeof __param3 !== "undefined") && typeof __param3 === "boolean")) {
  184. let fromKey : K = __param0 as unknown as K;
  185. let fromInclusive : boolean = __param1 as boolean;
  186. let toKey : K = __param2 as unknown as K;
  187. let toInclusive : boolean = __param3 as boolean;
  188. return this._sub.subMap(fromKey, fromInclusive, toKey, toInclusive);
  189. } else if (((typeof __param0 !== "undefined") && (typeof __param0 !== "undefined")) && ((typeof __param1 !== "undefined") && (typeof __param1 !== "undefined")) && (typeof __param2 === "undefined") && (typeof __param3 === "undefined")) {
  190. let fromKey : K = __param0 as unknown as K;
  191. let toKey : K = __param1 as unknown as K;
  192. return this._sub.subMap(fromKey, toKey);
  193. } else throw new Error('invalid method overload');
  194. }
  195. public headMap(toKey : K, inclusive : boolean) : NavigableMap<K, V>;
  196. public headMap(toKey : K) : SortedMap<K, V>;
  197. /**
  198. * Implementation for method overloads of 'headMap'
  199. */
  200. public headMap(__param0 : K, __param1? : boolean) : NavigableMap<K, V> | SortedMap<K, V> {
  201. if (((typeof __param0 !== "undefined") && (typeof __param0 !== "undefined")) && ((typeof __param1 !== "undefined") && typeof __param1 === "boolean")) {
  202. let toKey : K = __param0 as unknown as K;
  203. let inclusive : boolean = __param1 as boolean;
  204. return this._sub.headMap(toKey, inclusive);
  205. } else if (((typeof __param0 !== "undefined") && (typeof __param0 !== "undefined")) && (typeof __param1 === "undefined")) {
  206. let toKey : K = __param0 as unknown as K;
  207. return this._sub.headMap(toKey);
  208. } else throw new Error('invalid method overload');
  209. }
  210. public tailMap(fromKey : K, inclusive : boolean) : NavigableMap<K, V>;
  211. public tailMap(fromKey : K) : SortedMap<K, V>;
  212. /**
  213. * Implementation for method overloads of 'tailMap'
  214. */
  215. public tailMap(__param0 : K, __param1? : boolean) : NavigableMap<K, V> | SortedMap<K, V> {
  216. if (((typeof __param0 !== "undefined") && (typeof __param0 !== "undefined")) && ((typeof __param1 !== "undefined") && typeof __param1 === "boolean")) {
  217. let fromKey : K = __param0 as unknown as K;
  218. let inclusive : boolean = __param1 as boolean;
  219. return this._sub.tailMap(fromKey, inclusive);
  220. } else if (((typeof __param0 !== "undefined") && (typeof __param0 !== "undefined")) && (typeof __param1 === "undefined")) {
  221. let fromKey : K = __param0 as unknown as K;
  222. return this._sub.tailMap(fromKey);
  223. } else throw new Error('invalid method overload');
  224. }
  225. bcAddEntryReturnBool(e : JavaMapEntry<K, V>, iv : AVLMapIntervall<K>) : boolean {
  226. let old : V | null = this.bcAddEntryReturnOldValueOrNull(e.getKey(), e.getValue(), iv);
  227. return !this._valEquals(old, e.getValue());
  228. }
  229. bcAddAllEntries(c : Collection<JavaMapEntry<K, V>>, iv : AVLMapIntervall<K>) : boolean {
  230. let changed : boolean = false;
  231. for (let entry of c)
  232. changed = changed || this.bcAddEntryReturnBool(entry, iv);
  233. return changed;
  234. }
  235. bcAddEntryReturnOldValueOrNull(key : K, value : V, iv : AVLMapIntervall<K>) : V | null {
  236. if (key === null)
  237. throw new NullPointerException("TreeMap erlaubt keine NULL keys.")
  238. if (this._isOutOfRange(key, iv))
  239. throw new IllegalArgumentException("Der Schlüsselwert liegt nicht im gültigen Bereich.")
  240. if (this._root === null) {
  241. this._root = new AVLMapNode(key, value);
  242. return null;
  243. }
  244. let node : AVLMapNode<K, V> | null = this._nodeGetOrNull(key, iv);
  245. let old : V | null = (node === null) ? null : node._val;
  246. this._root = this._nodePutRecursive(this._root, key, value);
  247. return old;
  248. }
  249. bcAddValue(e : V, iv : AVLMapIntervall<K>) : boolean {
  250. throw new UnsupportedOperationException()
  251. }
  252. bcAddKey(e : K, iv : AVLMapIntervall<K>) : boolean {
  253. if (this._allowKeyAlone === false)
  254. throw new UnsupportedOperationException()
  255. if (this.bcContainsKey(e, iv))
  256. return false;
  257. this.bcAddEntryReturnOldValueOrNull(e, this._dummyValue, iv);
  258. return true;
  259. }
  260. bcAddAllKeys(c : Collection<K>, iv : AVLMapIntervall<K>) : boolean {
  261. let changed : boolean = false;
  262. for (let key of c)
  263. changed = changed || this.bcAddKey(key, iv);
  264. return changed;
  265. }
  266. bcAddAllEntriesOfMap(map : JavaMap<K, V>, iv : AVLMapIntervall<K>) : void {
  267. for (let entry of map.entrySet())
  268. this.bcAddEntryReturnOldValueOrNull(entry.getKey(), entry.getValue(), iv);
  269. }
  270. bcContainsKey(objKey : unknown, iv : AVLMapIntervall<K>) : boolean {
  271. return this._nodeGetOrNull(objKey as unknown as K, iv) !== null;
  272. }
  273. bcContainsAllKeys(c : Collection<unknown>, iv : AVLMapIntervall<K>) : boolean {
  274. for (let key of c)
  275. if (!this.bcContainsKey(key, iv))
  276. return false;
  277. return true;
  278. }
  279. bcContainsValue(objValue : unknown, iv : AVLMapIntervall<K>) : boolean {
  280. let value : V = objValue as unknown as V;
  281. let n1 : AVLMapNode<K, V> | null = this._nodeFirstOrNull(iv);
  282. if (n1 === null)
  283. return false;
  284. let n2 : AVLMapNode<K, V> | null = this._nodeLastOrNull(iv);
  285. if (n2 === null)
  286. return false;
  287. while (n1 as unknown !== n2 as unknown) {
  288. if (n1 === null)
  289. throw new NullPointerException()
  290. if (this._valEquals(n1._val, value))
  291. return true;
  292. n1 = n1._next;
  293. }
  294. return this._valEquals(n2._val, value);
  295. }
  296. bcContainsAllValues(col : Collection<unknown>, iv : AVLMapIntervall<K>) : boolean {
  297. for (let val of col)
  298. if (!this.bcContainsValue(val, iv))
  299. return false;
  300. return true;
  301. }
  302. bcContainsEntry(o : unknown, iv : AVLMapIntervall<K>) : boolean {
  303. if (((o instanceof JavaObject) && (o.isTranspiledInstanceOf('java.util.Map.Entry'))) === false)
  304. return false;
  305. let e : JavaMapEntry<K, V> = cast_java_util_Map_Entry(o);
  306. let node : AVLMapNode<K, V> | null = this._nodeGetOrNull(e.getKey(), iv);
  307. return (node === null) ? false : this._valEquals(node._val, e.getValue());
  308. }
  309. bcContainsAllEntries(c : Collection<unknown>, iv : AVLMapIntervall<K>) : boolean {
  310. for (let entry of c)
  311. if (!this.bcContainsEntry(entry, iv))
  312. return false;
  313. return true;
  314. }
  315. bcRemoveKeyReturnBool(o : unknown, iv : AVLMapIntervall<K>) : boolean {
  316. if (!this.bcContainsKey(o, iv))
  317. return false;
  318. this.bcRemoveKeyReturnOldValueOrNull(o, iv);
  319. return true;
  320. }
  321. bcRemoveAllKeys(c : Collection<unknown>, iv : AVLMapIntervall<K>) : boolean {
  322. let changed : boolean = false;
  323. for (let obj of c)
  324. changed = changed || this.bcRemoveKeyReturnBool(obj, iv);
  325. return changed;
  326. }
  327. bcRemoveEntry(o : unknown, iv : AVLMapIntervall<K>) : boolean {
  328. if (!this.bcContainsEntry(o, iv))
  329. return false;
  330. if (((o instanceof JavaObject) && (o.isTranspiledInstanceOf('java.util.Map.Entry'))) === false)
  331. return false;
  332. if (this._root === null)
  333. throw new NullPointerException()
  334. let e : JavaMapEntry<K, V> = cast_java_util_Map_Entry(o);
  335. this._root = this._nodeRemoveKeyRecursive(this._root, e.getKey());
  336. return true;
  337. }
  338. bcRemoveAllEntries(c : Collection<unknown>, iv : AVLMapIntervall<K>) : boolean {
  339. let removedAny : boolean = false;
  340. for (let entry of c)
  341. removedAny = removedAny || this.bcRemoveEntry(entry, iv);
  342. return removedAny;
  343. }
  344. bcRemoveKeyReturnOldValueOrNull(obj : unknown, iv : AVLMapIntervall<K>) : V | null {
  345. if (obj === null)
  346. throw new NullPointerException("TreeMap unterstützt keine NULL-Schlüssel.")
  347. let key : K = obj as unknown as K;
  348. let old : AVLMapNode<K, V> | null = this._nodeGetOrNull(key, iv);
  349. if (old === null)
  350. return null;
  351. if (this._root === null)
  352. throw new NullPointerException()
  353. this._root = this._nodeRemoveKeyRecursive(this._root, key);
  354. return old._val;
  355. }
  356. bcPollFirstEntryOrNull(iv : AVLMapIntervall<K>) : JavaMapEntry<K, V> | null {
  357. let node : AVLMapNode<K, V> | null = this._nodeFirstOrNull(iv);
  358. if (node === null)
  359. return node;
  360. if (this._root === null)
  361. throw new NullPointerException()
  362. this._root = this._nodeRemoveKeyRecursive(this._root, node._key);
  363. return node;
  364. }
  365. bcPollFirstKeyOrNull(iv : AVLMapIntervall<K>) : K | null {
  366. let node : AVLMapNode<K, V> | null = this._nodeFirstOrNull(iv);
  367. if (node === null)
  368. return null;
  369. if (this._root === null)
  370. throw new NullPointerException()
  371. this._root = this._nodeRemoveKeyRecursive(this._root, node._key);
  372. return node._key;
  373. }
  374. bcPollLastEntryOrNull(iv : AVLMapIntervall<K>) : JavaMapEntry<K, V> | null {
  375. let node : AVLMapNode<K, V> | null = this._nodeLastOrNull(iv);
  376. if (node === null)
  377. return null;
  378. if (this._root === null)
  379. throw new NullPointerException()
  380. this._root = this._nodeRemoveKeyRecursive(this._root, node._key);
  381. return node;
  382. }
  383. bcPollLastKeyOrNull(iv : AVLMapIntervall<K>) : K | null {
  384. let node : AVLMapNode<K, V> | null = this._nodeLastOrNull(iv);
  385. if (node === null)
  386. return null;
  387. if (this._root === null)
  388. throw new NullPointerException()
  389. this._root = this._nodeRemoveKeyRecursive(this._root, node._key);
  390. return node._key;
  391. }
  392. bcIsEmpty(iv : AVLMapIntervall<K>) : boolean {
  393. return this._nodeFirstOrNull(iv) === null;
  394. }
  395. bcGetComparator(iv : AVLMapIntervall<K>) : Comparator<K> {
  396. return this._comparator;
  397. }
  398. bcGetFirstKeyOrException(iv : AVLMapIntervall<K>) : K {
  399. return this._keyOrExeption(this._nodeFirstOrNull(iv));
  400. }
  401. bcGetFirstEntryOrNull(iv : AVLMapIntervall<K>) : AVLMapNode<K, V> | null {
  402. return this._nodeFirstOrNull(iv);
  403. }
  404. bcGetLastKeyOrException(iv : AVLMapIntervall<K>) : K {
  405. return this._keyOrExeption(this._nodeLastOrNull(iv));
  406. }
  407. bcGetLastEntryOrNull(iv : AVLMapIntervall<K>) : AVLMapNode<K, V> | null {
  408. return this._nodeLastOrNull(iv);
  409. }
  410. bcGetNextEntryOrNull(current : AVLMapNode<K, V>, iv : AVLMapIntervall<K>) : AVLMapNode<K, V> | null {
  411. return this._nodeNextOrNull(current, iv);
  412. }
  413. bcGetPrevEntryOrNull(current : AVLMapNode<K, V>, iv : AVLMapIntervall<K>) : AVLMapNode<K, V> | null {
  414. return this._nodePrevOrNull(current, iv);
  415. }
  416. bcGetSize(iv : AVLMapIntervall<K>) : number {
  417. let n1 : AVLMapNode<K, V> | null = this._nodeFirstOrNull(iv);
  418. if (n1 === null)
  419. return 0;
  420. let n2 : AVLMapNode<K, V> | null = this._nodeLastOrNull(iv);
  421. if (n2 === null)
  422. return 0;
  423. return this._nodeIndexOf(n2._key) - this._nodeIndexOf(n1._key) + 1;
  424. }
  425. bcGetValueOfKeyOrNull(objKey : unknown, iv : AVLMapIntervall<K>) : V | null {
  426. let key : K = objKey as unknown as K;
  427. let node : AVLMapNode<K, V> | null = this._nodeGetOrNull(key, iv);
  428. return (node === null) ? null : node._val;
  429. }
  430. bcGetLowerEntryOrNull(key : K, iv : AVLMapIntervall<K>) : AVLMapNode<K, V> | null {
  431. return this._nodeLowerOrNull(key, iv);
  432. }
  433. bcGetLowerKeyOrNull(key : K, iv : AVLMapIntervall<K>) : K | null {
  434. return this._keyOrNull(this._nodeLowerOrNull(key, iv));
  435. }
  436. bcGetFloorEntryOrNull(key : K, iv : AVLMapIntervall<K>) : AVLMapNode<K, V> | null {
  437. return this._nodeFloorOrNull(key, iv);
  438. }
  439. bcGetFloorKeyOrNull(key : K, iv : AVLMapIntervall<K>) : K | null {
  440. return this._keyOrNull(this._nodeFloorOrNull(key, iv));
  441. }
  442. bcGetCeilingEntryOrNull(key : K, iv : AVLMapIntervall<K>) : AVLMapNode<K, V> | null {
  443. return this._nodeCeilingOrNull(key, iv);
  444. }
  445. bcGetCeilingKeyOrNull(key : K, iv : AVLMapIntervall<K>) : K | null {
  446. return this._keyOrNull(this._nodeCeilingOrNull(key, iv));
  447. }
  448. bcGetHigherEntryOrNull(key : K, iv : AVLMapIntervall<K>) : AVLMapNode<K, V> | null {
  449. return this._nodeHigherOrNull(key, iv);
  450. }
  451. bcGetHigherKeyOrNull(key : K, iv : AVLMapIntervall<K>) : K | null {
  452. return this._keyOrNull(this._nodeHigherOrNull(key, iv));
  453. }
  454. bcCheckOutOfIntervall(key : K, inc : boolean, iv : AVLMapIntervall<K>) : boolean {
  455. if ((key === this._infinityMinus) || (key === this._infinityPlus))
  456. return false;
  457. let cmpF : number = this._compare(key, iv.from);
  458. if (cmpF < 0)
  459. return true;
  460. if ((cmpF === 0) && (!iv.fromInc) && (inc))
  461. return true;
  462. let cmpT : number = this._compare(key, iv.to);
  463. if (cmpT > 0)
  464. return true;
  465. if ((cmpT === 0) && (!iv.toInc) && (inc))
  466. return true;
  467. return false;
  468. }
  469. private _keyOrNull(node : AVLMapNode<K, V> | null) : K | null {
  470. return (node === null) ? null : node._key;
  471. }
  472. private _valEquals(v1 : V | null, v2 : V | null) : boolean {
  473. return (v1 === null) ? v2 === null : JavaObject.equalsTranspiler(v1, (v2));
  474. }
  475. private _keyOrExeption(node : AVLMapNode<K, V> | null) : K {
  476. if (node === null)
  477. throw new NoSuchElementException()
  478. return node._key;
  479. }
  480. private _compare(key1 : K, key2 : K) : number {
  481. if ((key1 === this._infinityMinus) || (key2 === this._infinityPlus))
  482. return -1;
  483. if ((key1 === this._infinityPlus) || (key2 === this._infinityMinus))
  484. return +1;
  485. return this._comparator.compare(key1, key2);
  486. }
  487. private _isOutOfRange(key : K, iv : AVLMapIntervall<K>) : boolean {
  488. let cmpKeyFrom : number = this._compare(key, iv.from);
  489. if ((cmpKeyFrom < 0) || (cmpKeyFrom === 0) && (!iv.fromInc))
  490. return true;
  491. let cmpKeyTo : number = this._compare(key, iv.to);
  492. if ((cmpKeyTo > 0) || (cmpKeyTo === 0) && (!iv.toInc))
  493. return true;
  494. return false;
  495. }
  496. private _nodeFirstOrNull(iv : AVLMapIntervall<K>) : AVLMapNode<K, V> | null {
  497. return iv.fromInc ? this._nodeCeilingOrNull(iv.from, iv) : this._nodeHigherOrNull(iv.from, iv);
  498. }
  499. private _nodeLastOrNull(iv : AVLMapIntervall<K>) : AVLMapNode<K, V> | null {
  500. return iv.toInc ? this._nodeFloorOrNull(iv.to, iv) : this._nodeLowerOrNull(iv.to, iv);
  501. }
  502. private _nodeCeilingOrNull(key : K, iv : AVLMapIntervall<K>) : AVLMapNode<K, V> | null {
  503. let node : AVLMapNode<K, V> | null = this._nodeDeepestOrNull(key, iv);
  504. if (node === null)
  505. return null;
  506. let cmpNodeKey : number = this._compare(node._key, key);
  507. return cmpNodeKey >= 0 ? node : this._nodeNextOrNull(node, iv);
  508. }
  509. private _nodeHigherOrNull(key : K, iv : AVLMapIntervall<K>) : AVLMapNode<K, V> | null {
  510. let node : AVLMapNode<K, V> | null = this._nodeDeepestOrNull(key, iv);
  511. if (node === null)
  512. return null;
  513. let cmpNodeKey : number = this._compare(node._key, key);
  514. return cmpNodeKey > 0 ? node : this._nodeNextOrNull(node, iv);
  515. }
  516. private _nodeFloorOrNull(key : K, iv : AVLMapIntervall<K>) : AVLMapNode<K, V> | null {
  517. let node : AVLMapNode<K, V> | null = this._nodeDeepestOrNull(key, iv);
  518. if (node === null)
  519. return null;
  520. let cmpNodeKey : number = this._compare(node._key, key);
  521. return cmpNodeKey <= 0 ? node : this._nodePrevOrNull(node, iv);
  522. }
  523. private _nodeLowerOrNull(key : K, iv : AVLMapIntervall<K>) : AVLMapNode<K, V> | null {
  524. let node : AVLMapNode<K, V> | null = this._nodeDeepestOrNull(key, iv);
  525. if (node === null)
  526. return null;
  527. let cmpNodeKey : number = this._compare(node._key, key);
  528. return cmpNodeKey < 0 ? node : this._nodePrevOrNull(node, iv);
  529. }
  530. private _nodeNextOrNull(node : AVLMapNode<K, V>, iv : AVLMapIntervall<K>) : AVLMapNode<K, V> | null {
  531. let next : AVLMapNode<K, V> | null = node._next;
  532. return (next === null) ? null : this._isOutOfRange(next._key, iv) ? null : next;
  533. }
  534. private _nodePrevOrNull(node : AVLMapNode<K, V>, iv : AVLMapIntervall<K>) : AVLMapNode<K, V> | null {
  535. let prev : AVLMapNode<K, V> | null = node._prev;
  536. return (prev === null) ? null : this._isOutOfRange(prev._key, iv) ? null : prev;
  537. }
  538. private _nodeGetOrNull(key : K, iv : AVLMapIntervall<K>) : AVLMapNode<K, V> | null {
  539. let node : AVLMapNode<K, V> | null = this._nodeDeepestOrNull(key, iv);
  540. if (node === null)
  541. return null;
  542. return this._compare(key, node._key) === 0 ? node : null;
  543. }
  544. private _nodeIndexOf(key : K) : number {
  545. let current : AVLMapNode<K, V> | null = this._root;
  546. let index : number = 0;
  547. while (true) {
  548. if (current === null)
  549. throw new NullPointerException()
  550. let cmp : number = this._compare(key, current._key);
  551. if (cmp < 0) {
  552. current = current._childL;
  553. continue;
  554. }
  555. let left : AVLMapNode<K, V> | null = current._childL;
  556. let sizeL : number = (left === null) ? 0 : left._size;
  557. if (cmp > 0) {
  558. index += sizeL + 1;
  559. current = current._childR;
  560. continue;
  561. }
  562. return index + sizeL;
  563. }
  564. }
  565. private _nodeDeepestOrNull(key : K, iv : AVLMapIntervall<K>) : AVLMapNode<K, V> | null {
  566. let current : AVLMapNode<K, V> | null = this._root;
  567. let last : AVLMapNode<K, V> | null = null;
  568. while (current !== null) {
  569. let cmpToKey : number = this._compare(iv.to, current._key);
  570. if ((cmpToKey < 0) || (cmpToKey === 0) && (!iv.toInc)) {
  571. current = current._childL;
  572. continue;
  573. }
  574. let cmpFromKey : number = this._compare(iv.from, current._key);
  575. if ((cmpFromKey > 0) || (cmpFromKey === 0) && (!iv.fromInc)) {
  576. current = current._childR;
  577. continue;
  578. }
  579. last = current;
  580. let cmp : number = this._compare(key, current._key);
  581. if (cmp < 0) {
  582. current = current._childL;
  583. continue;
  584. }
  585. if (cmp > 0) {
  586. current = current._childR;
  587. continue;
  588. }
  589. return current;
  590. }
  591. return last;
  592. }
  593. private _nodePutRecursive(current : AVLMapNode<K, V>, key : K, value : V) : AVLMapNode<K, V> {
  594. let cmp : number = this._compare(key, current._key);
  595. if (cmp === 0) {
  596. current._val = value;
  597. return current;
  598. }
  599. if (cmp < 0)
  600. current._childL = (current._childL === null) ? this._nodeCreateLeaf(current._prev, current, key, value) : this._nodePutRecursive(current._childL, key, value); else
  601. current._childR = (current._childR === null) ? this._nodeCreateLeaf(current, current._next, key, value) : this._nodePutRecursive(current._childR, key, value);
  602. return this._nodeRevalidate(current);
  603. }
  604. private _nodeCreateLeaf(prev : AVLMapNode<K, V> | null, next : AVLMapNode<K, V> | null, key : K, value : V) : AVLMapNode<K, V> {
  605. let child : AVLMapNode<K, V> | null = new AVLMapNode(key, value);
  606. if (prev !== null) {
  607. prev._next = child;
  608. child._prev = prev;
  609. }
  610. if (next !== null) {
  611. next._prev = child;
  612. child._next = next;
  613. }
  614. return child;
  615. }
  616. private _nodeRemoveKeyRecursive(current : AVLMapNode<K, V>, key : K) : AVLMapNode<K, V> | null {
  617. let cmp : number = this._compare(key, current._key);
  618. if (cmp < 0) {
  619. if (current._childL === null)
  620. throw new NullPointerException()
  621. current._childL = this._nodeRemoveKeyRecursive(current._childL, key);
  622. return this._nodeRevalidate(current);
  623. }
  624. if (cmp > 0) {
  625. if (current._childR === null)
  626. throw new NullPointerException()
  627. current._childR = this._nodeRemoveKeyRecursive(current._childR, key);
  628. return this._nodeRevalidate(current);
  629. }
  630. if (current._childL === null) {
  631. this._nodeRemovePrevNext(current);
  632. return current._childR;
  633. }
  634. if (current._childR === null) {
  635. this._nodeRemovePrevNext(current);
  636. return current._childL;
  637. }
  638. let next : AVLMapNode<K, V> | null = current._next;
  639. if (next === null)
  640. throw new NullPointerException()
  641. current._childR = this._nodeRemoveKeyRecursive(current._childR, next._key);
  642. return this._nodeRevalidate(this._nodeReplaceReferencesFromAwithB(next, current));
  643. }
  644. private _nodeReplaceReferencesFromAwithB(a : AVLMapNode<K, V>, b : AVLMapNode<K, V>) : AVLMapNode<K, V> {
  645. a._childL = b._childL;
  646. a._childR = b._childR;
  647. let p : AVLMapNode<K, V> | null = b._prev;
  648. let n : AVLMapNode<K, V> | null = b._next;
  649. a._prev = p;
  650. a._next = n;
  651. if (p !== null)
  652. p._next = a;
  653. if (n !== null)
  654. n._prev = a;
  655. return a;
  656. }
  657. private _nodeRemovePrevNext(current : AVLMapNode<K, V>) : void {
  658. let nodeP : AVLMapNode<K, V> | null = current._prev;
  659. let nodeN : AVLMapNode<K, V> | null = current._next;
  660. if (nodeP !== null)
  661. nodeP._next = nodeN;
  662. if (nodeN !== null)
  663. nodeN._prev = nodeP;
  664. }
  665. /**
  666. * Aktualisiert {@code node} und liefert, wenn es zur Rotation kommt, eine neue Sub-Wurzel.
  667. *
  668. * @param node Der Knoten, der revalidiert werden soll.
  669. *
  670. * @return node, oder die neue Sub-Wurzel, wenn es zur Rotation kam.
  671. */
  672. private _nodeRevalidate(node : AVLMapNode<K, V>) : AVLMapNode<K, V> {
  673. let heightBalance : number = this._nodeGetHeightBalance(node);
  674. if (heightBalance > +1) {
  675. if (node._childR === null)
  676. throw new NullPointerException()
  677. if (this._nodeGetHeightBalance(node._childR) < 0)
  678. node._childR = this._nodeRotateRight(node._childR);
  679. return this._nodeRotateLeft(node);
  680. }
  681. if (heightBalance < -1) {
  682. if (node._childL === null)
  683. throw new NullPointerException()
  684. if (this._nodeGetHeightBalance(node._childL) > 0)
  685. node._childL = this._nodeRotateLeft(node._childL);
  686. return this._nodeRotateRight(node);
  687. }
  688. this._nodeRevalidateHeightAndSize(node);
  689. return node;
  690. }
  691. private _nodeRotateLeft(nodeM : AVLMapNode<K, V>) : AVLMapNode<K, V> {
  692. if (nodeM._childR === null)
  693. throw new NullPointerException()
  694. let nodeR : AVLMapNode<K, V> = nodeM._childR;
  695. nodeM._childR = nodeR._childL;
  696. nodeR._childL = nodeM;
  697. this._nodeRevalidateHeightAndSize(nodeM);
  698. this._nodeRevalidateHeightAndSize(nodeR);
  699. return nodeR;
  700. }
  701. private _nodeRotateRight(nodeM : AVLMapNode<K, V>) : AVLMapNode<K, V> {
  702. if (nodeM._childL === null)
  703. throw new NullPointerException()
  704. let nodeL : AVLMapNode<K, V> = nodeM._childL;
  705. nodeM._childL = nodeL._childR;
  706. nodeL._childR = nodeM;
  707. this._nodeRevalidateHeightAndSize(nodeM);
  708. this._nodeRevalidateHeightAndSize(nodeL);
  709. return nodeL;
  710. }
  711. private _nodeRevalidateHeightAndSize(node : AVLMapNode<K, V>) : void {
  712. let sizeL : number = (node._childL === null) ? 0 : node._childL._size;
  713. let sizeR : number = (node._childR === null) ? 0 : node._childR._size;
  714. node._size = sizeL + sizeR + 1;
  715. let heightL : number = (node._childL === null) ? 0 : node._childL._height;
  716. let heightR : number = (node._childR === null) ? 0 : node._childR._height;
  717. node._height = Math.max(heightL, heightR) + 1;
  718. }
  719. private _nodeGetHeightBalance(node : AVLMapNode<K, V>) : number {
  720. let heightL : number = (node._childL === null) ? 0 : node._childL._height;
  721. let heightR : number = (node._childR === null) ? 0 : node._childR._height;
  722. return heightR - heightL;
  723. }
  724. isTranspiledInstanceOf(name : string): boolean {
  725. return ['java.util.Map', 'de.nrw.schule.svws.core.adt.map.AVLMap', 'java.util.NavigableMap', 'java.util.SortedMap'].includes(name);
  726. }
  727. }
  728. export function cast_de_nrw_schule_svws_core_adt_map_AVLMap<K, V>(obj : unknown) : AVLMap<K, V> {
  729. return obj as AVLMap<K, V>;
  730. }