diff options
Diffstat (limited to 'nexus/utils/collections')
| -rw-r--r-- | nexus/utils/collections/maps.go | 37 | ||||
| -rw-r--r-- | nexus/utils/collections/record.go | 3 |
2 files changed, 40 insertions, 0 deletions
diff --git a/nexus/utils/collections/maps.go b/nexus/utils/collections/maps.go new file mode 100644 index 0000000..4536515 --- /dev/null +++ b/nexus/utils/collections/maps.go @@ -0,0 +1,37 @@ +package collections
+
+type OrderedMap[K comparable, V any] struct {
+ keys []K
+ values map[K]V
+}
+
+func OrderedMapOf[K comparable, V any]() OrderedMap[K, V] {
+ return OrderedMap[K, V]{
+ keys: make([]K, 0),
+ values: make(map[K]V),
+ }
+}
+
+func (self *OrderedMap[K, V]) Set(key K, value V) {
+ if _, exists := self.values[key]; !exists {
+ self.keys = append(self.keys, key)
+ }
+ self.values[key] = value
+}
+
+func (self *OrderedMap[K, V]) Get(key K) (V, bool) {
+ value, exists := self.values[key]
+ return value, exists
+}
+
+func (self *OrderedMap[K, V]) All() []V {
+ result := make([]V, 0, len(self.keys))
+ for _, key := range self.keys {
+ result = append(result, self.values[key])
+ }
+ return result
+}
+
+func (self *OrderedMap[K, V]) Len() int {
+ return len(self.keys)
+}
diff --git a/nexus/utils/collections/record.go b/nexus/utils/collections/record.go new file mode 100644 index 0000000..18dbc2d --- /dev/null +++ b/nexus/utils/collections/record.go @@ -0,0 +1,3 @@ +package collections
+
+type Record[K comparable, V any] = map[K]V
|
