summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGianni Ceccarelli <gianni.ceccarelli@broadbean.com>2019-08-01 17:12:33 +0100
committerGianni Ceccarelli <gianni.ceccarelli@broadbean.com>2019-08-01 17:12:33 +0100
commit14bd7e9a6550526cddeed14a602c19cfd6aedd9c (patch)
tree825bca5d65c5949c10671c623721aa9d56dbb89b
parentgo implemenntation (diff)
downloadmixed-server-14bd7e9a6550526cddeed14a602c19cfd6aedd9c.tar.gz
mixed-server-14bd7e9a6550526cddeed14a602c19cfd6aedd9c.tar.bz2
mixed-server-14bd7e9a6550526cddeed14a602c19cfd6aedd9c.zip
simpler and more correct model
-rw-r--r--go/mixed-server.go53
-rw-r--r--go/mixed-server_test.go4
2 files changed, 24 insertions, 33 deletions
diff --git a/go/mixed-server.go b/go/mixed-server.go
index 9476cc2..2408a44 100644
--- a/go/mixed-server.go
+++ b/go/mixed-server.go
@@ -15,35 +15,24 @@ import (
"sync"
)
-type wordsByCount struct {
- words *map[string]uint
- keys []string
+type WordAndCount struct {
+ word string
+ count uint
}
-func newWordsByCount(w *map[string]uint) wordsByCount {
- keys := make([]string, len(*w))
+func newWordsAndCount(w *map[string]uint) []WordAndCount {
+ ret := make([]WordAndCount, len(*w))
i := 0
- for w, _ := range *w {
- keys[i] = w
+ for word, count := range *w {
+ ret[i] = WordAndCount{word,count}
i++
}
- return wordsByCount{words: w, keys: keys}
+ return ret
}
-func (wbc wordsByCount) Len() int { return len(*wbc.words) }
-func (wbc wordsByCount) Swap(i, j int) {
- wbc.keys[i], wbc.keys[j] = wbc.keys[j], wbc.keys[i]
-}
-func (wbc wordsByCount) Less(i, j int) bool {
- return (*wbc.words)[wbc.keys[i]] > (*wbc.words)[wbc.keys[j]]
-}
-func (wbc wordsByCount) MostCommonWords() []string {
- sort.Sort(wbc)
- mcw := make([]string, len(*wbc.words))
- for i, p := range wbc.keys {
- mcw[i] = p
- }
- return mcw
+
+func (wac WordAndCount) lessThan(other WordAndCount) bool {
+ return wac.count < other.count
}
type Model struct {
@@ -64,15 +53,17 @@ func (m *Model) AddWords(words []string) {
}
}
-func (m *Model) GetMostCommonPairs(how_many uint) []string {
+func (m *Model) GetMostCommonPairs(how_many uint) []WordAndCount {
m.lock.RLock()
defer m.lock.RUnlock()
- wbc := newWordsByCount(&m.words)
- mcw := wbc.MostCommonWords()
- if how_many >= uint(len(mcw)) {
- how_many = uint(len(mcw))
+ wac := newWordsAndCount(&m.words)
+ sort.SliceStable(wac, func(i,j int) bool {
+ return ! wac[i].lessThan(wac[j])
+ })
+ if how_many >= uint(len(wac)) {
+ how_many = uint(len(wac))
}
- return mcw[:how_many]
+ return wac[:how_many]
}
type View struct{}
@@ -83,11 +74,11 @@ func NewView() View {
func (v View) WordsFromInput(input string) []string {
return strings.Fields(input)
}
-func (v View) TableFromRankedPairs(ranked_pairs []string) string {
+func (v View) TableFromRankedPairs(ranked_pairs []WordAndCount) string {
// we're not actually getting pairs, just strings
var b strings.Builder
- for _, s := range ranked_pairs {
- fmt.Fprintf(&b, "%s\n", s)
+ for _, p := range ranked_pairs {
+ fmt.Fprintf(&b, "%-15s %3d\n", p.word,p.count)
}
return b.String()
}
diff --git a/go/mixed-server_test.go b/go/mixed-server_test.go
index 63e545b..c7e3624 100644
--- a/go/mixed-server_test.go
+++ b/go/mixed-server_test.go
@@ -4,13 +4,13 @@ import (
"testing"
)
-func eq_slice(t *testing.T, got, expected []string) {
+func eq_slice(t *testing.T, got []WordAndCount, expected []string) {
if !(len(got)==len(expected)) {
t.Errorf("bad length, got %d expected %d",len(got),len(expected))
return
}
for i,got_elem := range got {
- if !(got_elem == expected[i]) {
+ if !(got_elem.word == expected[i]) {
t.Errorf("slices differ at %d, got %v expected %v",
i,got_elem,expected[i])
return