-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataTable.go
121 lines (107 loc) · 2.75 KB
/
DataTable.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
package main
import (
"bufio"
"errors"
"fmt"
"log"
"os"
"regexp"
"strconv"
"strings"
)
//DataItem basic item for random choice
type DataItem struct {
itemName string
itemValue int
}
//DataTable basic data type for random choice
type DataTable struct {
dataValues []DataItem
}
// AddValue to DataTable
func (dt *DataTable) AddValue(name string, value int) {
item := DataItem{name, value}
dt.dataValues = append(dt.dataValues, item)
}
// AddItem adds item (struct) to DataTable
func (dt *DataTable) AddItem(item DataItem) {
dt.dataValues = append(dt.dataValues, item)
}
// GetSize get virtual (!) size of all elements
func (dt DataTable) GetSize() int {
result := 0
for k := range dt.dataValues {
result += dt.dataValues[k].itemValue
}
return result
}
//GetElementAtValue returns element from table at requested ID
func (dt DataTable) GetElementAtValue(value int) string {
localIndex := 0
for k := range dt.dataValues {
if (value >= localIndex) && (value <= localIndex+dt.dataValues[k].itemValue) {
return dt.dataValues[k].itemName
}
localIndex += dt.dataValues[k].itemValue
}
return "None"
}
// DumpData from DataTable in human readable format
func (dt DataTable) DumpData() {
index := 1
for k := range dt.dataValues {
fmt.Printf("%s : ", dt.dataValues[k].itemName)
if dt.dataValues[k].itemValue > 1 {
fmt.Printf("%d-%d\n", index, index+dt.dataValues[k].itemValue-1)
index += dt.dataValues[k].itemValue
} else {
fmt.Println(index)
index++
}
}
}
// ReadFromFile loads DataItems from various type of files
func (dt *DataTable) ReadFromFile(filename string) (items int, err error) {
rItems := 0
var rErr error
file, err := os.Open(filename)
if err != nil {
fmt.Printf("error opening file: %v\n", err)
os.Exit(1)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
// fmt.Println(line)
item, err := ParseInputLine(line)
if err == nil {
dt.AddItem(item)
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return rItems, rErr
}
// ParseInputLine check and parse input data
func ParseInputLine(dataIn string) (DataItem, error) {
// simple line
// number:value
re := regexp.MustCompile(`^(?P<value>\d{1,});(?P<name>.*)$`)
match := re.FindStringSubmatch(dataIn)
if len(match) > 0 {
// value, _ := strconv.Atoi(match[1])
value := 1
return DataItem{match[2], value}, nil
}
re = regexp.MustCompile(`^(?P<startValue>\d{1,})-(?P<endValue>\d{1,});(?P<name>.*)$`)
match = re.FindStringSubmatch(dataIn)
if len(match) > 0 {
startValue, _ := strconv.Atoi(match[1])
endValue, _ := strconv.Atoi(match[2])
value := (endValue + 1) - startValue
return DataItem{strings.Trim(match[3], " "), value}, nil
}
return DataItem{}, errors.New("Cannot parse line")
}