/
maprows.go
55 lines (47 loc) · 1.27 KB
/
maprows.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
// Copyright 2017, Shulhan <ms@kilabit.info>. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.
package tabula
import (
"math"
)
// MapRowsElement represent a single mapping of string key to rows.
type MapRowsElement struct {
Key string
Value Rows
}
// MapRows represent a list of mapping between string key and rows.
type MapRows []MapRowsElement
// insertRow will insert a row `v` into map using key `k`.
func (mapRows *MapRows) insertRow(k string, v *Row) {
rows := Rows{}
rows.PushBack(v)
el := MapRowsElement{k, rows}
(*mapRows) = append((*mapRows), el)
}
// AddRow will append a row `v` into map value if they key `k` exist in map,
// otherwise it will insert a new map element.
func (mapRows *MapRows) AddRow(k string, v *Row) {
for x := range *mapRows {
if (*mapRows)[x].Key == k {
(*mapRows)[x].Value.PushBack(v)
return
}
}
// no key found on map
mapRows.insertRow(k, v)
}
// GetMinority return map value which contain the minimum rows.
func (mapRows *MapRows) GetMinority() (keyMin string, valMin Rows) {
min := math.MaxInt32
for k := range *mapRows {
v := (*mapRows)[k].Value
l := len(v)
if l < min {
keyMin = (*mapRows)[k].Key
valMin = v
min = l
}
}
return
}