-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordTokenHashTable.go
131 lines (89 loc) · 3.24 KB
/
WordTokenHashTable.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
package WordTokenHashTable
import (
"SDCC-Project/aftmapreduce/utility"
"SDCC-Project/aftmapreduce/wordcount/DataStructures/WordToken"
"SDCC-Project/aftmapreduce/wordcount/DataStructures/WordTokenList"
"fmt"
)
type WordTokenHashTable struct {
hashTable []*WordTokenList.WordTokenList
hashTableSize uint
}
func New(size uint) *WordTokenHashTable {
output := new(WordTokenHashTable)
(*output).hashTable = make([]*WordTokenList.WordTokenList, size)
(*output).hashTableSize = size
for index := uint(0); index < size; index++ {
(*output).hashTable[index] = WordTokenList.New()
}
return output
}
func (obj *WordTokenHashTable) InsertWordToken(wordToken *WordToken.WordToken) error {
var index uint
var err error
var currentWordTokenList *WordTokenList.WordTokenList
if index, err = utility.GenerateArrayIndexFromString((*wordToken).Word, (*obj).hashTableSize); err != nil {
return err
}
currentWordTokenList = (*obj).hashTable[index]
(*currentWordTokenList).InsertWordToken(wordToken)
return nil
}
func (obj *WordTokenHashTable) InsertWord(word string) error {
return (*obj).InsertWordToken(WordToken.New(word, 1))
}
func (obj *WordTokenHashTable) Print() {
var currentList *WordTokenList.WordTokenList
for index := uint(0); index < (*obj).hashTableSize; index++ {
currentList = (*obj).hashTable[index]
fmt.Printf(" --- Array position: %d --- \n", index)
(*currentList).Print()
}
}
func (obj *WordTokenHashTable) GetWordTokenListAt(index int) *WordTokenList.WordTokenList {
return obj.hashTable[index]
}
func (obj *WordTokenHashTable) GetDigestAndSerializedData() (string, []byte, error) {
var output []WordToken.WordToken
var currentWordTokenList *WordTokenList.WordTokenList
var currentWordToken *WordToken.WordToken
totalNumberOfWordToken := 0
for index := uint(0); index < (*obj).hashTableSize; index++ {
currentWordTokenList = (*obj).hashTable[index]
totalNumberOfWordToken += (*currentWordTokenList).GetLength()
}
output = make([]WordToken.WordToken, totalNumberOfWordToken+1)
output[0].Word = ""
output[0].Occurrences = (*obj).hashTableSize
for index, outputIndex := uint(0), uint(1); index < (*obj).hashTableSize; index++ {
currentWordTokenList = (*obj).hashTable[index]
(*currentWordTokenList).IteratorReset()
for (*currentWordTokenList).Next() {
currentWordToken = (*currentWordTokenList).WordToken()
output[outputIndex].Word = (*currentWordToken).Word
output[outputIndex].Occurrences = (*currentWordToken).Occurrences
outputIndex++
}
}
if rawData, err := utility.Encoding(output); err != nil {
return "", nil, err
} else {
return utility.GenerateDigestUsingSHA512(rawData), rawData, nil
}
}
func Deserialize(input []byte) (*WordTokenHashTable, error) {
var output *WordTokenHashTable
var currentWordToken *WordToken.WordToken
serializedData := []WordToken.WordToken{}
if err := utility.Decoding(input, &serializedData); err != nil {
return nil, err
} else {
output = New(serializedData[0].Occurrences)
for index := uint(1); index < uint(len(serializedData)); index++ {
currentWordToken = WordToken.New(serializedData[index].Word, serializedData[index].Occurrences)
err := (*output).InsertWordToken(currentWordToken)
utility.CheckError(err)
}
return output, nil
}
}