Documentation
¶
Overview ¶
SIndex is a slice indexing library. It maintains an ordered list, by mapping list positions to slice indexes.
sindex.Interface implementation types: List (Basic slice list), LinkedList (Todo)
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Interface ¶
type Interface interface {
// Returns the index of where to put newly appended list item.
Append() (index int)
// Remove item at given position.
Remove(pos int)
// Insert item before given position. Returns index of where to put newly
// inserted list item. If pos >= Len() append.
Insert(pos int) (index int)
// Return the index of the given position.
Pos(pos int) (idex int)
// Return length of list.
Len() int
// Return iterator. It is initially invalid.
// The given position will be the position of the iterator after a Next() or
// Prev() call.
Iterator(pos int) IteratorInterface
// Clear the list to be empty.
Clear()
}
Implementations provide this interface to access the slice.
type IteratorInterface ¶
type IteratorInterface interface {
// Moves iterator to next position in a list. Returns true if next position
// exists else false. This is also the validity state of the iterator. If
// list is not empty and Next() returns false, to indicate end of the list,
// a call to Prev() will return true and move the iterator to last item in
// the list. (The opposite also applies when Prev() returns false to
// indicate there are no more preceding items).
Next() (valid bool)
Prev() (valid bool)
// Returns the index of the current position of the iterator. Must be called
// on valid iterator.
Pos() (index int)
// Insert item before current position of the iterator. Returns the index of
// the newly inserted item. The item to which the iterator points to does
// not change but it's position in the list will have incremented. Append on
// invalid iterator.
Insert() (index int)
// Removes list item at position of iterator.
// Changes the state of the iterator to invalid until a subsequent call to
// Next()/Prev() returns true. Must be called on valid iterator.
Remove()
}
An iterator iterates over a list and is able to modify the list. The state of an iterator is always at a position or is invalid. An iterator of a an empty list is always invalid.
Thinking of an iterator as a cursor here are all the positions:
invalid Pos(0) Pos(1) Pos(2) ... Pos(len-1) invalid cursor positions: ^ ^ ^ ^ ^ ^
Given a list of length four and an iterator at Pos(1):
invalid Pos(0) Pos(1) Pos(2) Pos(3) invalid cursor position: ^
After a Remove():
invalid Pos(0) invalid Pos(1) Pos(2) invalid cursor position: ^
And then a call to Next():
invalid Pos(0) Pos(1) Pos(2) invalid cursor position: ^
type List ¶
type List struct {
// contains filtered or unexported fields
}
Maps list positions directly to slice indexes. The length of the list is the length of the slice. The slice will grow if at capacity when calling Append/Insert.
Example ¶
bytes := []byte("helloworld")
bl := NewList(&bytes)
bytes[bl.Insert(5)] = ' '
bytes[bl.Append()] = '!'
fmt.Println(string(bytes))
for iter := bl.Iterator(0); iter.Next(); {
fmt.Print(string(bytes[iter.Pos()]))
}
Output: hello world! hello world!
func NewList ¶
func NewList(slicePointer interface{}, options ...OptionInterface) *List
Returns a new List that manages the slice pointed to by slicePointer. slicePointer should be a pointer to a slice. The slice pointed to by slicePointer will not be changed, the new List will be of the same length.
func (*List) Clear ¶
func (s *List) Clear()
Clear the list to be empty. Slice capacity remains the same.
func (*List) Insert ¶
Insert item before given position. Returns index of where to put newly inserted list item. If pos >= Len() append.
func (*List) Iterator ¶
func (s *List) Iterator(pos int) IteratorInterface
Return iterator. It is initially invalid. The given position will be the position of the iterator after a Next() or Prev() call.
type ListInterface ¶
type ListInterface interface {
Interface
// contains filtered or unexported methods
}
List satisfies this interface.
func InitListType ¶
func InitListType(structPointer ListInterface, options ...OptionInterface) (structPointerRet ListInterface)
Initializes a struct that promotes a List and also has a slice field. Pass this struct as a pointer to structPointer. Returns structPointer. This will call NewList to have the List field manage slice field. eg.
type stringList struct {
Data []string
List
}
strlist := InitListType(&stringList{}).(*stringList)
type OptionInterface ¶
type OptionInterface interface {
// contains filtered or unexported methods
}