added SetValue

Minor style and design changes
master
Pixdigit 5 years ago
parent a3302f8493
commit 4dae8ca837

@ -9,10 +9,18 @@ func (err ErrListEmpty) Error() string {
return err.S
}
type ErrDuplicateElement struct {
type ErrDuplicateElem struct {
S string
}
func (err ErrDuplicateElement) Error() string {
func (err ErrDuplicateElem) Error() string {
return err.S
}
type ErrNoElem struct {
S string
}
func (err ErrNoElem) Error() string {
return err.S
}

@ -6,6 +6,10 @@ import (
"gitlab.com/Pixdigit/uniqueID"
)
// Set is optimized for Set.Elems() to be fast.
// This is why it doesnt implement the functionality with a map
// This of course makes all other operations slow
type Set struct {
elems []*setElem
}
@ -26,7 +30,7 @@ func (se *setElem) ID() uniqueID.ID {
func (s *Set) Insert(uElem uniqueElem, value Num) error {
for _, elem := range s.elems {
if elem.ID() == uElem.ID() {
return &ErrDuplicateElement{"element already present in set"}
return &ErrDuplicateElem{"element already present in set"}
}
}
@ -55,6 +59,20 @@ func (s *Set) Insert(uElem uniqueElem, value Num) error {
return nil
}
func (s *Set) SetValue(ID uniqueID.ID, value Num) error {
for i, elem := range s.elems {
if elem.ID() == ID {
elem.value = value
//remove elem from array
s.elems = append(s.elems[:i], s.elems[i+1:]...)
//insert it again at new position
s.Insert(elem.elem, elem.value)
return nil
}
}
return ErrNoElem{"the list has no item with that ID"}
}
func (s *Set) Nearest(value Num) (interface{}, error) {
if len(s.elems) == 0 {
@ -104,7 +122,7 @@ func (s *Set) String() string {
func (s *Set) Elems() []interface{} {
copy := make([]interface{}, len(s.elems))
for i, elem := range s.elems {
copy[i] = interface{}(elem)
copy[i] = interface{}(elem.elem)
}
return copy
}

@ -12,7 +12,6 @@ type testElem struct {
Name string
}
var lastID uniqueID.ID
func NewSampleElem(IDSpace *uniqueID.IDSpace, name string) *testElem {
@ -27,7 +26,7 @@ func (te *testElem) ID() uniqueID.ID {
return te.id
}
func TestSet(t *testing.T) {
func TestNearest(t *testing.T) {
testIDSpace := &uniqueID.IDSpace{}
set := Set{}
@ -50,7 +49,32 @@ func TestSet(t *testing.T) {
tools.Test(set.Contains(lastID), "set did not contain added element", t)
t.Log(set.String())
}
func TestSetValue(t *testing.T) {
testIDSpace := &uniqueID.IDSpace{}
set := Set{}
a := NewSampleElem(testIDSpace, "a")
b := NewSampleElem(testIDSpace, "b")
c := NewSampleElem(testIDSpace, "c")
d := NewSampleElem(testIDSpace, "d")
e := NewSampleElem(testIDSpace, "e")
err := set.Insert(a, 1); if err != nil {tools.WrapErr(err, "could not insert test element", t)}
err = set.Insert(b, 2); if err != nil {tools.WrapErr(err, "could not insert test element", t)}
err = set.Insert(c, 3); if err != nil {tools.WrapErr(err, "could not insert test element", t)}
err = set.Insert(d, 5); if err != nil {tools.WrapErr(err, "could not insert test element", t)}
err = set.Insert(e, 4); if err != nil {tools.WrapErr(err, "could not insert test element", t)}
set.SetValue(a.ID(), 3.5)
set.SetValue(e.ID(), -2)
tools.Test(set.Elems()[0] == e, "did not correctly change position when set new value", t)
tools.Test(set.Elems()[1] == b, "did not correctly change position when set new value", t)
tools.Test(set.Elems()[2] == c, "did not correctly change position when set new value", t)
tools.Test(set.Elems()[3] == a, "did not correctly change position when set new value", t)
tools.Test(set.Elems()[4] == d, "did not correctly change position when set new value", t)
}

Loading…
Cancel
Save