summaryrefslogtreecommitdiff
path: root/box
diff options
context:
space:
mode:
authorKai Gunger <kgunger12@gmail.com>2026-11-15 04:23:39 -0500
committerKai Gunger <kgunger12@gmail.com>2026-11-15 04:23:39 -0500
commit3e09a6ae3aea3b90961e4216339368775ea7c842 (patch)
tree181277b38e19e04d24868e29ca58a9002e1ec284 /box
parent11a6fb0fed699a4028bcd454c0f175ca01607a68 (diff)
cstring Map to void pointer
Diffstat (limited to 'box')
-rw-r--r--box/box.tnsl2
-rw-r--r--box/list.tnsl16
-rw-r--r--box/map.tnsl185
-rw-r--r--box/optional.tnsl35
4 files changed, 230 insertions, 8 deletions
diff --git a/box/box.tnsl b/box/box.tnsl
index 189392f..ab934b8 100644
--- a/box/box.tnsl
+++ b/box/box.tnsl
@@ -1,5 +1,7 @@
/; module box
import "list.tnsl"
+ import "map.tnsl"
+ import "optional.tnsl"
;/
diff --git a/box/list.tnsl b/box/list.tnsl
index 58278e4..14ac1b8 100644
--- a/box/list.tnsl
+++ b/box/list.tnsl
@@ -9,14 +9,14 @@ struct List {
_elsz
}
-uint VECTOR_MIN_ELEMENTS = 4
-uint VECTOR_MAX_GROW = 256
+uint LIST_MIN_ELEMENTS = 4
+uint LIST_MAX_GROW = 256
-/; method Vector
+/; method List
/; init (uint elsz)
self._elsz = elsz
- self.size = VECTOR_MIN_ELEMENTS
+ self.size = LIST_MIN_ELEMENTS
self.count = 0
self.data = _alloc(self.size * self._elsz)
;/
@@ -41,10 +41,10 @@ uint VECTOR_MAX_GROW = 256
/; push (~void el)
/; if (self.size == self.count + 1)
- /; if (self.size < VECTOR_MAX_GROW)
+ /; if (self.size < LIST_MAX_GROW)
self._grow(self.size)
;; else
- self._grow(VECTOR_MAX_GROW)
+ self._grow(LIST_MAX_GROW)
;/
;/
@@ -127,8 +127,8 @@ uint VECTOR_MAX_GROW = 256
_delete(self.data)
;/
- /; copy [Vector]
- Vector out
+ /; copy [List]
+ List out
out.init(self._elsz)
/; loop (int i = 0; i < self.count) [i++]
diff --git a/box/map.tnsl b/box/map.tnsl
new file mode 100644
index 0000000..43d52a3
--- /dev/null
+++ b/box/map.tnsl
@@ -0,0 +1,185 @@
+
+# Currently just a hash map from cstring to an arbitrary pointer
+# but looking to expand this in the future with generics
+
+struct MapNode {
+ ~void
+ key,
+ value,
+
+ bool active
+}
+
+/; method MapNode
+ /; deleted [bool]
+ return self.active == false && self.key !== null
+ ;/
+;/
+
+struct Map {
+ uint
+ count,
+ size,
+
+ ~MapNode _under
+}
+
+uint MAP_MIN_SIZE = 8
+uint MAP_MAX_GROW = 4096
+
+/; method Map
+ /; _alloc
+ MapNode tmp
+ tmp.key = null
+ tmp.value = null
+ tmp.active = false
+
+ self._under = _alloc(self.size * len tmp)
+
+ /; loop (int i = 0; i < self.size) [i++]
+ self._under{i} = tmp
+ ;/
+ ;/
+
+ /; init
+ self.count = 0
+ self.size = MAP_MIN_SIZE
+
+ self._alloc()
+ ;/
+
+ /; _over [bool]
+ uint usage = (self.count * 10) / self.size
+ return usage > 7
+ ;/
+
+ /; _grow
+ ~MapNode old_list = self._under
+ uint old_size = self.size
+
+ self.count = 0
+ /; if (self.size < MAP_MAX_GROW / 2)
+ self.size = old_size * 2
+ ;; else
+ self.size = old_size + MAP_MAX_GROW
+ ;/
+ self._alloc()
+
+ /; loop (int i = 0; i < old_size) [i++]
+ MapNode to_copy = old_list{i}
+ /; if (to_copy.active)
+ uint idx = self._index(to_copy.key)
+ self._under{idx} = to_copy
+ ;; else if (to_copy.key !== null)
+ _delete(to_copy.key)
+ ;/
+ ;/
+
+ _delete(old_list)
+ ;/
+
+ /; _hash (~uint8 key, uint64 k0) [uint64]
+ crypto.hash.SipHashKey sk
+ sk.k0 = k0
+ sk.k1 = k0
+
+ uint in_len = text.cstr.length(key)
+
+ uint64 out = 0
+
+ crypto.hash.siphash(key, in_len, ~sk, ~out, 8)
+ return out % self.size
+ ;/
+
+ /; _index (~uint8 key) [int]
+ # Default hash
+ uint h = self._hash(key, 0)
+ int first_free = -1
+
+ /; loop (int i = 0; i < self.size) [i++]
+ uint try = h + (i + i * i) / 2
+ try = try % self.size
+
+ MapNode node = self._under{try}
+
+ /; if (node.key == null)
+ /; if (first_free < 0)
+ return try
+ ;/
+ return first_free
+ ;; else if (node.deleted() == true && first_free < 0)
+ first_free = try
+ ;; else if (node.key !== null)
+ /; if (text.cstr.eq(node.key, key))
+ return try
+ ;/
+ ;/
+ ;/
+
+ return first_free
+ ;/
+
+ /; set (~uint8 key, ~void value)
+ /; if (self._over())
+ self._grow()
+ ;/
+
+ uint idx = self._index(key)
+ ~MapNode n = ~self._under{idx}
+
+ /; if (n`.active == false)
+ self.count++
+ ;; else
+ n`.value = value
+ return
+ ;/
+
+ /; if (n`.key !== null)
+ _delete(n`.key)
+ ;/
+
+ n`.key = text.cstr.copy(key)
+ n`.value = value
+ n`.active = true
+ ;/
+
+ /; get (~uint8 key) [~void]
+ uint idx = self._index(key)
+
+ MapNode n = self._under{idx}
+
+ /; if (n.key == null || n.active == false)
+ return null
+ ;/
+
+ return n.value
+ ;/
+
+ /; del (~uint8 key)
+ uint idx = self._index(key)
+ MapNode n = self._under{idx}
+
+ /; if (n.active == true)
+ return
+ ;/
+
+ n.active = false
+ self._under{idx} = n
+ self.count--
+ ;/
+
+ /; end
+ /; loop (int i = 0; i < self.size) [i++]
+ MapNode n = self._under{i}
+ /; if (n.key !== null)
+ _delete(n.key)
+ ;/
+ ;/
+
+ _delete(self._under)
+ self._under = null
+ self.size = 0
+ self.count = 0
+ ;/
+;/
+
diff --git a/box/optional.tnsl b/box/optional.tnsl
new file mode 100644
index 0000000..558bc7d
--- /dev/null
+++ b/box/optional.tnsl
@@ -0,0 +1,35 @@
+
+struct Maybe {
+ ~void obj
+}
+
+/; method Maybe
+ /; ok [bool]
+ return self.obj !== NULL
+ ;/
+;/
+
+struct Error {
+ ~void trace,
+ ~uint8 msg
+}
+
+/; method Error
+
+ /; print
+ _print("ERROR: \0")
+ _print(self.msg)
+ _print("\n\0")
+ ;/
+;/
+
+struct Result {
+ Maybe obj,
+ Error err
+}
+
+/; method Result
+ /; ok [bool]
+ return self.obj.ok()
+ ;/
+;/