aboutsummaryrefslogtreecommitdiff
path: root/ops.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-04-08 20:36:25 +0200
committerDimitri Sokolyuk <demon@dim13.org>2015-04-08 20:36:25 +0200
commit72c353d58fa33d11d58eb9276d2095e538ecebb8 (patch)
treee9cf67c2d7bb2a1f05c41ad2999a0db8e1fb42cb /ops.go
parent60ac54b9ffd6f8d57e9807f710ace28900955366 (diff)
Rewrite
Diffstat (limited to 'ops.go')
-rw-r--r--ops.go74
1 files changed, 32 insertions, 42 deletions
diff --git a/ops.go b/ops.go
index 75b21a8..73a2d38 100644
--- a/ops.go
+++ b/ops.go
@@ -2,92 +2,82 @@ package main
import (
"container/list"
- "fmt"
"log"
)
type (
- List struct{ *list.List }
+ // Set is ordered set of alphabet
+ Set struct{ *list.List }
+
+ // Element of ordered set
Element struct{ *list.Element }
)
var (
- alphabet List
+ alphabet Set
zero, one Element
)
func init() {
- alphabet = List{list.New()}
+ alphabet = Set{list.New()}
for i := 0; i <= maxValue; i++ {
- alphabet.PushBack(formatRoman(i))
+ alphabet.PushBack(i)
}
zero = Element{alphabet.Front()}
one = Element{zero.Next()}
}
-// next returns next char in alphabet
-func (n Element) next() Element {
- if nxt := n.Next(); nxt != nil {
+func (m Element) next() Element {
+ if nxt := m.Next(); nxt != nil {
return Element{nxt}
}
- log.Fatal("out of range ", n)
+ log.Fatal("out of range ", m)
return Element{}
}
-// prev returns previous char in alphabet
-func (n Element) prev() Element {
- if prv := n.Prev(); prv != nil {
+func (m Element) prev() Element {
+ if prv := m.Prev(); prv != nil {
return Element{prv}
}
return zero
}
-// add defines addition
-func add(m, n Element) Element {
+// Add defines addition
+func Add(m, n Element) Element { return m.add(n) }
+
+func (m Element) add(n Element) Element {
if m == zero {
return n
}
- return add(m.prev(), n).next()
+ return m.prev().add(n).next()
}
-// times defines mutiplication
-func times(m, n Element) Element {
+// Mul defines mutiplication
+func Mul(m, n Element) Element { return m.mul(n) }
+
+func (m Element) mul(n Element) Element {
if m == zero {
return zero
}
- return add(times(m.prev(), n), n)
+ return m.prev().mul(n).add(n)
}
-// pot defines power function
-func pot(m, n Element) Element {
+// Pot defines power function
+func Pot(m, n Element) Element { return m.pot(n) }
+
+func (m Element) pot(n Element) Element {
if n == zero {
return one
}
- return times(pot(m, n.prev()), m)
+ return m.pot(n.prev()).mul(m)
}
-// sub defines substraction
-func sub(m, n Element) Element {
+// Sub defines substraction
+func Sub(m, n Element) Element { return m.sub(n) }
+
+func (m Element) sub(n Element) Element {
if n == zero {
return m
}
- return sub(m, n.prev()).prev()
-}
-
-// scan lookups n-th element representation
-func scan(n int) Element {
- if n > alphabet.Len() {
- log.Fatal("out of range ", n)
- return Element{}
- }
- e := alphabet.Front()
- for i := 0; i < n; i++ {
- e = e.Next()
- }
- return Element{e}
-}
-
-// String pretty-prints value
-func (n Element) String() string {
- return fmt.Sprint(n.Value)
+ return m.sub(n.prev()).prev()
}