Documentation ¶
Overview ¶
Package list implements a doubly linked list.
To iterate over a list (where l is a *List):
for e := l.Front(); e != nil; e = e.Next() { // do something with e.Value }
Example ¶
package main import ( "fmt" "github.com/GoLangsam/container/oneway/list" ) func main() { // Create a new list and put some numbers in it. l := list.New() e4 := l.PushBack(4) e1 := l.PushFront(1) l.InsertBefore(3, e4) l.InsertAfter(2, e1) // Iterate through list and print its contents. for e := l.Front(); e != nil; e = e.Next() { fmt.Println(e.Value) } }
Output: 1 2 3 4
Example (Interface) ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) // node abstracts the distinction between List and Element // - the focus is on common behaviour. type node interface { Beam Dust Init() *list.List List() *list.List AtomValues() list.Values Elements() []*list.Element } // Beam abstracts the 'lengthy' behaviour common to *list.Element & *list.List type Beam interface { CanGrow // Push Insert CanMove // Move MoveTo CanIter // Front Next CanReti // Back Prev Len() int Root() *list.Element } // CanGrow abstracts the growing behaviour common to *list.Element & *list.List type CanGrow interface { ValuesPushFront(values ...interface{}) ValuesPushBack(values ...interface{}) PushBack(v interface{}) *list.Element PushFront(v interface{}) *list.Element InsertAfter(v interface{}, mark *list.Element) *list.Element InsertBefore(v interface{}, mark *list.Element) *list.Element } // CanMove abstracts the moveing behaviour common to *list.Element & *list.List type CanMove interface { MoveToBack(e *list.Element) MoveToFront(e *list.Element) MoveAfter(e, mark *list.Element) MoveBefore(e, mark *list.Element) } // CanIter allows to iterate forward by starting with Front() and, if non-nil, repeating Next() until Next() returns nil type CanIter interface { Front() *list.Element Next() *list.Element ForEachNext(f func(*list.Element)) } // CanReti allows to iterate backward by starting with Back() and, if non-nil, repeating Prev() until Prev() returns nil // // Note: Reti is Iter spelled backwards. type CanReti interface { Back() *list.Element Prev() *list.Element ForEachPrev(f func(*list.Element)) } // Dust abstracts the 'pointy' behaviour common to *list.Element & *list.List type Dust interface { CVs() *list.ComposedValue IsAtom() bool IsComposed() bool PrintAtomValues(args ...interface{}) PrintValue(args ...interface{}) // Values() list.Values } // Coll combines all methods unique to any List, and not shared with Element type Coll interface { // Clear() *list.List IsEmpty() bool // TODO: e.IsEmpty() iff Value == nil? Print(args ...interface{}) // TODO: samesame for e, especially for Ring PushBackList(other *list.List) // TODO: samesame for e, especially for Ring PushFrontList(other *list.List) Remove(*list.Element) interface{} Values() list.Values // TODO: samesame for e, especially for Ring } // Atom combines all methods unique to any Element, and not shared with List type Atom interface { Remove() interface{} IsNode() bool IsRoot() bool MoveToPrevOf(*list.Element) *list.Element // MoveToNextOf(*list.Element) *list.Element } /* symmetric New(vals...) node Equals(x node) bool With(x node) *ComposedValue */ func main() { var _ node = list.New() var _ node = list.New().Root() var _ Coll = list.New() var _ Atom = list.New().Root() }
Output:
Index ¶
- type ComposedValue
- type Element
- func (e *Element) AtomValues() Values
- func (e *Element) Back() *Element
- func (e *Element) CVs() *ComposedValue
- func (e *Element) Elements() []*Element
- func (e *Element) Equals(i *Element) bool
- func (e *Element) ForEachNext(f func(*Element))
- func (e *Element) ForEachPrev(f func(*Element))
- func (e *Element) Front() *Element
- func (e *Element) Init() (list *List)
- func (root *Element) InsertAfter(v interface{}, mark *Element) *Element
- func (root *Element) InsertBefore(v interface{}, mark *Element) *Element
- func (e *Element) IsAtom() bool
- func (e *Element) IsComposed() bool
- func (e *Element) IsNode() bool
- func (e *Element) IsRoot() bool
- func (e *Element) Len() int
- func (e *Element) List() *List
- func (root *Element) MoveAfter(e, mark *Element)
- func (root *Element) MoveBefore(e, mark *Element)
- func (root *Element) MoveToBack(e *Element)
- func (root *Element) MoveToFront(e *Element)
- func (e *Element) MoveToPrevOf(at *Element) *Element
- func (e *Element) New(vals ...interface{}) *Element
- func (e *Element) Next() *Element
- func (e *Element) Prev() *Element
- func (e *Element) PrintAtomValues(args ...interface{})
- func (e *Element) PrintValue(args ...interface{})
- func (e *Element) PushBack(v interface{}) *Element
- func (e *Element) PushFront(v interface{}) *Element
- func (e *Element) Remove() interface{}
- func (e *Element) Root() *Element
- func (e *Element) Values() Values
- func (e *Element) ValuesPushBack(values ...interface{})
- func (e *Element) ValuesPushFront(values ...interface{})
- func (e *Element) With(x *Element) *ComposedValue
- type List
- func (l *List) AtomValues() Values
- func (l *List) Back() *Element
- func (l *List) CVs() *ComposedValue
- func (l *List) Elements() []*Element
- func (l *List) Equals(t *List) bool
- func (l *List) ForEachNext(f func(*Element))
- func (l *List) ForEachPrev(f func(*Element))
- func (l *List) Front() *Element
- func (l *List) Init() *List
- func (l *List) InsertAfter(v interface{}, mark *Element) *Element
- func (l *List) InsertBefore(v interface{}, mark *Element) *Element
- func (l *List) IsAtom() bool
- func (l *List) IsComposed() bool
- func (l *List) IsEmpty() bool
- func (l *List) Len() int
- func (l *List) List() *List
- func (l *List) MoveAfter(e, mark *Element)
- func (l *List) MoveBefore(e, mark *Element)
- func (l *List) MoveToBack(e *Element)
- func (l *List) MoveToFront(e *Element)
- func (l *List) New(vals ...interface{}) *List
- func (l *List) Next() *Element
- func (l *List) Prev() *Element
- func (l *List) Print(args ...interface{})
- func (l *List) PrintAtomValues(args ...interface{})
- func (l *List) PrintValue(args ...interface{})
- func (l *List) PushBack(v interface{}) *Element
- func (l *List) PushBackList(other *List)
- func (l *List) PushFront(v interface{}) *Element
- func (l *List) PushFrontList(other *List)
- func (l *List) Remove(e *Element) interface{}
- func (l *List) Root() *Element
- func (l *List) Values() Values
- func (l *List) ValuesPushBack(values ...interface{})
- func (l *List) ValuesPushFront(values ...interface{})
- func (l *List) With(x *List) *ComposedValue
- type Values
Examples ¶
- Package
- Package (Interface)
- Element.Back
- Element.Elements
- Element.Equals
- Element.ForEachNext
- Element.ForEachPrev
- Element.Front
- Element.Init
- Element.IsNode
- Element.IsRoot
- Element.List
- Element.MoveToPrevOf
- Element.New
- Element.Next
- Element.Prev
- Element.PrintAtomValues
- Element.PrintValue
- Element.Root
- Element.Values
- Element.With
- List.Back
- List.Elements
- List.Equals
- List.ForEachNext
- List.ForEachPrev
- List.Front
- List.Init
- List.IsEmpty
- List.List
- List.New
- List.Next
- List.Prev
- List.Print
- List.PrintAtomValues
- List.PrintValue
- List.Root
- List.Values
- List.ValuesPushBack
- List.ValuesPushFront
- List.With
- NewList
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ComposedValue ¶
type ComposedValue []*Element
ComposedValue aliases the Element.Value of IsComposed() Elements
type Element ¶
type Element struct { // The value stored with this element. Value interface{} // contains filtered or unexported fields }
Element is an element of a linked list.
func NewRing ¶
func NewRing(vals ...interface{}) (ring *Element)
NewRing returns a new ring. Iff no values are given, a nil element is returned.
func (*Element) AtomValues ¶
AtomValues - obtain slice of original values
func (*Element) Back ¶
Back returns the Back of this elements list (its last one, if any) or e.Prev(), iff e belongs to a Ring (and has no list).
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. var e = l.Front().Next().Next().Next() // D e.PrintValue("D = ") e = e.Back().Prev().Prev().Prev() // D e.PrintValue("D = ") }
Output: D = : DD = : D
func (*Element) CVs ¶
func (e *Element) CVs() *ComposedValue
CVs - obtain slice of *Element to be used as new Composed Value
If e.IsAtom() the slice has one element which points to e else a pointer(!) to the existing slice of atoms is returned
func (*Element) Elements ¶
Elements returns the Elements as a slice of the list of the element (syntactic sugar)
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. var e = l.Front().Next().Next().Next() // D for _, ele := range e.Elements() { ele.PrintValue() } }
Output: ABCDEFG
func (*Element) Equals ¶
Equals reports whether the elements e and i have the same values.
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. var e1 = l.Front().Next().Next().Next() // D var e2 = l.Back().Prev().Prev().Prev() // D if e1.Equals(e2) { // "D" == "D" // fine } else { e1.PrintValue("Element != Element") e2.PrintValue("Element != Element") } var l3 = list.NewList("List #3", "A", "B", "C", "D", "E", "F", "G") // another list with same values var e3 = l3.Front().Next().Next().Next() // D if e1.Equals(e3) { // different list, same values: "D" == "D" // fine } else { e1.PrintValue("Element != Element") e3.PrintValue("Element != Element") } if e1.Equals(e1.Next()) { // same lists, different values: "D" != "E" e1.PrintValue("Element != Element") e3.PrintValue("Element != Element") } else { // fine } e3.Value = 4711 if e1.Equals(e3) { // different lists, different values: "D" != 4711 e1.PrintValue("Element != Element") e3.PrintValue("Element != Element") } else { // fine } }
Output:
func (*Element) ForEachNext ¶
ForEachNext applies function f to each other element of e's list in natural order.
Example ¶
package main import ( "fmt" "github.com/GoLangsam/container/oneway/list" ) func main() { // Create a new list with some elements. var l = list.NewList("ForEach", "A", "B", "C", "D", "E", "F", "G") var e = l.Front().Next().Next().Next() // D fmt.Print("l.ForEachNext\t") // Prefix l.ForEachNext(func(e *list.Element) { e.PrintValue() }) // each Value fmt.Println("<") // Suffix line fmt.Print("e.ForEachNext\t") // Prefix e.ForEachNext(func(e *list.Element) { e.PrintValue() }) // every other Value fmt.Println("<") // Suffix line // Notice the subtle difference: // - for a list l all elements are iterated along the list l // - for an element e all *other* elements are iterated around e }
Output: l.ForEachNext ABCDEFG< e.ForEachNext EFGABC<
func (*Element) ForEachPrev ¶
ForEachPrev applies function f to each other element of e's list in reverse order.
Example ¶
package main import ( "fmt" "github.com/GoLangsam/container/oneway/list" ) func main() { // Create a new list with some elements. var l = list.NewList("ForEach", "A", "B", "C", "D", "E", "F", "G") var e = l.Front().Next().Next().Next() // D fmt.Print("l.ForEachPrev\t") // Prefix l.ForEachPrev(func(e *list.Element) { e.PrintValue() }) // each Value fmt.Println("<") // Suffix line fmt.Print("e.ForEachPrev\t") // Prefix e.ForEachPrev(func(e *list.Element) { e.PrintValue() }) // every other Value fmt.Println("<") // Suffix line // Notice the subtle difference: // - for a list l all elements are iterated along the list l // - for an element e all *other* elements are iterated around e }
Output: l.ForEachPrev GFEDCBA< e.ForEachPrev CBAGFE<
func (*Element) Front ¶
Front returns the Front of this elements list (its first one, if any) or e.Next(), iff e belongs to a Ring (and has no list).
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. var e = l.Front().Next().Next().Next() // D e.PrintValue("D = ") e = e.Front().Next().Next().Next() // D e.PrintValue("D = ") }
Output: D = : DD = : D
func (*Element) Init ¶
Init returns fresh and empty list. e is left untouched.
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. var e = l.Front().Next().Next().Next() // D e.PrintValue("e.before\t") e.Init() e.PrintValue("e.after\t") }
Output: e.before : De.after : D
func (*Element) InsertAfter ¶
InsertAfter inserts a new element e with value v immediately after mark and returns e. If mark is not an element of l, the list is not modified. The mark must not be nil.
func (*Element) InsertBefore ¶
InsertBefore inserts a new element e with value v immediately before mark and returns e. If mark is not an element of l, the list is not modified. The mark must not be nil.
func (*Element) IsComposed ¶
IsComposed <=> element is composed (and thus carries a Value.(type) []*Element)
func (*Element) IsNode ¶
IsNode reports whether the element e is non-nil and a (non-root) node of it's list or ring.
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. var e1 = l.Front().Next().Next().Next() // D var e2 = l.Root() // Root if e1.IsNode() { // fine } else { e1.PrintValue("is node?") } if e2.IsNode() { e2.PrintValue("is node?") } else { // fine } }
Output:
func (*Element) IsRoot ¶
IsRoot reports whether the element e is Root() of it's list unless it's nil, belongs to nil list or list's root is nil
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. var e1 = l.Front().Next().Next().Next() // D var e2 = l.Root() // Root if e1.IsRoot() { e1.PrintValue("no root?") } else { // fine } if e2.IsRoot() { // fine } else { e2.PrintValue("is root?") } }
Output:
func (*Element) Len ¶
Len returns the number of elements in the list of e or 0 (zero), if e is root or e is nil.
The complexity is O(1) iff e is element of a list and O(n) otherwise (e is element of some ring).
func (*Element) List ¶
List returns the list of e or nil iff e == nil or its list is nil.
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. var e = l.Back().Prev().Prev().Prev() // D if e.List() != l.List() { l.Print("List != List") } }
Output:
func (*Element) MoveAfter ¶
MoveAfter moves element e to its new position after mark. If e or mark is not an element of l, or e == mark, the list is not modified. The element and mark must not be nil.
func (*Element) MoveBefore ¶
MoveBefore moves element e to its new position before mark. If e or mark is not an element of l, or e == mark, the list is not modified. The element and mark must not be nil.
func (*Element) MoveToBack ¶
MoveToBack moves element e to the back of root. If e is not an element of l, the list is not modified. The element and root must not be nil.
func (*Element) MoveToFront ¶
MoveToFront moves element e to the front of root. If e is not an element of l, the list is not modified. The element and root must not be nil.
func (*Element) MoveToPrevOf ¶
MoveToPrevOf moves e to before at (or to the back of e.list, if at is nil) If at is not an element of the list of e, the list of e is not modified.
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. var e = l.Front().Next().Next().Next() // D var f = e.Next().Next() // F l.PrintAtomValues("l.before\t") // Show e.MoveToPrevOf(f) // move D before F (after E, that is) l.PrintAtomValues("l.after\t") // Show again }
Output: l.before : List=Example A | B | C | D | E | F | G | Total=7 l.after : List=Example A | B | C | E | D | F | G | Total=7
func (*Element) New ¶
New returns the root element of a new list the Root() of which carries vals[0] (if any) and the list has elements vals[1:] (if any)
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. var e = l.Front().Next().Next().Next() // D root := e.New("New", 111, 222, 333, 444, 555, 666, 777) // get the root of a new (and populated) list root.PrintAtomValues() // show root root.List().PrintAtomValues() // show root's list }
Output: Element=New. List=New 111 | 222 | 333 | 444 | 555 | 666 | 777 | Total=7
func (*Element) Next ¶
Next returns the next list element or nil.
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. var e = l.Front().Next().Next().Next() // D e.PrintValue("D = ") e = e.Back().Prev().Prev().Prev() // D e.PrintValue("D = ") }
Output: D = : DD = : D
func (*Element) Prev ¶
Prev returns the previous list element or nil.
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. var e = l.Back().Prev().Prev().Prev() // D e.PrintValue("D = ") }
Output: D = : D
func (*Element) PrintAtomValues ¶
func (e *Element) PrintAtomValues(args ...interface{})
PrintAtomValues is a convenience to print the atom values of e
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. var e = l.Front().Next().Next().Next() // D e.PrintAtomValues("Show", "Me") }
Output: ShowMe: Element=D.
func (*Element) PrintValue ¶
func (e *Element) PrintValue(args ...interface{})
PrintValue (AtomValues)
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. var e = l.Front().Next().Next().Next() // D e.PrintValue("Show", "Me") }
Output: ShowMe: D
func (*Element) PushBack ¶
PushBack inserts a new element e with value v at the back of list l and returns e.
func (*Element) PushFront ¶
PushFront inserts a new element e with value v at the front of ring e and returns e.
func (*Element) Remove ¶
func (e *Element) Remove() interface{}
Remove removes e from l if e is an element of list l. It returns the element value e.Value. The element must not be nil.
func (*Element) Root ¶
Root returns the Root of this elements list or nil iff e == nil or e iff its list is nil.
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. var e = l.Back().Prev().Prev().Prev() // D if e.List() != l.List() { l.Print("List != List") } e.Root().PrintValue("Root =") }
Output: Root =: Example
func (*Element) Values ¶
Values returns all Element.Values of e.List() as Values-slice (syntactic sugar)
Example ¶
package main import ( "fmt" "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. var e = l.Front().Next().Next().Next() // D for _, v := range e.Values() { fmt.Print(v) } fmt.Println() }
Output: ABCDEFG
func (*Element) ValuesPushBack ¶
func (e *Element) ValuesPushBack(values ...interface{})
ValuesPushBack appends a slice of Values
func (*Element) ValuesPushFront ¶
func (e *Element) ValuesPushFront(values ...interface{})
ValuesPushFront prepends a slice of Values
func (*Element) With ¶
func (e *Element) With(x *Element) *ComposedValue
With returns a new slice of *Element to be used as ComposedValue for new Elements
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. var e = l.Front().Next().Next().Next() // D var f = e.Next().Next() // F l.PrintAtomValues("l.before\t") // Show l.PushBack(e.With(f)) // move D before F (after E, that is) l.PrintAtomValues("l.after\t") // Show again }
Output: l.before : List=Example A | B | C | D | E | F | G | Total=7 l.after : List=Example A | B | C | D | E | F | G | D|F | Total=8
type List ¶
type List struct {
// contains filtered or unexported fields
}
List represents a doubly linked list. The zero value for List is an empty list ready to use.
func NewList ¶
func NewList(vals ...interface{}) *List
NewList returns a new list the Root() of which carries vals[0] (if any) and the list has elements vals[1:] (if any)
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. l.PrintAtomValues() // show it }
Output: List=Example A | B | C | D | E | F | G | Total=7
func (*List) AtomValues ¶
AtomValues - obtain slice of original values
Convenience for l.root.AtomValues()
func (*List) Back ¶
Back returns the last element of list l or nil if the list is empty.
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. var e = l.Back().Prev().Prev().Prev() // D e.PrintValue("D = ") }
Output: D = : D
func (*List) CVs ¶
func (l *List) CVs() *ComposedValue
CVs - obtain slice of *Element to be used as new Composed Value
Convenience for l.root.CVs()
func (*List) Elements ¶
Elements returns the elements of list l as a slice
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. for _, e := range l.Elements() { e.PrintValue() } }
Output: ABCDEFG
func (*List) Equals ¶
Equals reports whether the lists l and t have the same element-values.
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l1 = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. var l2 = list.NewList("List #2", "A", "B", "C", 444, "E", "F", "G") // a different one var l3 = list.NewList("List #3", "A", "B", "C", "D", "E", "F", "G") // a similar one if !l1.Equals(l2) { // "D" != 444 // fine } else { l1.Print("l1") l2.Print("l2") } if l1.Equals(l3) { // different list, same values in same sequence // fine } else { l1.Print("l1") l2.Print("l3") } }
Output:
func (*List) ForEachNext ¶
ForEachNext applies function f to each element of the list l in natural order.
Example ¶
package main import ( "fmt" "github.com/GoLangsam/container/oneway/list" ) func main() { // Create a new list with some elements. var l = list.NewList("ForEach", "A", "B", "C", "D", "E", "F", "G") var e = l.Front().Next().Next().Next() // D fmt.Print("l.ForEachNext\t") // Prefix l.ForEachNext(func(e *list.Element) { e.PrintValue() }) // each Value fmt.Println("<") // Suffix line fmt.Print("e.ForEachNext\t") // Prefix e.ForEachNext(func(e *list.Element) { e.PrintValue() }) // every other Value fmt.Println("<") // Suffix line // Notice the subtle difference: // - for a list l all elements are iterated along the list l // - for an element e all *other* elements are iterated around e }
Output: l.ForEachNext ABCDEFG< e.ForEachNext EFGABC<
func (*List) ForEachPrev ¶
ForEachPrev applies function f to each element of the list l in reverse order.
Example ¶
package main import ( "fmt" "github.com/GoLangsam/container/oneway/list" ) func main() { // Create a new list with some elements. var l = list.NewList("ForEach", "A", "B", "C", "D", "E", "F", "G") var e = l.Front().Next().Next().Next() // D fmt.Print("l.ForEachPrev\t") // Prefix l.ForEachPrev(func(e *list.Element) { e.PrintValue() }) // each Value fmt.Println("<") // Suffix line fmt.Print("e.ForEachPrev\t") // Prefix e.ForEachPrev(func(e *list.Element) { e.PrintValue() }) // every other Value fmt.Println("<") // Suffix line // Notice the subtle difference: // - for a list l all elements are iterated along the list l // - for an element e all *other* elements are iterated around e }
Output: l.ForEachPrev GFEDCBA< e.ForEachPrev CBAGFE<
func (*List) Front ¶
Front returns the first element of list l or nil if the list is empty.
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. var e = l.Front().Next().Next().Next() // D e.PrintValue("D = ") }
Output: D = : D
func (*List) Init ¶
Init initializes or clears list l.
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. l.PrintAtomValues("l.before\t") l.Init() l.PrintAtomValues("l.after\t") }
Output: l.before : List=Example A | B | C | D | E | F | G | Total=7 l.after : List=Example Total=0
func (*List) InsertAfter ¶
InsertAfter inserts a new element e with value v immediately after mark and returns e. If mark is not an element of l, the list is not modified. The mark must not be nil.
func (*List) InsertBefore ¶
InsertBefore inserts a new element e with value v immediately before mark and returns e. If mark is not an element of l, the list is not modified. The mark must not be nil.
func (*List) IsComposed ¶
IsComposed <=> l.root is composed (and thus carries a Value.(type) []*Element)
Convenience for l.root.IsComposed()
func (*List) IsEmpty ¶
IsEmpty reports whether the list l is empty. Note: Does not evaluate Len(), as this could be scrambled temporarily
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l1 = list.New() // A new list var l2 = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. var l3 = new(list.List) // Root if l1.IsEmpty() { // fine } else { l1.Print("l1 is empty?") } if l2.IsEmpty() { l2.PrintValue("not empty?") } else { // fine } if l3.IsEmpty() { // fine } else { l3.Print("l3 is empty?") } }
Output:
func (*List) List ¶
List returns this list (which may be nil).
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. var e = l.Front().Next().Next().Next() // D if l.List() != e.List() { l.Print("List != List") } }
Output:
func (*List) MoveAfter ¶
MoveAfter moves element e to its new position after mark. If e or mark is not an element of l, or e == mark, the list is not modified. The element and mark must not be nil.
func (*List) MoveBefore ¶
MoveBefore moves element e to its new position before mark. If e or mark is not an element of l, or e == mark, the list is not modified. The element and mark must not be nil.
func (*List) MoveToBack ¶
MoveToBack moves element e to the back of list l. If e is not an element of l, the list is not modified. The element must not be nil.
func (*List) MoveToFront ¶
MoveToFront moves element e to the front of list l. If e is not an element of l, the list is not modified. The element must not be nil.
func (*List) New ¶
New returns a new list the Root() of which carries vals[0] (if any) and the list has elements vals[1:] (if any)
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { nl := list.New() // a new list l1 := nl.New("Example", "A", "B", "C", "D", "E", "F", "G") l1.PrintAtomValues() // show it }
Output: List=Example A | B | C | D | E | F | G | Total=7
func (*List) Next ¶
Next returns the Front element of list l (its first one, if any) or nil.
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. if l.Next() != l.Front() { l.Print("Next != Front") } }
Output:
func (*List) Prev ¶
Prev returns the Back element of list l (its last one, if any) or nil.
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. if l.Prev() != l.Back() { l.Print("Prev != Back") } }
Output:
func (*List) Print ¶
func (l *List) Print(args ...interface{})
Print "List=" AtomValues | Total= & lf
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. l.Print("Show", "Me") }
Output: ShowMe: List=Example | Total=7
func (*List) PrintAtomValues ¶
func (l *List) PrintAtomValues(args ...interface{})
PrintAtomValues is a convenience to print a list with all elements atom values
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. l.PrintAtomValues("Show", "Me") }
Output: ShowMe: List=Example A | B | C | D | E | F | G | Total=7
func (*List) PrintValue ¶
func (l *List) PrintValue(args ...interface{})
PrintValue of l.Root()
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. l.PrintValue("Show", "Me") }
Output: ShowMe: Example
func (*List) PushBack ¶
PushBack inserts a new element e with value v at the back of list l and returns e.
func (*List) PushBackList ¶
PushBackList inserts a copy of an other list at the back of list l. The lists l and other may be the same. They must not be nil.
func (*List) PushFront ¶
PushFront inserts a new element e with value v at the front of list l and returns e.
func (*List) PushFrontList ¶
PushFrontList inserts a copy of an other list at the front of list l. The lists l and other may be the same. They must not be nil.
func (*List) Remove ¶
Remove removes e from l if e is an element of list l. It returns the element value e.Value. The element must not be nil.
func (*List) Root ¶
Root returns the root element of list l or nil iff l == nil or its root is nil.
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. var e = l.Front().Next().Next().Next() // D if l.Root() != e.Root() { l.Print("List != List") } l.Root().PrintValue("Root =") }
Output: Root =: Example
func (*List) Values ¶
Values returns all Element.Values as Values-slice
Example ¶
package main import ( "fmt" "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. for _, v := range l.Values() { fmt.Print(v) } fmt.Println() }
Output: ABCDEFG
func (*List) ValuesPushBack ¶
func (l *List) ValuesPushBack(values ...interface{})
ValuesPushBack appends a slice of Values
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. l.ValuesPushBack("H", "I", "J", "K") // Push some more at Back l.PrintAtomValues() // Show }
Output: List=Example A | B | C | D | E | F | G | H | I | J | K | Total=11
func (*List) ValuesPushFront ¶
func (l *List) ValuesPushFront(values ...interface{})
ValuesPushFront prepends a slice of Values
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. l.ValuesPushFront("X", "Y", "Z") // Push some more at Front l.PrintAtomValues() // Show }
Output: List=Example Z | Y | X | A | B | C | D | E | F | G | Total=10
func (*List) With ¶
func (l *List) With(x *List) *ComposedValue
With returns a new slice of *Element to be used as ComposedValue for new Elements
Example ¶
package main import ( "github.com/GoLangsam/container/oneway/list" ) func main() { var l1 = list.NewList("Example", "A", "B", "C", "D", "E", "F", "G") // Create a new list with some elements. var l2 = list.NewList("List #2", 111, 222, 333, 444, 555, 666, 777) // And another list with some elements. var l3 = list.NewList(l1.With(l2)) l3.PrintAtomValues() // Show }
Output: List=Example|List #2 Total=0