diff options
| author | Shulhan <ms@kilabit.info> | 2018-09-17 01:21:27 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2018-09-18 01:50:21 +0700 |
| commit | 44b26edf7f390db383fe025454be0c4e30cfbd9b (patch) | |
| tree | 84d02953bc9095312182534936c1b60667957f07 /lib/tabula/maprows.go | |
| parent | 4a820ec157501c957d2e30f1670656cceec5c044 (diff) | |
| download | pakakeh.go-44b26edf7f390db383fe025454be0c4e30cfbd9b.tar.xz | |
Merge package "github.com/shuLhan/tabula"
Diffstat (limited to 'lib/tabula/maprows.go')
| -rw-r--r-- | lib/tabula/maprows.go | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/lib/tabula/maprows.go b/lib/tabula/maprows.go new file mode 100644 index 00000000..a93f0308 --- /dev/null +++ b/lib/tabula/maprows.go @@ -0,0 +1,65 @@ +// 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 +} |
