summaryrefslogtreecommitdiffstats
path: root/cid
diff options
context:
space:
mode:
authorJakob Borg <jakob@nym.se>2014-03-01 11:11:37 +0100
committerJakob Borg <jakob@nym.se>2014-03-01 11:11:37 +0100
commit51788d6f0ef0eed7614f0ca2b657fff087d8e4c2 (patch)
tree4edee6d96f0f98e2e4b31fa0f65a24244b1e92bd /cid
parentea0bed22382888584a9dcf1db4c89d2609253701 (diff)
Add some support packages
Diffstat (limited to 'cid')
-rw-r--r--cid/cid.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/cid/cid.go b/cid/cid.go
new file mode 100644
index 0000000000..1fd343cc60
--- /dev/null
+++ b/cid/cid.go
@@ -0,0 +1,42 @@
+package cid
+
+type Map struct {
+ toCid map[string]int
+ toName []string
+}
+
+func NewMap() *Map {
+ return &Map{
+ toCid: make(map[string]int),
+ }
+}
+
+func (m *Map) Get(name string) int {
+ cid, ok := m.toCid[name]
+ if ok {
+ return cid
+ }
+
+ // Find a free slot to get a new ID
+ for i, n := range m.toName {
+ if n == "" {
+ m.toName[i] = name
+ m.toCid[name] = i
+ return i
+ }
+ }
+
+ // Add it to the end since we didn't find a free slot
+ m.toName = append(m.toName, name)
+ cid = len(m.toName) - 1
+ m.toCid[name] = cid
+ return cid
+}
+
+func (m *Map) Clear(name string) {
+ cid, ok := m.toCid[name]
+ if ok {
+ m.toName[cid] = ""
+ delete(m.toCid, name)
+ }
+}