-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
interface.ts
95 lines (87 loc) · 3.56 KB
/
interface.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
import { HashMap } from '../../../../hashed/map/index.ts';
import { MultiSetBase, MultiSetContext } from '../../../../multiset/custom/index.ts';
import type { Stream, Streamable } from '../../../../stream/mod.ts';
import type { HashMultiSetCreators } from '../../../../multiset/custom/index.ts';
/**
* A type-invariant immutable MultiSet of value type T.
* In the MultiSet, each value can occur multiple times.
* See the [MultiSet documentation](https://rimbu.org/docs/collections/multiset) and the [HashMultiSet API documentation](https://rimbu.org/api/rimbu/multiset/HashMultiSet/interface)
* @typeparam T - the value type
* @note
* - The `HashMultiSet` uses the contexts' `HashMap` `mapContext` to hash
* the values.
* @example
* ```ts
* const m1 = HashMultiSet.empty<string>()
* const m2 = HashMultiSet.of('a', 'b', 'a', 'c')
* ```
*/
export interface HashMultiSet<T> extends MultiSetBase<T, HashMultiSet.Types> {}
export namespace HashMultiSet {
/**
* A type-invariant immutable MultiSet of value type T.
* In the MultiSet, each value can occur multiple times.
* See the [MultiSet documentation](https://rimbu.org/docs/collections/multiset) and the [HashMultiSet API documentation](https://rimbu.org/api/rimbu/multiset/HashMultiSet/interface)
* @typeparam T - the value type
* @note
* - The `HashMultiSet` uses the contexts' `HashMap` `mapContext` to hash
* the values.
* @example
* ```ts
* const m1 = HashMultiSet.empty<string>()
* const m2 = HashMultiSet.of('a', 'b', 'a', 'c')
* ```
*/
export interface NonEmpty<T>
extends MultiSetBase.NonEmpty<T, HashMultiSet.Types>,
Omit<HashMultiSet<T>, keyof MultiSetBase.NonEmpty<any, any>>,
Streamable.NonEmpty<T> {
stream(): Stream.NonEmpty<T>;
}
/**
* A context instance for an `HashMultiSet` that acts as a factory for every instance of this
* type of collection.
* @typeparam UT - the upper value type bound for which the context can be used
*/
export interface Context<UT>
extends MultiSetBase.Context<UT, HashMultiSet.Types> {
readonly typeTag: 'HashMultiSet';
}
/**
* A mutable `HashMultiSet` builder used to efficiently create new immutable instances.
* See the [MultiSet documentation](https://rimbu.org/docs/collections/multiset) and the [HashMultiSet.Builder API documentation](https://rimbu.org/api/rimbu/multiset/HashMultiSet/Builder/interface)
* @typeparam T - the value type
*/
export interface Builder<T>
extends MultiSetBase.Builder<T, HashMultiSet.Types> {}
/**
* Utility interface that provides higher-kinded types for this collection.
*/
export interface Types extends MultiSetBase.Types {
readonly normal: HashMultiSet<this['_T']>;
readonly nonEmpty: HashMultiSet.NonEmpty<this['_T']>;
readonly context: HashMultiSet.Context<this['_T']>;
readonly builder: HashMultiSet.Builder<this['_T']>;
readonly countMap: HashMap<this['_T'], number>;
readonly countMapNonEmpty: HashMap.NonEmpty<this['_T'], number>;
readonly countMapContext: HashMap.Context<this['_T']>;
}
}
function createContext<UT>(options?: {
countMapContext?: HashMap.Context<UT>;
}): HashMultiSet.Context<UT> {
return Object.freeze(
new MultiSetContext<UT, 'HashMultiSet', any>(
'HashMultiSet',
options?.countMapContext ?? HashMap.defaultContext()
)
);
}
const _defaultContext: HashMultiSet.Context<any> = createContext();
export const HashMultiSet: HashMultiSetCreators = Object.freeze({
..._defaultContext,
createContext,
defaultContext<UT>(): HashMultiSet.Context<UT> {
return _defaultContext;
},
});