summaryrefslogtreecommitdiff
path: root/search/elementary.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2023-04-02 20:22:13 +0300
committerPaul Buetow <paul@buetow.org>2023-04-02 20:22:13 +0300
commit0c6d4ed2e499e3e17165e43803d0d1c6dd0956d9 (patch)
tree6d6a5df53d1dd3e655d24f0423f24bc52ad9784c /search/elementary.go
parentf78ba2cdc6840dbc52a27a2f9fac28f3b61e8b7b (diff)
initial generics
Diffstat (limited to 'search/elementary.go')
-rw-r--r--search/elementary.go34
1 files changed, 19 insertions, 15 deletions
diff --git a/search/elementary.go b/search/elementary.go
index ed00159..abfc7de 100644
--- a/search/elementary.go
+++ b/search/elementary.go
@@ -1,31 +1,35 @@
package search
-type ElementaryElem struct {
- key int
- val int
- next *ElementaryElem
+import (
+ "codeberg.org/snonux/algorithms/ds"
+)
+
+type ElementaryElem[K, V ds.Number] struct {
+ key K
+ val V
+ next *ElementaryElem[K,V]
}
-type Elementary struct {
- root *ElementaryElem
+type Elementary[K,V ds.Number] struct {
+ root *ElementaryElem[K,V]
size int
}
-func NewElementary() *Elementary {
- return &Elementary{}
+func NewElementary[K, V ds.Number]() *Elementary[K,V] {
+ return &Elementary[K,V]{}
}
-func (s *Elementary) Empty() bool {
+func (s *Elementary[K,V]) Empty() bool {
return s.root == nil
}
-func (s *Elementary) Size() int {
+func (s *Elementary[K,V]) Size() int {
return s.size
}
-func (s *Elementary) Put(key int, val int) {
+func (s *Elementary[K,V]) Put(key K, val V) {
if s.Empty() {
- s.root = &ElementaryElem{key, val, nil}
+ s.root = &ElementaryElem[K,V]{key, val, nil}
s.size++
return
}
@@ -38,7 +42,7 @@ func (s *Elementary) Put(key int, val int) {
return
}
if elem.next == nil {
- elem.next = &ElementaryElem{key, val, nil}
+ elem.next = &ElementaryElem[K,V]{key, val, nil}
s.size++
return
}
@@ -46,7 +50,7 @@ func (s *Elementary) Put(key int, val int) {
}
}
-func (s *Elementary) Get(key int) (int, error) {
+func (s *Elementary[K,V]) Get(key K) (V, error) {
elem := s.root
for elem != nil {
@@ -59,7 +63,7 @@ func (s *Elementary) Get(key int) (int, error) {
return 0, NotFound
}
-func (s *Elementary) Del(key int) (int, error) {
+func (s *Elementary[K,V]) Del(key K) (V, error) {
if s.Empty() {
return 0, NotFound
}