-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
83 lines (72 loc) · 2.51 KB
/
api.go
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
// Package mutable provides implementations of mutable containers.
package mutable
import (
"github.com/m4gshm/gollections/c"
"github.com/m4gshm/gollections/loop"
"github.com/m4gshm/gollections/map_"
"github.com/m4gshm/gollections/slice"
)
// NewSet instantiates set and copies elements to it
func NewSet[T comparable](elements ...T) *Set[T] {
uniques := make(map[T]struct{}, len(elements))
for _, e := range elements {
uniques[e] = struct{}{}
}
return WrapSet(uniques)
}
// NewSetCap creates a set with a predefined capacity
func NewSetCap[T comparable](capacity int) *Set[T] {
return WrapSet(make(map[T]struct{}, capacity))
}
// SetFromLoop creates a set with elements retrieved by the 'next' function.
// The next returns an element with true or zero value with false if there are no more elements.
func SetFromLoop[T comparable](next func() (T, bool)) *Set[T] {
if next == nil {
return nil
}
uniques := map[T]struct{}{}
for e, ok := next(); ok; e, ok = next() {
uniques[e] = struct{}{}
}
return WrapSet(uniques)
}
// NewMap instantiates an map using key/value pairs
func NewMap[K comparable, V any](elements ...c.KV[K, V]) *Map[K, V] {
return WrapMap(map_.Of(elements...))
}
// NewMapCap instantiates Map with a predefined capacity
func NewMapCap[K comparable, V any](capacity int) *Map[K, V] {
return WrapMap(make(map[K]V, capacity))
}
// NewMapOf instantiates Map populated by the 'elements' map key/values
func NewMapOf[K comparable, V any](elements map[K]V) *Map[K, V] {
uniques := make(map[K]V, len(elements))
for key, val := range elements {
uniques[key] = val
}
return WrapMap(uniques)
}
// MapFromLoop creates a map with elements retrieved converter the 'next' function
func MapFromLoop[K comparable, V any](next func() (K, V, bool)) *Map[K, V] {
if next == nil {
return nil
}
uniques := map[K]V{}
for key, val, ok := next(); ok; key, val, ok = next() {
uniques[key] = val
}
return WrapMap(uniques)
}
// NewVector instantiates Vector populated by the 'elements' slice
func NewVector[T any](elements ...T) *Vector[T] {
return WrapVector(slice.Clone(elements))
}
// NewVectorCap instantiates Vector with a predefined capacity
func NewVectorCap[T any](capacity int) *Vector[T] {
return WrapVector(make([]T, 0, capacity))
}
// VectorFromLoop creates a vector with elements retrieved by the 'next' function.
// The next returns an element with true or zero value with false if there are no more elements.
func VectorFromLoop[T any](next func() (T, bool)) *Vector[T] {
return WrapVector(loop.Slice(next))
}