-
Notifications
You must be signed in to change notification settings - Fork 0
/
registration.go
executable file
·68 lines (52 loc) · 1.94 KB
/
registration.go
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2011 The Misu Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package component
import "sort"
import "fmt"
type TypeSlice []Type
func (p TypeSlice) Len() int { return len(p) }
func (p TypeSlice) Less(i, j int) bool { return *p[i] < *p[j] }
func (p TypeSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
type RegistrationList struct {
registered_types_list []TypeSlice
}
func MakeRegistrationList(len, cap int) (r RegistrationList) {
r.registered_types_list = make([]TypeSlice, len, cap)
return
}
// returns the number of the RegistrationIDCount
func (t *RegistrationList) RegistrationIDCount() int { return len(t.registered_types_list) }
func (t *RegistrationList) AddNewRegistrationID() (id int) {
id = t.RegistrationIDCount()
t.registered_types_list = append(t.registered_types_list, nil)
return
}
func (t *RegistrationList) Register(id int, collection_types ...Type) {
if 0 > id || id >= t.RegistrationIDCount() {
panic(fmt.Sprintf("id (%d) out of bounds [0,%d)", id, t.RegistrationIDCount()))
}
// If there's no types, just bail
if len(collection_types) == 0 {
t.registered_types_list[id] = nil
return
}
// Sort for duplicate removal and fast access
sort.Sort(TypeSlice(collection_types))
// removes all duplicates
var wi int
for i := range collection_types {
if collection_types[i] == collection_types[wi] {
continue
}
wi++
collection_types[wi] = collection_types[i]
}
collection_types = collection_types[:wi+1]
// Store the slice for safety checks
t.registered_types_list[id] = TypeSlice(collection_types)
}
func (t *RegistrationList) Check(id int, collection_type Type) bool {
i := sort.Search(len(t.registered_types_list[id]), func(i int) bool { return *t.registered_types_list[id][i] >= *collection_type })
return i < len(t.registered_types_list[id]) && t.registered_types_list[id][i] == collection_type
}