aboutsummaryrefslogtreecommitdiffstats
path: root/sw/cal/table.go
blob: 20d21d0d8c11c75d2adceb5ce59956ae1f864f66 (plain) (blame)
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
package main

import (
	"fmt"
	"slices"
)

const tabRows = 32

type Table struct {
	keys []int32
	vals []uint16
}

func (t *Table) Insert(key int32, val uint16) error {
	if len(t.keys) >= tabRows {
		return fmt.Errorf("too many rows")
	}

	i, ok := slices.BinarySearch(t.keys, key)
	if ok {
		return ErrDupKey{key}
	}
	t.keys = slices.Insert(t.keys, i, key)
	t.vals = slices.Insert(t.vals, i, val)

	return nil
}