-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathcollections.go
114 lines (98 loc) · 2.77 KB
/
collections.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package collections
type (
Collection[T any] interface {
ForEach(callback func(T) bool)
Size() int
}
// A Deque is a double-ended queue.
Deque[T any] interface {
Clear()
PeekBack() (value T, ok bool)
PeekFront() (value T, ok bool)
PopBack() (value T, ok bool)
PopFront() (value T, ok bool)
PushBack(value T)
PushFront(value T)
Size() int
}
// A Dictionary is a collection of key value pairs.
Dictionary[TKey, TValue any] interface {
Clear()
Delete(key TKey)
ForEach(callback func(Pair[TKey, TValue]) bool)
Get(key TKey) (value TValue, ok bool)
Keys() Collection[TKey]
Set(key TKey, value TValue)
Size() int
Values() Collection[TValue]
}
// A Queue is a collection that supports FIFO operations.
Queue[T any] interface {
Clear()
Peek() (value T, ok bool)
Pop() (value T, ok bool)
Push(value T)
Size() int
}
// A Set is a unique collection of values.
Set[T any] interface {
Add(value T)
Clear()
Delete(value T)
ForEach(callback func(T) bool)
Has(value T) bool
Size() int
}
// A Stack is a collection that supports LIFO operations.
Stack[T any] interface {
Clear()
Peek() (value T, ok bool)
Pop() (value T, ok bool)
Push(value T)
Size() int
}
)
// NewDeque creates a new Deque implemented using a slice as a ring buffer.
func NewDeque[T any]() Deque[T] {
return newDequeViaRingSlice[T]()
}
// NewDictionary creates a new Dictionary implemented using a map.
func NewDictionary[TKey comparable, TValue any]() Dictionary[TKey, TValue] {
return newDictionaryViaMap[TKey, TValue]()
}
// NewQueue creates a new Queue implemented using a slice.
func NewQueue[T any]() Queue[T] {
return newQueueViaSlice[T]()
}
// NewSet creates a new Set implemented using a map.
func NewSet[T comparable]() Set[T] {
return newSetViaMap[T]()
}
// NewSlice creates a new slice from a collection.
func NewSlice[T any](collection Collection[T]) []T {
arr := make([]T, 0, collection.Size())
collection.ForEach(func(item T) bool {
arr = append(arr, item)
return true
})
return arr
}
// NewSortedDictionary creates a new Dictionary implemented using a btree, providing sorted iteration by the less function.
func NewSortedDictionary[TKey, TValue any](less func(TKey, TKey) bool) Dictionary[TKey, TValue] {
return newDictionaryViaBTree[TKey, TValue](less)
}
// NewSortedSet creates a new Set implemented using a btree, providing sorted iteration by the less function.
func NewSortedSet[T any](less func(T, T) bool) Set[T] {
return newSetViaBTree(less)
}
// NewStack creates a new Stack implemented using a slice.
func NewStack[T any]() Stack[T] {
return newStackViaSlice[T]()
}
type integer interface {
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
~int | ~int8 | ~int16 | ~int32 | ~int64
}
func mod[T integer](x, y T) T {
return (x%y + y) % y
}