Skip to content

Commit 0e53cd2

Browse files
committed
Add asSet property (issue #14)
1 parent 1ef4f4a commit 0e53cd2

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

b+tree.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// B+ tree by David Piepgrass. License: MIT
2-
import { ISortedMap, ISortedMapF } from './interfaces';
2+
import { ISortedMap, ISortedMapF, ISortedSet } from './interfaces';
33

44
export {
55
ISetSource, ISetSink, ISet, ISetF, ISortedSetSource, ISortedSet, ISortedSetF,
@@ -1175,6 +1175,15 @@ export default class BTree<K=any, V=any> implements ISortedMapF<K,V>, ISortedMap
11751175
return this.hasOwnProperty('editRange');
11761176
}
11771177

1178+
/** A TypeScript helper function that returns `this as ISortedSet<K>` if this
1179+
* BTree implements it, which it does if `V extends undefined`. If `V` cannot
1180+
* be `undefined`, it returns `unknown` instead. Or at least, that was the
1181+
* intention, but TypeScript is acting weird and may return `ISortedSet<K>`
1182+
* even if `V` can't be `undefined` (discussion: btree-typescript issue #14) */
1183+
get asSet(): undefined extends V ? ISortedSet<K> : unknown {
1184+
return this as any;
1185+
}
1186+
11781187
/** Scans the tree for signs of serious bugs (e.g. this.size doesn't match
11791188
* number of elements, internal nodes not caching max element properly...)
11801189
* Computational complexity: O(number of nodes), i.e. O(size). This method

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sorted-btree",
3-
"version": "1.6.2",
3+
"version": "1.7.0",
44
"description": "A sorted list of key-value pairs in a fast, typed in-memory B+ tree with a powerful API.",
55
"main": "b+tree.js",
66
"typings": "b+tree",

readme.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Features
3636
with all keys in a given node.
3737
- Includes neat stuff such as `Range` methods for batch operations
3838
- Throws an exception if you try to use `NaN` as a key, but infinity is allowed.
39-
- No dependencies. 19.5K minified.
39+
- No dependencies. 19.8K minified.
4040
- Includes a lattice of interfaces for TypeScript users (see below)
4141
- Supports diffing computation between two trees that is highly optimized for the case
4242
in which a majority of nodes are shared (such as when persistent methods are used).
@@ -145,7 +145,7 @@ t.editRange(t.minKey(), t.maxKey(), true, (k, v) => {
145145
Interface lattice
146146
-----------------
147147

148-
BTree includes a lattice of interface types representing subsets of BTree's interface. I would encourage other authors of map/dictionary/tree/hashtable types to utilize these interfaces. These interfaces can be divided along three dimensions:
148+
BTree includes a [lattice of interface types](https://github.com/qwertie/btree-typescript/blob/master/interfaces.d.ts) representing subsets of BTree's interface. I would encourage other authors of map/dictionary/tree/hashtable types to utilize these interfaces. These interfaces can be divided along three dimensions:
149149

150150
### 1. Read/write access ###
151151

@@ -160,6 +160,8 @@ I have defined several kinds of interfaces along the read/write access dimension
160160

161161
The `Sorted` interfaces extend the non-sorted interfaces with queries that only a sorted collection can perform efficiently, such as `minKey()` and `nextHigherKey(k)`. At minimum, sorted interfaces add methods `minKey`, `maxKey`, `nextHigherKey`, `nextLowerKey`, and `forRange`, plus iterators that return keys/values/pairs in sorted order and accept a `firstKey` parameter to control the starting point of iteration.
162162

163+
**Note:** in sorted-btree ≤ v1.7.x, these interfaces have methods `nextHigherKey(key: K)` and `nextLowerKey(key: K)` which should be `nextHigherKey(key: K|undefined)` and `nextLowerKey(key: K|undefined)`. These signatures are changed in the next version.
164+
163165
### 3. Set versus map ###
164166

165167
A map is a collection of keys with values, while a set is a collection of keys without values.
@@ -168,7 +170,7 @@ For the most part, each `Set` interface is a subset of the corresponding `Map` i
168170

169171
### List of interfaces ###
170172

171-
All of these interfaces use `any` as the default type of `K` and `V`.
173+
All of these [interfaces](https://github.com/qwertie/btree-typescript/blob/master/interfaces.d.ts) use `any` as the default type of `K` and `V`.
172174

173175
- `ISetSource<K>`
174176
- `ISetSink<K>`
@@ -185,9 +187,9 @@ All of these interfaces use `any` as the default type of `K` and `V`.
185187
- `ISortedSetF<K> extends ISetF<K>, ISortedSetSource<K>`
186188
- `ISortedMapF<K,V> extends ISortedSetF<K>, IMapF<K,V>, ISortedMapSource<K,V>`
187189

188-
If the lattice were complete there would be 16 interfaces (4*2*2). In fact there are only 14 interfaces because `ISortedMapSink<K,V>` and `ISortedSetSink<K, V>` don't exist, because sorted sinks are indistinguishable from unsorted sinks.
190+
If the lattice were complete there would be 16 interfaces (`4*2*2`). In fact there are only 14 interfaces because `ISortedMapSink<K,V>` and `ISortedSetSink<K, V>` don't exist, because sorted sinks are indistinguishable from unsorted sinks.
189191

190-
`BTree<K,V>` implements all of these interfaces except `ISetSink<K>`, `ISet<K>`, and `ISortedSet<K>`.
192+
`BTree<K,V>` implements all of these interfaces except `ISetSink<K>`, `ISet<K>`, and `ISortedSet<K>`. However, `BTree<K,V>` may be _compatible_ with these interfaces even if TypeScript doesn't realize it. Therefore, if `V` includes `undefined`, the `BTree<K,V>.asSet` property is provided to cast the `BTree` to a set type. The `asSet` property returns the same `BTree` as type `ISortedSet<K>` (which is assignable to `ISetSink<K>`, `ISet<K>` and `ISortedSetSource<K>`).
191193

192194
### ES6 Map/Set compatibility ###
193195

0 commit comments

Comments
 (0)