interface OrderedHashSet<T>
A type-invariant immutable Ordered HashSet of value type T. In the Set, there are no duplicate values. See the Set documentation and the OrderedHashSet API documentation
Companion namespace: OrderedHashSet
Extends: OrderedSetBase<T,Tp>
Implemented by: OrderedHashSet.NonEmpty<T>
Type parameters
Name | Description |
---|---|
T | the value type |
- The OrderedHashSet keeps the insertion order of values, thus iterators and stream will also reflect this order. - The OrderedHashSet wraps around a HashSet instance, thus has the same time complexity as the HashSet. - The OrderedHashSet keeps the key insertion order in a List, thus its space complexity is higher than a regular HashSet.
const s1 = OrderedHashSet.empty<string>()
const s2 = OrderedHashSet.of('a', 'b', 'c')
Properties
context
Returns the context
associated to this collection instance.
context
context
associated to this collection instance.isEmpty
Returns true if the collection is empty.
isEmpty
order
Returns a List
instance containing the order of the elements.
order
List
instance containing the order of the elements.size
Returns the number of values in the collection.
size
sourceSet
Returns the contained Set
instance.
sourceSet
Set
instance.Methods
[Symbol.iterator]
Returns a FastIterator
instance used to iterate over the values of this Iterable
.
[Symbol.iterator]
FastIterator
instance used to iterate over the values of this Iterable
.add
Returns the collection with given value
added.
add
value
added.addAll
Returns the collection with the values in given values
StreamSource
added.
addAll
values
StreamSource
added.Definitions
addAll(values:
StreamSource.NonEmpty
<T>):
WithElem
<Tp, T>['nonEmpty'];
addAll(values:
StreamSource
<T>):
WithElem
<Tp, T>['normal'];
Parameters
Name | Type | Description |
---|---|---|
values | StreamSource.NonEmpty <T> | a StreamSource containing values to add |
HashSet.of(1, 2, 3).addAll(10, 11).toArray() // => [1, 2, 3, 10, 11]
Overrides
assumeNonEmpty
Returns the same collection typed as non-empty.
assumeNonEmpty
Definition
assumeNonEmpty():
WithElem
<Tp, T>['nonEmpty'];
RimbuError.EmptyCollectionAssumedNonEmptyError
if the collection is empty
HashSet.empty().assumeNonEmpty() // => throws RimbuError.EmptyCollectionAssumedNonEmptyError
HashSet.from([[0, 1]]).assumeNonEmpty() // => List.NonEmpty(0, 1, 2)
Overrides
difference
Returns a collection where each value of given other
StreamSource
is removed from this collection.
difference
other
StreamSource
is removed from this collection.Definition
difference<U = T>(other:
StreamSource
<
RelatedTo
<T, U>>):
WithElem
<Tp, T>['normal'];
Type parameters
Name | Default | Description |
---|---|---|
U | T |
Parameters
Name | Type | Description |
---|---|---|
other | StreamSource < RelatedTo <T, U>> | a StreamSource containing values |
HashSet.of(1, 2, 3).difference(HashSet.of(1, 3)).toArray() // => [2]
Overrides
filter
Returns a collection containing only those entries that satisfy given pred
predicate.
filter
pred
predicate.Definition
filter(pred: (value: T, index: number, halt: () => void) => boolean):
WithElem
<Tp, T>['normal'];
Parameters
Name | Type | Description |
---|---|---|
pred | (value: T, index: number, halt: () => void) => boolean | a predicate function receiving: - value : the next value- index : the entry index- halt : a function that, when called, ensures no next elements are passed |
HashSet.of(1, 2, 3).filter(value < 3).toArray()
// => [1, 2]
Overrides
forEach
Performs given function f
for each value of the collection, using given state
as initial traversal state.
forEach
f
for each value of the collection, using given state
as initial traversal state.Definition
forEach(f: (value: T, index: number, halt: () => void) => void, state?:
TraverseState
): void;
Parameters
Name | Type | Description |
---|---|---|
f | (value: T, index: number, halt: () => void) => void | the function to perform for each element, receiving: - value : the next element- index : the index of the element- halt : a function that, if called, ensures that no new elements are passed |
state | TraverseState | (optional) the traverse state |
HashSet.of(1, 2, 3).forEach((value, i, halt) => {
console.log([value, i]);
if (i >= 1) halt();
})
// => logs [1, 0] [2, 1]
O(N)
Overrides
has
Returns true if given value
is in the collection.
has
value
is in the collection.intersect
Returns a collection containing values that are both in this collection, and in the given other
StreamSource
.
intersect
other
StreamSource
.Definition
intersect<U = T>(other:
StreamSource
<
RelatedTo
<T, U>>):
WithElem
<Tp, T>['normal'];
Type parameters
Name | Default | Description |
---|---|---|
U | T |
Parameters
Name | Type | Description |
---|---|---|
other | StreamSource < RelatedTo <T, U>> | a StreamSource containing values |
HashSet.of(1, 2, 3).interface(HashSet.of(1, 3)).toArray() // => [1, 3]
Overrides
nonEmpty
Returns true if there is at least one entry in the collection, and instructs the compiler to treat the collection as a .NonEmpty type.
nonEmpty
Definition
nonEmpty(): this is
WithElem
<Tp, T>['nonEmpty'];
const m: HashSet<number> = HashSet.of(1, 2, 2)
m.stream().first(0) // compiler allows fallback value since the Stream may be empty
if (m.nonEmpty()) {
m.stream().first(0) // compiler error: fallback value not allowed since Stream is not empty
}
Overrides
remove
Returns the collection with given value
removed.
remove
value
removed.Definition
remove<U = T>(value:
RelatedTo
<T, U>):
WithElem
<Tp, T>['normal'];
Type parameters
Name | Default | Description |
---|---|---|
U | T |
Parameters
Name | Type | Description |
---|---|---|
value | RelatedTo <T, U> | the value to remove |
const s = HashSet.of(1, 2, 3)
s.remove(2).toArray() // => [1, 3]
s.remove(10).toArray() // => [1, 2, 3]
Overrides
removeAll
Returns the collection with all values in the given values
StreamSource
removed.
removeAll
values
StreamSource
removed.Definition
removeAll<U = T>(values:
StreamSource
<
RelatedTo
<T, U>>):
WithElem
<Tp, T>['normal'];
Type parameters
Name | Default | Description |
---|---|---|
U | T |
Parameters
Name | Type | Description |
---|---|---|
values | StreamSource < RelatedTo <T, U>> | a StreamSource containing values to remove |
HashSet.of(1, 2, 3).removeAll([1, 3]).toArray()
// => [2]
Overrides
stream
Returns a Stream containing all elements of this collection.
stream
symDifference
Returns a collection of the values that are either in this collection or in the other
StreamSource
, but not in both.
symDifference
other
StreamSource
, but not in both.Definition
symDifference(other:
StreamSource
<T>):
WithElem
<Tp, T>['normal'];
Parameters
Name | Type | Description |
---|---|---|
other | StreamSource <T> | a StreamSource containing values |
HashSet.of(1, 2, 3).symDifference([2, 4]).toArray()
// => [1, 3, 4]
Overrides
toArray
Returns an array containing all values in this collection.
toArray
toBuilder
Returns a builder object containing the values of this collection.
toBuilder
toJSON
Returns a JSON representation of this collection.
toJSON
toString
Returns a string representation of this collection.
toString
union
Returns a collection containing all values from this collection and all values of given other
StreamSource
.
union
other
StreamSource
.Definitions
union(other:
StreamSource.NonEmpty
<T>):
WithElem
<Tp, T>['nonEmpty'];
union(other:
StreamSource
<T>):
WithElem
<Tp, T>['normal'];
Parameters
Name | Type | Description |
---|---|---|
other | StreamSource.NonEmpty <T> | a StreamSource containing values |
HashSet.of(1, 2, 3).union(HashSet.of(2, 4, 6)).toArray()
// => [1, 2, 3, 4, 6]