diff options
Diffstat (limited to 'src/pkg/container/vector/vector.go')
-rw-r--r-- | src/pkg/container/vector/vector.go | 110 |
1 files changed, 55 insertions, 55 deletions
diff --git a/src/pkg/container/vector/vector.go b/src/pkg/container/vector/vector.go index 0408490be..ed1845b27 100644 --- a/src/pkg/container/vector/vector.go +++ b/src/pkg/container/vector/vector.go @@ -9,8 +9,8 @@ package vector // Vector is the container itself. // The zero value for Vector is an empty vector ready to use. type Vector struct { - a []interface{}; - bootstrap [8]interface{}; + a []interface{} + bootstrap [8]interface{} } @@ -21,31 +21,31 @@ func (p *Vector) realloc(length, capacity int) (b []interface{}) { } else { b = make([]interface{}, length, capacity) } - copy(b, p.a); - p.a = b; - return; + copy(b, p.a) + p.a = b + return } // Insert n elements at position i. func (p *Vector) expand(i, n int) { - a := p.a; + a := p.a // make sure we have enough space - len0 := len(a); - len1 := len0 + n; + len0 := len(a) + len1 := len0 + n if len1 <= cap(a) { // enough space - just expand a = a[0:len1] } else { // not enough space - double capacity - capb := cap(a) * 2; + capb := cap(a) * 2 if capb < len1 { // still not enough - use required length capb = len1 } // capb >= len1 - a = p.realloc(len1, capb); + a = p.realloc(len1, capb) } // make a hole @@ -53,7 +53,7 @@ func (p *Vector) expand(i, n int) { a[j+n] = a[j] } - p.a = a; + p.a = a } @@ -64,7 +64,7 @@ func (p *Vector) expand(i, n int) { // new length or capacity is longer that the current capacity. The resized // vector's capacity may be larger than the requested capacity. func (p *Vector) Resize(length, capacity int) *Vector { - a := p.a; + a := p.a if length > cap(a) || capacity > cap(a) { // not enough space or larger capacity requested explicitly @@ -76,91 +76,91 @@ func (p *Vector) Resize(length, capacity int) *Vector { } } - p.a = a[0:length]; - return p; + p.a = a[0:length] + return p } // Len returns the number of elements in the vector. -func (p *Vector) Len() int { return len(p.a) } +func (p *Vector) Len() int { return len(p.a) } // Cap returns the capacity of the vector; that is, the // maximum length the vector can grow without resizing. -func (p *Vector) Cap() int { return cap(p.a) } +func (p *Vector) Cap() int { return cap(p.a) } // At returns the i'th element of the vector. -func (p *Vector) At(i int) interface{} { return p.a[i] } +func (p *Vector) At(i int) interface{} { return p.a[i] } // Set sets the i'th element of the vector to value x. -func (p *Vector) Set(i int, x interface{}) { p.a[i] = x } +func (p *Vector) Set(i int, x interface{}) { p.a[i] = x } // Last returns the element in the vector of highest index. -func (p *Vector) Last() interface{} { return p.a[len(p.a)-1] } +func (p *Vector) Last() interface{} { return p.a[len(p.a)-1] } // Data returns all the elements as a slice. func (p *Vector) Data() []interface{} { - arr := make([]interface{}, p.Len()); + arr := make([]interface{}, p.Len()) for i, v := range p.a { arr[i] = v } - return arr; + return arr } // Insert inserts into the vector an element of value x before // the current element at index i. func (p *Vector) Insert(i int, x interface{}) { - p.expand(i, 1); - p.a[i] = x; + p.expand(i, 1) + p.a[i] = x } // Delete deletes the i'th element of the vector. The gap is closed so the old // element at index i+1 has index i afterwards. func (p *Vector) Delete(i int) { - a := p.a; - n := len(a); + a := p.a + n := len(a) - copy(a[i:n-1], a[i+1:n]); - a[n-1] = nil; // support GC, nil out entry - p.a = a[0 : n-1]; + copy(a[i:n-1], a[i+1:n]) + a[n-1] = nil // support GC, nil out entry + p.a = a[0 : n-1] } // InsertVector inserts into the vector the contents of the Vector // x such that the 0th element of x appears at index i after insertion. func (p *Vector) InsertVector(i int, x *Vector) { - p.expand(i, len(x.a)); - copy(p.a[i:i+len(x.a)], x.a); + p.expand(i, len(x.a)) + copy(p.a[i:i+len(x.a)], x.a) } // Cut deletes elements i through j-1, inclusive. func (p *Vector) Cut(i, j int) { - a := p.a; - n := len(a); - m := n - (j - i); + a := p.a + n := len(a) + m := n - (j - i) - copy(a[i:m], a[j:n]); + copy(a[i:m], a[j:n]) for k := m; k < n; k++ { - a[k] = nil // support GC, nil out entries + a[k] = nil // support GC, nil out entries } - p.a = a[0:m]; + p.a = a[0:m] } // Slice returns a new Vector by slicing the old one to extract slice [i:j]. // The elements are copied. The original vector is unchanged. func (p *Vector) Slice(i, j int) *Vector { - s := new(Vector).Resize(j-i, 0); // will fail in Init() if j < i - copy(s.a, p.a[i:j]); - return s; + s := new(Vector).Resize(j-i, 0) // will fail in Init() if j < i + copy(s.a, p.a[i:j]) + return s } @@ -168,7 +168,7 @@ func (p *Vector) Slice(i, j int) *Vector { // The function should not change the indexing of the vector underfoot. func (p *Vector) Do(f func(elem interface{})) { for i := 0; i < len(p.a); i++ { - f(p.a[i]) // not too safe if f changes the Vector + f(p.a[i]) // not too safe if f changes the Vector } } @@ -176,39 +176,39 @@ func (p *Vector) Do(f func(elem interface{})) { // Convenience wrappers // Push appends x to the end of the vector. -func (p *Vector) Push(x interface{}) { p.Insert(len(p.a), x) } +func (p *Vector) Push(x interface{}) { p.Insert(len(p.a), x) } // Pop deletes the last element of the vector. func (p *Vector) Pop() interface{} { - i := len(p.a) - 1; - x := p.a[i]; - p.a[i] = nil; // support GC, nil out entry - p.a = p.a[0:i]; - return x; + i := len(p.a) - 1 + x := p.a[i] + p.a[i] = nil // support GC, nil out entry + p.a = p.a[0:i] + return x } // AppendVector appends the entire Vector x to the end of this vector. -func (p *Vector) AppendVector(x *Vector) { p.InsertVector(len(p.a), x) } +func (p *Vector) AppendVector(x *Vector) { p.InsertVector(len(p.a), x) } // Partial sort.Interface support // LessInterface provides partial support of the sort.Interface. type LessInterface interface { - Less(y interface{}) bool; + Less(y interface{}) bool } // Less returns a boolean denoting whether the i'th element is less than the j'th element. -func (p *Vector) Less(i, j int) bool { return p.a[i].(LessInterface).Less(p.a[j]) } +func (p *Vector) Less(i, j int) bool { return p.a[i].(LessInterface).Less(p.a[j]) } // Swap exchanges the elements at indexes i and j. func (p *Vector) Swap(i, j int) { - a := p.a; - a[i], a[j] = a[j], a[i]; + a := p.a + a[i], a[j] = a[j], a[i] } @@ -217,13 +217,13 @@ func (p *Vector) iterate(c chan<- interface{}) { for _, v := range p.a { c <- v } - close(c); + close(c) } // Channel iterator for range. func (p *Vector) Iter() <-chan interface{} { - c := make(chan interface{}); - go p.iterate(c); - return c; + c := make(chan interface{}) + go p.iterate(c) + return c } |