forked from david415/HoneyBadger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ring.go
78 lines (69 loc) · 1.7 KB
/
ring.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Modified from Google's original container ring;
// Here the goal is to use the ring with our Reassembly type
// and remove any need for type assertions;
// thus we simplify our code making it easier to reason about.
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Honeybadger types package
package types
// 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.
type Ring struct {
next, prev *Ring
Reassembly *Reassembly
}
func (r *Ring) init() *Ring {
r.next = r
r.prev = r
return r
}
// Next returns the next ring element. r must not be empty.
func (r *Ring) Next() *Ring {
return r.next
}
// Prev returns the previous ring element. r must not be empty.
func (r *Ring) Prev() *Ring {
return r.prev
}
// NewRing creates a ring of n elements.
func NewRing(n int) *Ring {
if n <= 0 {
return nil
}
r := new(Ring)
p := r
for i := 1; i < n; i++ {
p.next = &Ring{prev: p}
p = p.next
}
p.next = r
r.prev = p
return r
}
// Len computes the number of elements in ring r.
// It executes in time proportional to the number of elements.
func (r *Ring) Len() int {
n := 0
if r != nil {
n = 1
for p := r.Next(); p != r; p = p.next {
n++
}
}
return n
}
// Count computes the number of none nil Reassembly structs populating the ring
func (r *Ring) Count() int {
count := 0
for current := r; current != r.Next(); current = current.Prev() {
if current.Reassembly != nil {
count += 1
} else {
break
}
}
return count
}