]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
1 | // Licensed to the Apache Software Foundation (ASF) under one |
2 | // or more contributor license agreements. See the NOTICE file | |
3 | // distributed with this work for additional information | |
4 | // regarding copyright ownership. The ASF licenses this file | |
5 | // to you under the Apache License, Version 2.0 (the | |
6 | // "License"); you may not use this file except in compliance | |
7 | // with the License. You may obtain a copy of the License at | |
8 | // | |
9 | // http://www.apache.org/licenses/LICENSE-2.0 | |
10 | // | |
11 | // Unless required by applicable law or agreed to in writing, software | |
12 | // distributed under the License is distributed on an "AS IS" BASIS, | |
13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | // See the License for the specific language governing permissions and | |
15 | // limitations under the License. | |
16 | ||
17 | package encoding | |
18 | ||
19 | import ( | |
20 | "github.com/apache/arrow/go/v6/parquet" | |
21 | ) | |
22 | ||
23 | // standard map based implementation of memo tables which can be more efficient | |
24 | // in some cases based on the uniqueness / amount / size of the data. | |
25 | // these are left here for now for use in the benchmarks to compare against the | |
26 | // custom hash table implementation in the internal/hashing package as a base | |
27 | // benchmark comparison. | |
28 | ||
29 | {{range .In}} | |
30 | {{if and (ne .Name "ByteArray") (ne .Name "FixedLenByteArray") (ne .Name "Float64") (ne .Name "Boolean")}} | |
31 | func New{{.Name}}MemoTable(memory.Allocator) MemoTable { | |
32 | return &{{.lower}}MemoTableImpl{ | |
33 | table: make(map[{{.name}}]struct{ | |
34 | value {{.name}} | |
35 | memoIndex int | |
36 | }), | |
37 | nullIndex: keyNotFound, | |
38 | } | |
39 | } | |
40 | ||
41 | type {{.lower}}MemoTableImpl struct { | |
42 | table map[{{.name}}]struct{ | |
43 | value {{.name}} | |
44 | memoIndex int | |
45 | } | |
46 | nullIndex int | |
47 | } | |
48 | ||
49 | func (m *{{.lower}}MemoTableImpl) Reset() { | |
50 | m.table = make(map[{{.name}}]struct{ | |
51 | value {{.name}} | |
52 | memoIndex int | |
53 | }) | |
54 | m.nullIndex = keyNotFound | |
55 | } | |
56 | ||
57 | func (m *{{.lower}}MemoTableImpl) GetNull() (int, bool) { | |
58 | return m.nullIndex, m.nullIndex != keyNotFound | |
59 | } | |
60 | ||
61 | func (m *{{.lower}}MemoTableImpl) Size() int { | |
62 | sz := len(m.table) | |
63 | if _, ok := m.GetNull(); ok { | |
64 | sz++ | |
65 | } | |
66 | return sz | |
67 | } | |
68 | ||
69 | func (m *{{.lower}}MemoTableImpl) GetOrInsertNull() (idx int, found bool) { | |
70 | idx, found = m.GetNull() | |
71 | if !found { | |
72 | idx = m.Size() | |
73 | m.nullIndex = idx | |
74 | } | |
75 | return | |
76 | } | |
77 | ||
78 | func (m *{{.lower}}MemoTableImpl) Get(val interface{}) (int, bool) { | |
79 | v := val.({{.name}}) | |
80 | if p, ok := m.table[v]; ok { | |
81 | return p.memoIndex, true | |
82 | } | |
83 | return keyNotFound, false | |
84 | } | |
85 | ||
86 | func (m *{{.lower}}MemoTableImpl) GetOrInsert(val interface{}) (idx int, found bool, err error) { | |
87 | v := val.({{.name}}) | |
88 | p, ok := m.table[v] | |
89 | if ok { | |
90 | idx = p.memoIndex | |
91 | } else { | |
92 | idx = m.Size() | |
93 | p.value = v | |
94 | p.memoIndex = idx | |
95 | m.table[v] = p | |
96 | found = true | |
97 | } | |
98 | return | |
99 | } | |
100 | ||
101 | func (m *{{.lower}}MemoTableImpl) WriteOut(out []byte) { | |
102 | m.CopyValuesSubset(0, out) | |
103 | } | |
104 | ||
105 | func (m *{{.lower}}MemoTableImpl) WriteOutSubset(start int, out []byte) { | |
106 | m.CopyValuesSubset(start, out) | |
107 | } | |
108 | ||
109 | func (m *{{.lower}}MemoTableImpl) CopyValues(out interface{}) { | |
110 | m.CopyValuesSubset(0, out) | |
111 | } | |
112 | ||
113 | func (m *{{.lower}}MemoTableImpl) CopyValuesSubset(start int, out interface{}) { | |
114 | outval := out.([]{{.name}}) | |
115 | for _, v := range m.table { | |
116 | idx := v.memoIndex - start | |
117 | if idx >= 0 { | |
118 | outval[idx] = v.value | |
119 | } | |
120 | } | |
121 | } | |
122 | {{end}} | |
123 | {{end}} |