Skip to content

Commit 0797813

Browse files
committed
TreeSet adding higher and lower methods
1 parent a8fd2bc commit 0797813

11 files changed

+270
-65
lines changed

definitions/spec/TestTreeSet.spec.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright Larry Diamond 2017 All Rights Reserved.
3+
* Copyright Larry Diamond 2019 All Rights Reserved.
44
*
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://github.com/larrydiamond/typescriptcollectionsframework/LICENSE

definitions/src/TreeSet.d.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright Larry Diamond 2018 All Rights Reserved.
3+
* Copyright Larry Diamond 2019 All Rights Reserved.
44
*
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://github.com/larrydiamond/typescriptcollectionsframework/blob/master/LICENSE
@@ -61,13 +61,25 @@ export declare class TreeSet<K> implements NavigableSet<K> {
6161
* @return {K} the greatest element less than or equal to e, or null if there is no such element
6262
*/
6363
floor(item: K): K;
64+
/**
65+
* Returns the highest key lower than the given key, or null if there is no such key.
66+
* @param {K} key the key
67+
* @return {K} the highest key lower than key, or null if there is no such key
68+
*/
69+
lower(item: K): K;
6470
/**
6571
* Returns the least element in this set greater than or equal to the given element, or null if there is no such element.
6672
* @param {K} item to find ceiling node for
6773
* @return {K} the least element greater than or equal to item, or null if there is no such element
6874
*/
6975
ceiling(item: K): K;
7076
/**
77+
* Returns the least key greater than the given key, or null if there is no such key.
78+
* @param {K} key the key
79+
* @return {K} the least key greater than key, or null if there is no such key
80+
*/
81+
higher(item: K): K;
82+
/**
7183
* Returns the first (lowest) element currently in this set.
7284
* @return {K} the first (lowest) element currently in this set, null if there are no elements in this set
7385
*/

dist/spec/TestTreeSet.spec.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22
/**
33
* @license
4-
* Copyright Larry Diamond 2017 All Rights Reserved.
4+
* Copyright Larry Diamond 2019 All Rights Reserved.
55
*
66
* Use of this source code is governed by an MIT-style license that can be
77
* found in the LICENSE file at https://github.com/larrydiamond/typescriptcollectionsframework/LICENSE
@@ -313,6 +313,46 @@ describe("Test TreeSet functionality", function () {
313313
expect(TreeSet2.ceiling(16)).toEqual(17); // 16 isnt there, 17 is
314314
expect(TreeSet2.ceiling(17)).toEqual(17); // 17 is there
315315
});
316+
it("Test higher", function () {
317+
var TreeSet2 = new TreeSet_1.TreeSet(Collections_1.Collections.getNumberComparator());
318+
expect(TreeSet2.add(44)).toEqual(true);
319+
expect(TreeSet2.add(5)).toEqual(true);
320+
expect(TreeSet2.add(20)).toEqual(true);
321+
expect(TreeSet2.add(88)).toEqual(true);
322+
expect(TreeSet2.add(50)).toEqual(true);
323+
expect(TreeSet2.add(30)).toEqual(true);
324+
expect(TreeSet2.add(1)).toEqual(true);
325+
expect(TreeSet2.add(48)).toEqual(true);
326+
expect(TreeSet2.add(62)).toEqual(true);
327+
expect(TreeSet2.add(78)).toEqual(true);
328+
expect(TreeSet2.add(17)).toEqual(true);
329+
expect(TreeSet2.add(70)).toEqual(true);
330+
expect(TreeSet2.add(80)).toEqual(true);
331+
expect(TreeSet2.add(32)).toEqual(true);
332+
expect(TreeSet2.higher(16)).toEqual(17); // 16 isnt there, 17 is
333+
expect(TreeSet2.higher(16)).toEqual(17); // 16 isnt there, 17 is
334+
expect(TreeSet2.higher(17)).toEqual(20); // 17 is there, 20 is next
335+
});
336+
it("Test lower", function () {
337+
var TreeSet2 = new TreeSet_1.TreeSet(Collections_1.Collections.getNumberComparator());
338+
expect(TreeSet2.add(44)).toEqual(true);
339+
expect(TreeSet2.add(5)).toEqual(true);
340+
expect(TreeSet2.add(20)).toEqual(true);
341+
expect(TreeSet2.add(88)).toEqual(true);
342+
expect(TreeSet2.add(50)).toEqual(true);
343+
expect(TreeSet2.add(30)).toEqual(true);
344+
expect(TreeSet2.add(1)).toEqual(true);
345+
expect(TreeSet2.add(48)).toEqual(true);
346+
expect(TreeSet2.add(62)).toEqual(true);
347+
expect(TreeSet2.add(78)).toEqual(true);
348+
expect(TreeSet2.add(17)).toEqual(true);
349+
expect(TreeSet2.add(70)).toEqual(true);
350+
expect(TreeSet2.add(80)).toEqual(true);
351+
expect(TreeSet2.add(32)).toEqual(true);
352+
expect(TreeSet2.lower(38)).toEqual(32); // 38 isnt there, 32 is next
353+
expect(TreeSet2.lower(31)).toEqual(30); // 31 isnt there, 30 is next
354+
expect(TreeSet2.lower(17)).toEqual(5); // 17 is there, 16 is next
355+
});
316356
it("Test lots", function () {
317357
var tset = new TreeSet_1.TreeSet(Collections_1.Collections.getStringComparator());
318358
for (var loop1 = 1; loop1 <= 26; loop1++) {

dist/src/TreeSet.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22
/**
33
* @license
4-
* Copyright Larry Diamond 2018 All Rights Reserved.
4+
* Copyright Larry Diamond 2019 All Rights Reserved.
55
*
66
* Use of this source code is governed by an MIT-style license that can be
77
* found in the LICENSE file at https://github.com/larrydiamond/typescriptcollectionsframework/blob/master/LICENSE
@@ -99,6 +99,17 @@ var TreeSet = /** @class */ (function () {
9999
return null;
100100
return tmp;
101101
};
102+
/**
103+
* Returns the highest key lower than the given key, or null if there is no such key.
104+
* @param {K} key the key
105+
* @return {K} the highest key lower than key, or null if there is no such key
106+
*/
107+
TreeSet.prototype.lower = function (item) {
108+
var tmp = this.datastore.lowerKey(item);
109+
if (tmp === undefined)
110+
return null;
111+
return tmp;
112+
};
102113
/**
103114
* Returns the least element in this set greater than or equal to the given element, or null if there is no such element.
104115
* @param {K} item to find ceiling node for
@@ -111,6 +122,17 @@ var TreeSet = /** @class */ (function () {
111122
return tmp;
112123
};
113124
/**
125+
* Returns the least key greater than the given key, or null if there is no such key.
126+
* @param {K} key the key
127+
* @return {K} the least key greater than key, or null if there is no such key
128+
*/
129+
TreeSet.prototype.higher = function (item) {
130+
var tmp = this.datastore.higherKey(item);
131+
if (tmp === undefined)
132+
return null;
133+
return tmp;
134+
};
135+
/**
114136
* Returns the first (lowest) element currently in this set.
115137
* @return {K} the first (lowest) element currently in this set, null if there are no elements in this set
116138
*/

docs/typedoc/assets/js/search.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)