Why are element and ring elements for golang list / ring?

Why do golang list / ring types use additional Element / Ring structures for individual elements, and not for the {} interface? I suppose there is some benefit, but I do not see it.

Edit: I wanted to ask about api and NOT about using Element / Ring in an implementation. An implementation may still use a non-exportable type, but has an api to give and receive interface {}, so why would users go in and out of Element / Ring?

Edit2: As an example, the Back () function might look like

func (l *List) Back() interface{} {
    if l.len == 0 {
        return nil
    }
    return l.root.prev.Value
}

If the list still uses the element inside, but it will just be the element (not exported), since it will not return it, but only return the value.

+4
2

/ - , List, .

, . , , ( Next() Prev()). Element , / .

, -.

type List struct {
    root Element // sentinel list element, only &root, root.prev, and root.next are used
    len  int     // current list length excluding (this) sentinel element
}

type Element struct {
    // Next and previous pointers in the doubly-linked list of elements.
    // To simplify the implementation, internally a list l is implemented
    // as a ring, such that &l.root is both the next element of the last
    // list element (l.Back()) and the previous element of the first list
    // element (l.Front()).
    next, prev *Element

    // The list to which this element belongs.
    list *List

    // The value stored with this element.
    Value interface{}
}

/ "" , . Ring, / , . / Ring, , .

type Ring struct {
    next, prev *Ring
    Value      interface{} // for use by client; untouched by this library
}
+4

.


list.go:

// Package list implements a doubly linked list.

// Element is an element of a linked list.
type Element struct {
    // Next and previous pointers in the doubly-linked list of elements.
    // To simplify the implementation, internally a list l is implemented
    // as a ring, such that &l.root is both the next element of the last
    // list element (l.Back()) and the previous element of the first list
    // element (l.Front()).
    next, prev *Element

    // The list to which this element belongs.
    list *List

    // The value stored with this element.
    Value interface{}
}

ring.go:

// Package ring implements operations on circular lists.

// A Ring is an element of a circular list, or ring.
// Rings do not have a beginning or end; a pointer to any ring element
// serves as reference to the entire ring. Empty rings are represented
// as nil Ring pointers. The zero value for a Ring is a one-element
// ring with a nil Value.
//
type Ring struct {
    next, prev *Ring
    Value      interface{} // for use by client; untouched by this library
}

, Element Ring interface{}, . .

Go

- . , , .

MethodDecl   = "func" Receiver MethodName ( Function | Signature ) .
Receiver     = "(" [ identifier ] [ "*" ] BaseTypeName ")" .
BaseTypeName = identifier .

T * T, T - . , T, ; , . , .

+3

Source: https://habr.com/ru/post/1544985/


All Articles