made Num public and Elems() now returns a list of empty interfaces

master
Pixdigit 5 years ago
parent 76c4ab31ec
commit a3302f8493

@ -2,18 +2,18 @@ package sorted
import "fmt"
type num float64
type Num float64
type List struct {
elems []*listElem
}
type listElem struct {
value num
value Num
elem interface{}
}
func (sl *List) Insert(elem interface{}, value num) {
func (sl *List) Insert(elem interface{}, value Num) {
newElem := listElem{
value,
elem,
@ -38,7 +38,7 @@ func (sl *List) Insert(elem interface{}, value num) {
}
}
func (sl *List) Nearest(value num) (interface{}, error) {
func (sl *List) Nearest(value Num) (interface{}, error) {
if len(sl.elems) == 0 {
return listElem{}, &ErrListEmpty{"List has no nearest element"}

@ -15,7 +15,7 @@ type uniqueElem interface {
}
type setElem struct {
value num
value Num
elem uniqueElem
}
@ -23,7 +23,7 @@ func (se *setElem) ID() uniqueID.ID {
return se.elem.ID()
}
func (s *Set) Insert(uElem uniqueElem, value num) error {
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"}
@ -55,7 +55,7 @@ func (s *Set) Insert(uElem uniqueElem, value num) error {
return nil
}
func (s *Set) Nearest(value num) (interface{}, error) {
func (s *Set) Nearest(value Num) (interface{}, error) {
if len(s.elems) == 0 {
return setElem{}, &ErrListEmpty{"List has no nearest element"}
@ -101,8 +101,12 @@ func (s *Set) String() string {
return str
}
func (s *Set) Elems() []*setElem {
return s.elems
func (s *Set) Elems() []interface{} {
copy := make([]interface{}, len(s.elems))
for i, elem := range s.elems {
copy[i] = interface{}(elem)
}
return copy
}
func (s *Set) Contains(testID uniqueID.ID) bool {

Loading…
Cancel
Save