diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/go/container%2Fring%2Findex.html | |
new repository
Diffstat (limited to 'devdocs/go/container%2Fring%2Findex.html')
| -rw-r--r-- | devdocs/go/container%2Fring%2Findex.html | 199 |
1 files changed, 199 insertions, 0 deletions
diff --git a/devdocs/go/container%2Fring%2Findex.html b/devdocs/go/container%2Fring%2Findex.html new file mode 100644 index 00000000..6abf62b0 --- /dev/null +++ b/devdocs/go/container%2Fring%2Findex.html @@ -0,0 +1,199 @@ +<h1> Package ring </h1> <ul id="short-nav"> +<li><code>import "container/ring"</code></li> +<li><a href="#pkg-overview" class="overviewLink">Overview</a></li> +<li><a href="#pkg-index" class="indexLink">Index</a></li> +<li><a href="#pkg-examples" class="examplesLink">Examples</a></li> +</ul> <h2 id="pkg-overview">Overview </h2> <p>Package ring implements operations on circular lists. </p> <h2 id="pkg-index">Index </h2> <ul id="manual-nav"> +<li><a href="#Ring">type Ring</a></li> +<li> <a href="#New">func New(n int) *Ring</a> +</li> +<li> <a href="#Ring.Do">func (r *Ring) Do(f func(any))</a> +</li> +<li> <a href="#Ring.Len">func (r *Ring) Len() int</a> +</li> +<li> <a href="#Ring.Link">func (r *Ring) Link(s *Ring) *Ring</a> +</li> +<li> <a href="#Ring.Move">func (r *Ring) Move(n int) *Ring</a> +</li> +<li> <a href="#Ring.Next">func (r *Ring) Next() *Ring</a> +</li> +<li> <a href="#Ring.Prev">func (r *Ring) Prev() *Ring</a> +</li> +<li> <a href="#Ring.Unlink">func (r *Ring) Unlink(n int) *Ring</a> +</li> +</ul> <div id="pkg-examples"> <h3>Examples</h3> <dl> <dd><a class="exampleLink" href="#example_Ring_Do">Ring.Do</a></dd> <dd><a class="exampleLink" href="#example_Ring_Len">Ring.Len</a></dd> <dd><a class="exampleLink" href="#example_Ring_Link">Ring.Link</a></dd> <dd><a class="exampleLink" href="#example_Ring_Move">Ring.Move</a></dd> <dd><a class="exampleLink" href="#example_Ring_Next">Ring.Next</a></dd> <dd><a class="exampleLink" href="#example_Ring_Prev">Ring.Prev</a></dd> <dd><a class="exampleLink" href="#example_Ring_Unlink">Ring.Unlink</a></dd> </dl> </div> <h3>Package files</h3> <p> <span>ring.go</span> </p> <h2 id="Ring">type <span>Ring</span> </h2> <p>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. </p> +<pre data-language="go">type Ring struct { + Value any // for use by client; untouched by this library + // contains filtered or unexported fields +} +</pre> <h3 id="New">func <span>New</span> </h3> <pre data-language="go">func New(n int) *Ring</pre> <p>New creates a ring of n elements. </p> +<h3 id="Ring.Do">func (*Ring) <span>Do</span> </h3> <pre data-language="go">func (r *Ring) Do(f func(any))</pre> <p>Do calls function f on each element of the ring, in forward order. The behavior of Do is undefined if f changes *r. </p> <h4 id="example_Ring_Do"> <span class="text">Example</span> +</h4> <p>Code:</p> <pre class="code" data-language="go">// Create a new ring of size 5 +r := ring.New(5) + +// Get the length of the ring +n := r.Len() + +// Initialize the ring with some integer values +for i := 0; i < n; i++ { + r.Value = i + r = r.Next() +} + +// Iterate through the ring and print its contents +r.Do(func(p any) { + fmt.Println(p.(int)) +}) + +</pre> <p>Output:</p> <pre class="output" data-language="go">0 +1 +2 +3 +4 +</pre> <h3 id="Ring.Len">func (*Ring) <span>Len</span> </h3> <pre data-language="go">func (r *Ring) Len() int</pre> <p>Len computes the number of elements in ring r. It executes in time proportional to the number of elements. </p> <h4 id="example_Ring_Len"> <span class="text">Example</span> +</h4> <p>Code:</p> <pre class="code" data-language="go">// Create a new ring of size 4 +r := ring.New(4) + +// Print out its length +fmt.Println(r.Len()) + +</pre> <p>Output:</p> <pre class="output" data-language="go">4 +</pre> <h3 id="Ring.Link">func (*Ring) <span>Link</span> </h3> <pre data-language="go">func (r *Ring) Link(s *Ring) *Ring</pre> <p>Link connects ring r with ring s such that r.Next() becomes s and returns the original value for r.Next(). r must not be empty. </p> +<p>If r and s point to the same ring, linking them removes the elements between r and s from the ring. The removed elements form a subring and the result is a reference to that subring (if no elements were removed, the result is still the original value for r.Next(), and not nil). </p> +<p>If r and s point to different rings, linking them creates a single ring with the elements of s inserted after r. The result points to the element following the last element of s after insertion. </p> <h4 id="example_Ring_Link"> <span class="text">Example</span> +</h4> <p>Code:</p> <pre class="code" data-language="go">// Create two rings, r and s, of size 2 +r := ring.New(2) +s := ring.New(2) + +// Get the length of the ring +lr := r.Len() +ls := s.Len() + +// Initialize r with 0s +for i := 0; i < lr; i++ { + r.Value = 0 + r = r.Next() +} + +// Initialize s with 1s +for j := 0; j < ls; j++ { + s.Value = 1 + s = s.Next() +} + +// Link ring r and ring s +rs := r.Link(s) + +// Iterate through the combined ring and print its contents +rs.Do(func(p any) { + fmt.Println(p.(int)) +}) + +</pre> <p>Output:</p> <pre class="output" data-language="go">0 +0 +1 +1 +</pre> <h3 id="Ring.Move">func (*Ring) <span>Move</span> </h3> <pre data-language="go">func (r *Ring) Move(n int) *Ring</pre> <p>Move moves n % r.Len() elements backward (n < 0) or forward (n >= 0) in the ring and returns that ring element. r must not be empty. </p> <h4 id="example_Ring_Move"> <span class="text">Example</span> +</h4> <p>Code:</p> <pre class="code" data-language="go">// Create a new ring of size 5 +r := ring.New(5) + +// Get the length of the ring +n := r.Len() + +// Initialize the ring with some integer values +for i := 0; i < n; i++ { + r.Value = i + r = r.Next() +} + +// Move the pointer forward by three steps +r = r.Move(3) + +// Iterate through the ring and print its contents +r.Do(func(p any) { + fmt.Println(p.(int)) +}) + +</pre> <p>Output:</p> <pre class="output" data-language="go">3 +4 +0 +1 +2 +</pre> <h3 id="Ring.Next">func (*Ring) <span>Next</span> </h3> <pre data-language="go">func (r *Ring) Next() *Ring</pre> <p>Next returns the next ring element. r must not be empty. </p> <h4 id="example_Ring_Next"> <span class="text">Example</span> +</h4> <p>Code:</p> <pre class="code" data-language="go">// Create a new ring of size 5 +r := ring.New(5) + +// Get the length of the ring +n := r.Len() + +// Initialize the ring with some integer values +for i := 0; i < n; i++ { + r.Value = i + r = r.Next() +} + +// Iterate through the ring and print its contents +for j := 0; j < n; j++ { + fmt.Println(r.Value) + r = r.Next() +} + +</pre> <p>Output:</p> <pre class="output" data-language="go">0 +1 +2 +3 +4 +</pre> <h3 id="Ring.Prev">func (*Ring) <span>Prev</span> </h3> <pre data-language="go">func (r *Ring) Prev() *Ring</pre> <p>Prev returns the previous ring element. r must not be empty. </p> <h4 id="example_Ring_Prev"> <span class="text">Example</span> +</h4> <p>Code:</p> <pre class="code" data-language="go">// Create a new ring of size 5 +r := ring.New(5) + +// Get the length of the ring +n := r.Len() + +// Initialize the ring with some integer values +for i := 0; i < n; i++ { + r.Value = i + r = r.Next() +} + +// Iterate through the ring backwards and print its contents +for j := 0; j < n; j++ { + r = r.Prev() + fmt.Println(r.Value) +} + +</pre> <p>Output:</p> <pre class="output" data-language="go">4 +3 +2 +1 +0 +</pre> <h3 id="Ring.Unlink">func (*Ring) <span>Unlink</span> </h3> <pre data-language="go">func (r *Ring) Unlink(n int) *Ring</pre> <p>Unlink removes n % r.Len() elements from the ring r, starting at r.Next(). If n % r.Len() == 0, r remains unchanged. The result is the removed subring. r must not be empty. </p> <h4 id="example_Ring_Unlink"> <span class="text">Example</span> +</h4> <p>Code:</p> <pre class="code" data-language="go">// Create a new ring of size 6 +r := ring.New(6) + +// Get the length of the ring +n := r.Len() + +// Initialize the ring with some integer values +for i := 0; i < n; i++ { + r.Value = i + r = r.Next() +} + +// Unlink three elements from r, starting from r.Next() +r.Unlink(3) + +// Iterate through the remaining ring and print its contents +r.Do(func(p any) { + fmt.Println(p.(int)) +}) + +</pre> <p>Output:</p> <pre class="output" data-language="go">0 +4 +5 +</pre><div class="_attribution"> + <p class="_attribution-p"> + © Google, Inc.<br>Licensed under the Creative Commons Attribution License 3.0.<br> + <a href="http://golang.org/pkg/container/ring/" class="_attribution-link">http://golang.org/pkg/container/ring/</a> + </p> +</div> |
