Skip to content

Commit 8b3ff9e

Browse files
committed
Add a contains() method. Fixes #1
1 parent 33c149b commit 8b3ff9e

12 files changed

+72
-4
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ As an illustration, let's build a simple sorted set out of an `Array`:
1717
| Find | `set.sort(); var index = set.indexOf(value);` |
1818
| Previous | `var previousIndex = index - 1;` |
1919
| Next | `var nextIndex = index + 1;` |
20+
| Test | `var isInSet = set.indexOf(value) != -1;` |
2021

2122
... this works, but it's a bit cryptic and some operations--notably iterate--
2223
will be very slow with large sets.
@@ -37,6 +38,7 @@ Then write code like this:
3738
set.insert(3);
3839
set.insert(2);
3940
set.remove(3);
41+
var yes = set.contains(2);
4042
console.log(set.map(function(x) { return x * 2; })); // returns [ 20, 4 ]
4143
});
4244

@@ -48,6 +50,7 @@ If you don't like RequireJS, you can download the standalone version,
4850
set.insert(3);
4951
set.insert(2);
5052
set.remove(3);
53+
var yes = set.contains(2);
5154
console.log(set.map(function(x) { return x * 2; })); // returns [ 20, 4 ]
5255

5356
Operations
@@ -60,6 +63,7 @@ The SortedSet API:
6063
| Create | `var set = new SortedSet();` |
6164
| Insert | `set.insert(value);` |
6265
| Remove | `set.remove(value);` |
66+
| Test | `set.contains(value);` | Returns `true` or `false` |
6367
| Iterate | `set.forEach(doSomething);` | Plus `set.map()` and other [iterative methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript/1.6#Array_extras), returning `Array`s and scalars |
6468

6569
Find, Previous and Next work with an Iterator pattern. An iterator is an
@@ -163,7 +167,7 @@ You'll see running times like this:
163167
| Insert | O(n) (often slow) | O(n) (often slow) | O(lg n) (fast) |
164168
| Remove | O(n) (often slow) | O(n) (often slow) | O(lg n) (fast) |
165169
| Iterate | O(n) (fast) | O(n) (slowest) | O(n) (slower than Array) |
166-
| Find | O(lg n) (fastest) | O(n) (slowest) | O(lg n) (slower than Array) |
170+
| Find, Test | O(lg n) (fastest) | O(n) (slowest) | O(lg n) (slower than Array) |
167171

168172
According to some simple [jsPerf
169173
tests](http://jsperf.com/js-sorted-set-insert-remove), you should use

coffee/SortedSet/AbstractBinaryTreeStrategy.coffee

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ define [ './BinaryTreeIterator' ], (BinaryTreeIterator) ->
2121
i += 1
2222
undefined
2323

24+
contains: (value) ->
25+
comparator = @comparator
26+
27+
node = @root
28+
while node isnt null
29+
cmp = comparator(value, node.value)
30+
if cmp == 0
31+
break
32+
else if cmp < 0
33+
node = node.left
34+
else
35+
node = node.right
36+
37+
node isnt null && node.value == value
38+
2439
findIterator: (value) -> BinaryTreeIterator.find(this, value, @comparator)
2540
beginIterator: -> BinaryTreeIterator.left(this)
2641
endIterator: -> BinaryTreeIterator.right(this)

coffee/SortedSet/AbstractSortedSet.coffee

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ define ->
1313
@priv.remove(value)
1414
this
1515

16+
contains: (value) ->
17+
@priv.contains(value)
18+
1619
# Returns this set as an Array
1720
toArray: -> @priv.toArray()
1821

coffee/SortedSet/ArrayStrategy.coffee

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ define ->
5050
throw 'Value not in set' if @data[index] != value
5151
@data.splice(index, 1)
5252

53+
contains: (value) ->
54+
@data.indexOf(value) != -1
55+
5356
forEachImpl: (callback, sortedSet, thisArg) ->
5457
for value, index in @data
5558
callback.call(thisArg, value, index, sortedSet)

js/SortedSet/AbstractBinaryTreeStrategy.js

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/SortedSet/AbstractBinaryTreeStrategy.js.map

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

js/SortedSet/AbstractSortedSet.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/SortedSet/AbstractSortedSet.js.map

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

js/SortedSet/ArrayStrategy.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/SortedSet/ArrayStrategy.js.map

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)