Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libgo / go / container / ring / ring.go
blob335afbc3cc546db23a8cc0ca3be6219134c1ef67
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // The ring package implements operations on circular lists.
6 package ring
8 // A Ring is an element of a circular list, or ring.
9 // Rings do not have a beginning or end; a pointer to any ring element
10 // serves as reference to the entire ring. Empty rings are represented
11 // as nil Ring pointers. The zero value for a Ring is a one-element
12 // ring with a nil Value.
14 type Ring struct {
15 next, prev *Ring
16 Value interface{} // for use by client; untouched by this library
20 func (r *Ring) init() *Ring {
21 r.next = r
22 r.prev = r
23 return r
27 // Next returns the next ring element. r must not be empty.
28 func (r *Ring) Next() *Ring {
29 if r.next == nil {
30 return r.init()
32 return r.next
36 // Prev returns the previous ring element. r must not be empty.
37 func (r *Ring) Prev() *Ring {
38 if r.next == nil {
39 return r.init()
41 return r.prev
45 // Move moves n % r.Len() elements backward (n < 0) or forward (n >= 0)
46 // in the ring and returns that ring element. r must not be empty.
48 func (r *Ring) Move(n int) *Ring {
49 if r.next == nil {
50 return r.init()
52 switch {
53 case n < 0:
54 for ; n < 0; n++ {
55 r = r.prev
57 case n > 0:
58 for ; n > 0; n-- {
59 r = r.next
62 return r
66 // New creates a ring of n elements.
67 func New(n int) *Ring {
68 if n <= 0 {
69 return nil
71 r := new(Ring)
72 p := r
73 for i := 1; i < n; i++ {
74 p.next = &Ring{prev: p}
75 p = p.next
77 p.next = r
78 r.prev = p
79 return r
83 // Link connects ring r with with ring s such that r.Next()
84 // becomes s and returns the original value for r.Next().
85 // r must not be empty.
87 // If r and s point to the same ring, linking
88 // them removes the elements between r and s from the ring.
89 // The removed elements form a subring and the result is a
90 // reference to that subring (if no elements were removed,
91 // the result is still the original value for r.Next(),
92 // and not nil).
94 // If r and s point to different rings, linking
95 // them creates a single ring with the elements of s inserted
96 // after r. The result points to the element following the
97 // last element of s after insertion.
99 func (r *Ring) Link(s *Ring) *Ring {
100 n := r.Next()
101 if s != nil {
102 p := s.Prev()
103 // Note: Cannot use multiple assignment because
104 // evaluation order of LHS is not specified.
105 r.next = s
106 s.prev = r
107 n.prev = p
108 p.next = n
110 return n
114 // Unlink removes n % r.Len() elements from the ring r, starting
115 // at r.Next(). If n % r.Len() == 0, r remains unchanged.
116 // The result is the removed subring. r must not be empty.
118 func (r *Ring) Unlink(n int) *Ring {
119 if n <= 0 {
120 return nil
122 return r.Link(r.Move(n + 1))
126 // Len computes the number of elements in ring r.
127 // It executes in time proportional to the number of elements.
129 func (r *Ring) Len() int {
130 n := 0
131 if r != nil {
132 n = 1
133 for p := r.Next(); p != r; p = p.next {
137 return n
141 func (r *Ring) Iter() <-chan interface{} {
142 c := make(chan interface{})
143 go func() {
144 if r != nil {
145 c <- r.Value
146 for p := r.Next(); p != r; p = p.next {
147 c <- p.Value
150 close(c)
152 return c