/
dataTypes.go
149 lines (126 loc) · 2.86 KB
/
dataTypes.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package schematree
import (
"fmt"
"sort"
"strings"
"sync"
"sync/atomic"
)
// A struct capturing
// - a string IRI (str) and
// - its support, i.e. its total number of occurrences (totalCount)
// - an integer indicating sort order
type IItem struct {
Str *string
TotalCount uint64
SortOrder uint32
traversalPointer *SchemaNode // node traversal pointer
}
func (p *IItem) increment() {
atomic.AddUint64(&p.TotalCount, 1)
}
const typePrefix = "t#"
func (p *IItem) IsType() bool {
return strings.HasPrefix(*p.Str, typePrefix)
}
func (p *IItem) IsProp() bool {
return !strings.HasPrefix(*p.Str, typePrefix)
}
func (p IItem) String() string {
return fmt.Sprint(p.TotalCount, "x\t", *p.Str, " (", p.SortOrder, ")")
}
type propMap map[string]*IItem
var propMapLock sync.Mutex
// Note: get() is a mutator function. If no property is found with that iri, then it
// will build a new property, mutate the propMap to include it, and return the
// newly created property. The returned `item` is guaranteed to be non-null.
// thread-safe
func (m propMap) get(iri string) (item *IItem) { // TODO: Implement sameas Mapping/Resolution to single group identifier upon insert!
item, ok := m[iri]
if !ok {
propMapLock.Lock()
defer propMapLock.Unlock()
// recheck existence - might have been created by other thread
if item, ok = m[iri]; ok {
return
}
item = &IItem{&iri, 0, uint32(len(m)), nil}
m[iri] = item
}
return
}
func (p propMap) count() (int, int) {
props := 0
types := 0
for _, item := range p {
if item.IsType() {
types++
} else {
props++
}
}
return props, types
}
// An array of pointers to IRI structs
type IList []*IItem
// Sort the list according to the current iList Sort order
func (l IList) Sort() {
sort.Slice(l, func(i, j int) bool { return l[i].SortOrder < l[j].SortOrder })
}
// inplace sorting and deduplication.
func (l *IList) sortAndDeduplicate() {
ls := *l
ls.Sort()
// inplace deduplication
j := 0
for i := 1; i < len(ls); i++ {
if ls[j] == ls[i] {
continue
}
j++
ls[i], ls[j] = ls[j], ls[i]
}
*l = ls[:j+1]
}
func (l IList) toSet() map[*IItem]bool {
pSet := make(map[*IItem]bool, len(l))
for _, p := range l {
pSet[p] = true
}
return pSet
}
func (l IList) count() (int, int) {
props := 0
types := 0
for _, item := range l {
if item.IsType() {
types++
} else {
props++
}
}
return props, types
}
func (l IList) removeTypes() IList {
nl := IList{}
for _, item := range l {
if !item.IsType() {
nl = append(nl, item)
}
}
return nl
}
func (l IList) String() string {
//// list representation (includes duplicates)
o := "[ "
for i := 0; i < len(l); i++ {
o += *(l[i].Str) + " "
}
return o + "]"
//// couter presentation (loses order)
// ctr := make(map[string]int)
// for i := 0; i < len(p); i++ {
// ctr[p[i].str]++
// }
// return fmt.Sprint(ctr)
}