-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
ordered-map.ts
103 lines (97 loc) · 4.14 KB
/
ordered-map.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import type { RMap } from '../../../collection-types/map/index.ts';
import { List } from '../../../list/mod.ts';
import {
OrderedMapBase,
OrderedMapContextImpl,
type OrderedMapCreators,
} from '../../../ordered/map-custom/index.ts';
import type { Stream, Streamable } from '../../../stream/mod.ts';
/**
* A type-invariant immutable Ordered Map of key type K, and value type V.
* In the Map, each key has exactly one value, and the Map cannot contain
* duplicate keys.
* See the [Map documentation](https://rimbu.org/docs/collections/map) and the [OrderedMap API documentation](https://rimbu.org/api/rimbu/ordered/map/OrderedMap/interface)
* @note
* - The OrderedMap keeps maintains the insertion order of elements, thus
* iterators and streams will also reflect this order.
* - The OrderedMap wraps around a Map instance, thus has mostly the same time complexity
* as the contained Map.
* - The OrderedMap keeps the key insertion order in a List, thus its space complexity
* is higher than a regular Map.
* @typeparam K - the key type
* @typeparam V - the value type
* @example
* ```ts
* const m1 = OrderedHashMap.empty<number, string>()
* const m2 = OrderedHashMap.of([1, 'a'], [2, 'b'])
* ```
*/
export interface OrderedMap<K, V>
extends OrderedMapBase<K, V, OrderedMap.Types> {}
export namespace OrderedMap {
/**
* A non-empty type-invariant immutable Ordered Map of key type K, and value type V.
* In the Map, each key has exactly one value, and the Map cannot contain
* duplicate keys.
* See the [Map documentation](https://rimbu.org/docs/collections/map) and the [OrderedMap API documentation](https://rimbu.org/api/rimbu/ordered/map/OrderedMap/interface)
* @note
* - The OrderedMap keeps maintains the insertion order of elements, thus
* iterators and streams will also reflect this order.
* - The OrderedMap wraps around a Map instance, thus has mostly the same time complexity
* as the contained Map.
* - The OrderedMap keeps the key insertion order in a List, thus its space complexity
* is higher than a regular Map.
* @typeparam K - the key type
* @typeparam V - the value type
* @example
* ```ts
* const m1 = OrderedHashMap.empty<number, string>()
* const m2 = OrderedHashMap.of([1, 'a'], [2, 'b'])
* ```
*/
export interface NonEmpty<K, V>
extends OrderedMapBase.NonEmpty<K, V, OrderedMap.Types>,
Omit<OrderedMap<K, V>, keyof OrderedMapBase.NonEmpty<any, any, any>>,
Streamable.NonEmpty<readonly [K, V]> {
stream(): Stream.NonEmpty<readonly [K, V]>;
}
/**
* A mutable `OrderedMap` builder used to efficiently create new immutable instances.
* See the [Map documentation](https://rimbu.org/docs/collections/map) and the [OrderedMap.Builder API documentation](https://rimbu.org/api/rimbu/ordered/map/OrderedMap/Builder/interface)
* @typeparam K - the key type
* @typeparam V - the value type
*/
export interface Builder<K, V>
extends OrderedMapBase.Builder<K, V, OrderedMap.Types> {}
/**
* A context instance for an `OrderedMap` that acts as a factory for every instance of this
* type of collection.
* @typeparam UK - the upper key type bound for which the context can be used
*/
export interface Context<UK> extends OrderedMapBase.Context<UK> {}
/**
* Utility interface that provides higher-kinded types for this collection.
*/
export interface Types extends OrderedMapBase.Types {
readonly normal: OrderedMap<this['_K'], this['_V']>;
readonly nonEmpty: OrderedMap.NonEmpty<this['_K'], this['_V']>;
readonly context: OrderedMap.Context<this['_K']>;
readonly builder: OrderedMap.Builder<this['_K'], this['_V']>;
readonly sourceContext: RMap.Context<this['_K']>;
readonly sourceMap: RMap<this['_K'], this['_V']>;
readonly sourceMapNonEmpty: RMap.NonEmpty<this['_K'], this['_V']>;
}
}
export const OrderedMap: OrderedMapCreators = {
createContext<UK>(options: {
listContext?: List.Context;
mapContext: RMap.Context<UK>;
}): OrderedMap.Context<UK> {
return Object.freeze(
new OrderedMapContextImpl<UK>(
options.listContext ?? List.defaultContext(),
options.mapContext
)
) as any;
},
};