github.com/code-reading/golang@v0.0.0-20220303082512-ba5bc0e589a3/go/src/container/ring/ring.go (about)

     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.
     4  
     5  // Package ring implements operations on circular lists.
     6  // ring 实现了一个循环链表
     7  package ring
     8  
     9  // A Ring is an element of a circular list, or ring.
    10  // Rings do not have a beginning or end; a pointer to any ring element
    11  // serves as reference to the entire ring. Empty rings are represented
    12  // as nil Ring pointers. The zero value for a Ring is a one-element
    13  // ring with a nil Value.
    14  // Ring 是循环链表的元素结构, 可以称之为环
    15  // Rings 没有开始或结束位置
    16  // 执行任何一个ring元素的指针,都可以作为整个环的引用
    17  // Empty rings 表示一个空指针环
    18  // Ring的零值 表示只有一个nil元素的ring
    19  type Ring struct {
    20  	next, prev *Ring // 前后环指针
    21  	// 值,这个值在ring包中不会被处理
    22  	Value interface{} // for use by client; untouched by this library
    23  }
    24  
    25  // 初始化一个ring 其前后指针都执行ring本身, 表示一个空ring
    26  func (r *Ring) init() *Ring {
    27  	r.next = r
    28  	r.prev = r
    29  	return r
    30  }
    31  
    32  // Next returns the next ring element. r must not be empty.
    33  // 返回下一个ring 元素, r不能为Nil
    34  // 如果没有下一个元素, 则返回一个初始化的ring
    35  // r.next == nil 表示没有初始化,是一个空ring 此时
    36  func (r *Ring) Next() *Ring {
    37  	if r.next == nil {
    38  		return r.init()
    39  	}
    40  	return r.next
    41  }
    42  
    43  // Prev returns the previous ring element. r must not be empty.
    44  // 返回前一个ring元素
    45  // 如果为空, 则直接初始化一个ring 返回
    46  func (r *Ring) Prev() *Ring {
    47  	if r.next == nil {
    48  		return r.init()
    49  	}
    50  	return r.prev
    51  }
    52  
    53  // Move moves n % r.Len() elements backward (n < 0) or forward (n >= 0)
    54  // in the ring and returns that ring element. r must not be empty.
    55  // 指针从当前元素开始向后移动或者向前(n 可以为负数)
    56  // n < 0 向前移动
    57  // n > 0 向后移动
    58  // n == 0 什么都不做
    59  // r.next == nil 直接初始化这个ring 并返回
    60  // 否则返回移动之后的r指针
    61  func (r *Ring) Move(n int) *Ring {
    62  	if r.next == nil {
    63  		return r.init()
    64  	}
    65  	switch {
    66  	case n < 0:
    67  		for ; n < 0; n++ {
    68  			r = r.prev
    69  		}
    70  	case n > 0:
    71  		for ; n > 0; n-- {
    72  			r = r.next
    73  		}
    74  	}
    75  	return r
    76  }
    77  
    78  // New creates a ring of n elements.
    79  // 用于创建一个新的 Ring, 接收一个整形参数,用于初始化 Ring 的长度
    80  func New(n int) *Ring {
    81  	if n <= 0 {
    82  		return nil
    83  	}
    84  	r := new(Ring)
    85  	p := r
    86  	for i := 1; i < n; i++ {
    87  		p.next = &Ring{prev: p}
    88  		p = p.next
    89  	}
    90  	// 最后一个p 的后继执行根元素r
    91  	// 根元素的上一个指针prev 指向最后一个元素
    92  	p.next = r
    93  	r.prev = p
    94  	return r
    95  }
    96  
    97  // Link connects ring r with ring s such that r.Next()
    98  // becomes s and returns the original value for r.Next().
    99  // r must not be empty.
   100  //
   101  // If r and s point to the same ring, linking
   102  // them removes the elements between r and s from the ring.
   103  // The removed elements form a subring and the result is a
   104  // reference to that subring (if no elements were removed,
   105  // the result is still the original value for r.Next(),
   106  // and not nil).
   107  //
   108  // If r and s point to different rings, linking
   109  // them creates a single ring with the elements of s inserted
   110  // after r. The result points to the element following the
   111  // last element of s after insertion.
   112  // 将两个 ring 连接到一起 (r 不能为空)
   113  func (r *Ring) Link(s *Ring) *Ring {
   114  	n := r.Next()
   115  	if s != nil {
   116  		p := s.Prev()
   117  		// Note: Cannot use multiple assignment because
   118  		// evaluation order of LHS is not specified.
   119  		r.next = s
   120  		s.prev = r
   121  		n.prev = p
   122  		p.next = n
   123  	}
   124  	return n
   125  }
   126  
   127  // Unlink removes n % r.Len() elements from the ring r, starting
   128  // at r.Next(). If n % r.Len() == 0, r remains unchanged.
   129  // The result is the removed subring. r must not be empty.
   130  // 从当前元素开始,删除 n 个元素
   131  // r.Move(n + 1) 表示在当前ring 位置向前移动n+1 位
   132  //然后在和r.Link() 连接, 那么两个环已连接,就相当于把刚才走过的n个ring 删除了
   133  func (r *Ring) Unlink(n int) *Ring {
   134  	if n <= 0 {
   135  		return nil
   136  	}
   137  	return r.Link(r.Move(n + 1))
   138  }
   139  
   140  // Len computes the number of elements in ring r.
   141  // It executes in time proportional to the number of elements.
   142  // 返回ring的长度
   143  func (r *Ring) Len() int {
   144  	n := 0
   145  	// 只要r 不是nil 其长度就至少为1,
   146  	// init()返回的ring 是r !=nil 所以初始化的r 长度位1 但是value 是nil
   147  	if r != nil {
   148  		n = 1
   149  		for p := r.Next(); p != r; p = p.next {
   150  			n++
   151  		}
   152  	}
   153  	return n
   154  }
   155  
   156  // Do calls function f on each element of the ring, in forward order.
   157  // The behavior of Do is undefined if f changes *r.
   158  // Do 会依次将每个节点的 Value 当作参数调用这个函数 f,
   159  // 实际上这是策略方法的引用,通过传递不同的函数以在同一个 ring 上实现多种不同的操作。
   160  func (r *Ring) Do(f func(interface{})) {
   161  	if r != nil {
   162  		f(r.Value)
   163  		for p := r.Next(); p != r; p = p.next {
   164  			f(p.Value)
   165  		}
   166  	}
   167  }