Skip to content

Commit 5db86cf

Browse files
committed
set higher and lower methods
1 parent 0797813 commit 5db86cf

19 files changed

+538
-43
lines changed

definitions/src/NavigableHash.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,12 @@ export declare class NavigableHashSet<K> implements NavigableSet<K> {
382382
*/
383383
ceiling(key: K): K;
384384
/**
385+
* Returns the least element in this set greater than the given element, or null if there is no such element.
386+
* @param {K} item to find ceiling node for
387+
* @return {K} the least element greater than the given element, or null if there is no such element
388+
*/
389+
higher(key: K): K;
390+
/**
385391
* Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. Unless otherwise specified by the implementing class, actions are performed in the order of iteration (if an iteration order is specified). Exceptions thrown by the action are relayed to the caller.
386392
* @param {Consumer} consumer - the action to be performed for each element
387393
*/
@@ -392,6 +398,12 @@ export declare class NavigableHashSet<K> implements NavigableSet<K> {
392398
*/
393399
first(): K;
394400
/**
401+
* Returns the greatest element in this set less than the given element, or null if there is no such element.
402+
* @param {K} item to find floor node for
403+
* @return {K} the greatest element less than the given element, or null if there is no such element
404+
*/
405+
lower(key: K): K;
406+
/**
395407
* Returns the last (highest) element currently in this set.
396408
* @return {K} the last (highest) element currently in this set, undefined if there are no elements in this set
397409
*/

definitions/src/NavigableSet.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,24 @@ export interface NavigableSet<K> extends JSet<K> {
3232
*/
3333
ceiling(item: K): K;
3434
/**
35+
* Returns the least element in this set greater than the given element, or null if there is no such element.
36+
* @param {K} item to find ceiling node for
37+
* @return {K} the least element greater than the given element, or null if there is no such element
38+
*/
39+
higher(item: K): K;
40+
/**
3541
* Returns the greatest element in this set less than or equal to the given element, or null if there is no such element.
3642
* @param {K} item to find floor node for
3743
* @return {K} the greatest element less than or equal to e, or null if there is no such element
3844
*/
3945
floor(item: K): K;
4046
/**
47+
* Returns the greatest element in this set less than the given element, or null if there is no such element.
48+
* @param {K} item to find floor node for
49+
* @return {K} the greatest element less than the given element, or null if there is no such element
50+
*/
51+
lower(item: K): K;
52+
/**
4153
* Returns the first (lowest) element currently in this set.
4254
* @return {K} the first (lowest) element currently in this set, null if there are no elements in this set
4355
*/

definitions/src/SkipList.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,12 +371,24 @@ export declare class SkipListSet<K> implements NavigableSet<K> {
371371
*/
372372
floor(key: K): K;
373373
/**
374+
* Returns the greatest element in this set less than the given element, or null if there is no such element.
375+
* @param {K} item to find floor node for
376+
* @return {K} the greatest element less than the given element, or null if there is no such element
377+
*/
378+
lower(key: K): K;
379+
/**
374380
* Returns the least element in this set greater than or equal to the given element, or null if there is no such element.
375381
* @param {K} item to find ceiling node for
376382
* @return {K} the least element greater than or equal to item, or null if there is no such element
377383
*/
378384
ceiling(key: K): K;
379385
/**
386+
* Returns the least element in this set greater than the given element, or null if there is no such element.
387+
* @param {K} item to find ceiling node for
388+
* @return {K} the least element greater than the given element, or null if there is no such element
389+
*/
390+
higher(key: K): K;
391+
/**
380392
* Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. Unless otherwise specified by the implementing class, actions are performed in the order of iteration (if an iteration order is specified). Exceptions thrown by the action are relayed to the caller.
381393
* @param {Consumer} consumer - the action to be performed for each element
382394
*/

dist/spec/TestNavigableSet.spec.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ describe("Test NavigableSet functionality", function () {
9999
testCeilingString(new TreeSet_1.TreeSet(Collections_1.Collections.getStringComparator()));
100100
testCeilingString(new SkipList_1.SkipListSet(Collections_1.Collections.getStringComparator()));
101101
});
102+
it("Test higher", function () {
103+
testHigherNumber(new NavigableHash_1.NavigableHashSet(Collections_1.Collections.getNumberComparator()));
104+
testHigherNumber(new TreeSet_1.TreeSet(Collections_1.Collections.getNumberComparator()));
105+
testHigherNumber(new SkipList_1.SkipListSet(Collections_1.Collections.getNumberComparator()));
106+
testHigherString(new NavigableHash_1.NavigableHashSet(Collections_1.Collections.getStringComparator()));
107+
testHigherString(new TreeSet_1.TreeSet(Collections_1.Collections.getStringComparator()));
108+
testHigherString(new SkipList_1.SkipListSet(Collections_1.Collections.getStringComparator()));
109+
});
102110
it("Test floor", function () {
103111
testFloorNumber(new NavigableHash_1.NavigableHashSet(Collections_1.Collections.getNumberComparator()));
104112
testFloorNumber(new TreeSet_1.TreeSet(Collections_1.Collections.getNumberComparator()));
@@ -107,6 +115,14 @@ describe("Test NavigableSet functionality", function () {
107115
testFloorString(new TreeSet_1.TreeSet(Collections_1.Collections.getStringComparator()));
108116
testFloorString(new SkipList_1.SkipListSet(Collections_1.Collections.getStringComparator()));
109117
});
118+
it("Test lower", function () {
119+
testLowerNumber(new NavigableHash_1.NavigableHashSet(Collections_1.Collections.getNumberComparator()));
120+
testLowerNumber(new TreeSet_1.TreeSet(Collections_1.Collections.getNumberComparator()));
121+
testLowerNumber(new SkipList_1.SkipListSet(Collections_1.Collections.getNumberComparator()));
122+
testLowerString(new NavigableHash_1.NavigableHashSet(Collections_1.Collections.getStringComparator()));
123+
testLowerString(new TreeSet_1.TreeSet(Collections_1.Collections.getStringComparator()));
124+
testLowerString(new SkipList_1.SkipListSet(Collections_1.Collections.getStringComparator()));
125+
});
110126
it("Test tostring", function () {
111127
testToString(new NavigableHash_1.NavigableHashSet(Collections_1.Collections.getStringComparator()));
112128
testToString(new TreeSet_1.TreeSet(Collections_1.Collections.getStringComparator()));
@@ -225,6 +241,18 @@ function testCeilingNumber(set) {
225241
expect(set.ceiling(1)).toEqual(100);
226242
expect(set.ceiling(99999)).toEqual(null);
227243
}
244+
function testHigherNumber(set) {
245+
expect(set.higher(456)).toEqual(null);
246+
expect(set.size()).toEqual(0);
247+
expect(set.isEmpty()).toEqual(true);
248+
addTestNumbers(set);
249+
expect(set.size()).toEqual(10);
250+
expect(set.isEmpty()).toEqual(false);
251+
expect(set.higher(456)).toEqual(500);
252+
expect(set.higher(600)).toEqual(700);
253+
expect(set.higher(1)).toEqual(100);
254+
expect(set.higher(99999)).toEqual(null);
255+
}
228256
function testCeilingString(set) {
229257
expect(set.ceiling("notfound")).toEqual(null);
230258
expect(set.size()).toEqual(0);
@@ -237,6 +265,18 @@ function testCeilingString(set) {
237265
expect(set.ceiling("aaaaa")).toEqual("eighth");
238266
expect(set.ceiling("zzzzz")).toEqual(null);
239267
}
268+
function testHigherString(set) {
269+
expect(set.higher("notfound")).toEqual(null);
270+
expect(set.size()).toEqual(0);
271+
expect(set.isEmpty()).toEqual(true);
272+
addTestStrings(set);
273+
expect(set.size()).toEqual(10);
274+
expect(set.isEmpty()).toEqual(false);
275+
expect(set.higher("notfound")).toEqual("second");
276+
expect(set.higher("first")).toEqual("fourth");
277+
expect(set.higher("aaaaa")).toEqual("eighth");
278+
expect(set.higher("zzzzz")).toEqual(null);
279+
}
240280
function testFloorNumber(set) {
241281
expect(set.floor(456)).toEqual(null);
242282
expect(set.size()).toEqual(0);
@@ -261,6 +301,30 @@ function testFloorString(set) {
261301
expect(set.floor("aaaaa")).toEqual(null);
262302
expect(set.floor("zzzzz")).toEqual("third");
263303
}
304+
function testLowerNumber(set) {
305+
expect(set.lower(456)).toEqual(null);
306+
expect(set.size()).toEqual(0);
307+
expect(set.isEmpty()).toEqual(true);
308+
addTestNumbers(set);
309+
expect(set.size()).toEqual(10);
310+
expect(set.isEmpty()).toEqual(false);
311+
expect(set.lower(456)).toEqual(400);
312+
expect(set.lower(600)).toEqual(500);
313+
expect(set.lower(1)).toEqual(null);
314+
expect(set.lower(99999)).toEqual(1000);
315+
}
316+
function testLowerString(set) {
317+
expect(set.lower("notfound")).toEqual(null);
318+
expect(set.size()).toEqual(0);
319+
expect(set.isEmpty()).toEqual(true);
320+
addTestStrings(set);
321+
expect(set.size()).toEqual(10);
322+
expect(set.isEmpty()).toEqual(false);
323+
expect(set.lower("notfound")).toEqual("ninth");
324+
expect(set.lower("first")).toEqual("fifth");
325+
expect(set.lower("aaaaa")).toEqual(null);
326+
expect(set.lower("zzzzz")).toEqual("third");
327+
}
264328
function testToString(set) {
265329
jasts_1.TestString.equals("Empty set should stringify to []", JSON.stringify(set), '[]');
266330
expect(set.size()).toEqual(0);

dist/src/NavigableHash.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,18 @@ var NavigableHashSet = /** @class */ (function () {
12541254
return node.getKey();
12551255
};
12561256
/**
1257+
* Returns the least element in this set greater than the given element, or null if there is no such element.
1258+
* @param {K} item to find ceiling node for
1259+
* @return {K} the least element greater than the given element, or null if there is no such element
1260+
*/
1261+
NavigableHashSet.prototype.higher = function (key) {
1262+
var node = this.impl.higherEntry(key);
1263+
if ((node === undefined) || (node === null)) {
1264+
return null;
1265+
}
1266+
return node.getKey();
1267+
};
1268+
/**
12571269
* Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. Unless otherwise specified by the implementing class, actions are performed in the order of iteration (if an iteration order is specified). Exceptions thrown by the action are relayed to the caller.
12581270
* @param {Consumer} consumer - the action to be performed for each element
12591271
*/
@@ -1275,6 +1287,18 @@ var NavigableHashSet = /** @class */ (function () {
12751287
return node.getKey();
12761288
};
12771289
/**
1290+
* Returns the greatest element in this set less than the given element, or null if there is no such element.
1291+
* @param {K} item to find floor node for
1292+
* @return {K} the greatest element less than the given element, or null if there is no such element
1293+
*/
1294+
NavigableHashSet.prototype.lower = function (key) {
1295+
var node = this.impl.lowerEntry(key);
1296+
if ((node === undefined) || (node === null)) {
1297+
return null;
1298+
}
1299+
return node.getKey();
1300+
};
1301+
/**
12781302
* Returns the last (highest) element currently in this set.
12791303
* @return {K} the last (highest) element currently in this set, undefined if there are no elements in this set
12801304
*/

dist/src/SkipList.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,18 @@ var SkipListSet = /** @class */ (function () {
12341234
return node.getKey();
12351235
};
12361236
/**
1237+
* Returns the greatest element in this set less than the given element, or null if there is no such element.
1238+
* @param {K} item to find floor node for
1239+
* @return {K} the greatest element less than the given element, or null if there is no such element
1240+
*/
1241+
SkipListSet.prototype.lower = function (key) {
1242+
var node = this.impl.lowerEntry(key);
1243+
if ((node === undefined) || (node === null)) {
1244+
return null;
1245+
}
1246+
return node.getKey();
1247+
};
1248+
/**
12371249
* Returns the least element in this set greater than or equal to the given element, or null if there is no such element.
12381250
* @param {K} item to find ceiling node for
12391251
* @return {K} the least element greater than or equal to item, or null if there is no such element
@@ -1246,6 +1258,18 @@ var SkipListSet = /** @class */ (function () {
12461258
return node.getKey();
12471259
};
12481260
/**
1261+
* Returns the least element in this set greater than the given element, or null if there is no such element.
1262+
* @param {K} item to find ceiling node for
1263+
* @return {K} the least element greater than the given element, or null if there is no such element
1264+
*/
1265+
SkipListSet.prototype.higher = function (key) {
1266+
var node = this.impl.higherEntry(key);
1267+
if ((node === undefined) || (node === null)) {
1268+
return null;
1269+
}
1270+
return node.getKey();
1271+
};
1272+
/**
12491273
* Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. Unless otherwise specified by the implementing class, actions are performed in the order of iteration (if an iteration order is specified). Exceptions thrown by the action are relayed to the caller.
12501274
* @param {Consumer} consumer - the action to be performed for each element
12511275
*/

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)