github.com/kazu/loncha@v0.6.3/example/sample_list.go (about)

     1  // generated by github.com/kazu/loncha/structer
     2  
     3  // ContainerListWriter is a base of http://golang.org/pkg/container/list/
     4  // this is tuning performancem, reduce heap usage.
     5  // Copyright 2009 The Go Authors. All rights reserved.
     6  // Use of this source code is governed by a BSD-style
     7  // license that can be found in the LICENSE file.
     8  package example
     9  
    10  import (
    11      "github.com/kazu/loncha/list_head"
    12      "unsafe"
    13      "errors"
    14  )
    15  
    16  func (d *Sample) Init() {
    17  	d.ListHead.Init()
    18  }
    19  
    20  // Next returns the next list element or nil.
    21  func (d *Sample) Next() *Sample {
    22  	if d.ListHead.Next() == nil {
    23  		panic(errors.New("d.next is nil"))
    24  	}
    25  	return (*Sample)(unsafe.Pointer(uintptr(unsafe.Pointer(d.ListHead.Next())) - unsafe.Offsetof(d.ListHead)))
    26  }
    27  // Prev returns the previous list element or nil.
    28  func (d *Sample) Prev() *Sample {
    29  	if d.ListHead.Next() == nil {
    30  		panic(errors.New("d.prev is nil"))
    31  	}
    32  	return (*Sample)(unsafe.Pointer(uintptr(unsafe.Pointer(d.ListHead.Prev())) - unsafe.Offsetof(d.ListHead)))
    33  }
    34  
    35  // New returns an initialized list.
    36  func NewSampleList(h *Sample) *Sample {
    37  	h.Init()
    38  	return h
    39  }
    40  
    41  func (d *Sample) Len() int {
    42  	return d.ListHead.Len()
    43  }
    44  
    45  func (d *Sample) Add(n *Sample)  *Sample {
    46  	d.ListHead.Add(&n.ListHead)
    47  	return n
    48  }
    49  
    50  func (d *Sample) Delete() *Sample {
    51  	ptr := d.ListHead.Delete()
    52  	return (*Sample)(unsafe.Pointer(uintptr(unsafe.Pointer(ptr)) - unsafe.Offsetof(d.ListHead)))
    53  }
    54  
    55  func (d *Sample) Remove() *Sample {
    56  	return d.Delete()
    57  }
    58  
    59  func (d *Sample) ContainOf(ptr *list_head.ListHead) *Sample {
    60  	return (*Sample)(unsafe.Pointer(uintptr(unsafe.Pointer(ptr)) - unsafe.Offsetof(d.ListHead)))
    61  }
    62  
    63  func (d *Sample) Front() *Sample {
    64  	return d.ContainOf(d.ListHead.Front())
    65  }
    66  
    67  
    68  func (d *Sample) Back() *Sample {
    69  	return d.ContainOf(d.ListHead.Back())
    70  }
    71  
    72  // PushFront inserts a new value v at the front of list l and returns e.
    73  func (d *Sample) PushFront(v *Sample) *Sample {
    74  	front := d.Front()
    75  	v.Add(front)
    76  	return v
    77  }
    78  
    79  
    80  // PushBack inserts a new element e with value v at the back of list l and returns e.
    81  func (l *Sample) PushBack(v *Sample) *Sample {
    82  	last := l.Back()
    83  	last.Add(v)
    84  	return v
    85  }
    86  
    87  // InsertBefore inserts a new element e with value v immediately before mark and returns e.
    88  // If mark is not an element of l, the list is not modified.
    89  func (l *Sample) InsertBefore(v *Sample) *Sample {
    90  	l.Prev().Add(v)
    91  	return v
    92  }
    93  
    94  // InsertAfter inserts a new element e with value v immediately after mark and returns e.
    95  // If mark is not an element of l, the list is not modified.
    96  func (l *Sample) InsertAfter(v *Sample) *Sample {
    97  	l.Next().Add(v)
    98  	return v
    99  }
   100  
   101  
   102  // MoveToFront moves element e to the front of list l.
   103  // If e is not an element of l, the list is not modified.
   104  func (l *Sample) MoveToFront(v *Sample) *Sample {
   105  	v.Remove()
   106  	return l.PushFront(v)
   107  }
   108  
   109  func (l *Sample) MoveToBack(v *Sample) *Sample {
   110  	v.Remove()
   111  	return l.PushBack(v)
   112  }
   113  
   114  
   115  // MoveBefore moves element e to its new position before mark.
   116  // If e or mark is not an element of l, or e == mark, the list is not modified.
   117  func (l *Sample) MoveBefore(v *Sample) *Sample {
   118  	v.Remove()
   119  	l.Prev().Add(v)
   120  	return v
   121  }
   122  
   123  // MoveAfter moves element e to its new position after mark.
   124  // If e is not an element of l, or e == mark, the list is not modified.
   125  func (l *Sample) MoveAfter(v *Sample) *Sample {
   126  	v.Remove()
   127  	l.Add(v)
   128  	return v
   129  }
   130  
   131  func (l *Sample) PushBackList(other *Sample) {
   132  	l.Back().Add(other)
   133  	return 
   134  }
   135  
   136  func (l *Sample) PushFrontList(other *Sample) {
   137  	other.PushBackList(l)
   138  	return
   139  }
   140  
   141  
   142  func (l *Sample) Each(fn func(e *Sample)) {
   143  
   144  	cur := l.Cursor()
   145  
   146  	for cur.Next() {
   147  		fn(l.ContainOf(cur.Pos))
   148  	}
   149  
   150  }