The Go Programming Language

Source file src/pkg/container/vector/stringvector.go

     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	// CAUTION: If this file is not vector.go, it was generated
     6	// automatically from vector.go - DO NOT EDIT in that case!
     7	
     8	package vector
     9	
    10	func (p *StringVector) realloc(length, capacity int) (b []string) {
    11		if capacity < initialSize {
    12			capacity = initialSize
    13		}
    14		if capacity < length {
    15			capacity = length
    16		}
    17		b = make(StringVector, length, capacity)
    18		copy(b, *p)
    19		*p = b
    20		return
    21	}
    22	
    23	// Insert n elements at position i.
    24	func (p *StringVector) Expand(i, n int) {
    25		a := *p
    26	
    27		// make sure we have enough space
    28		len0 := len(a)
    29		len1 := len0 + n
    30		if len1 <= cap(a) {
    31			// enough space - just expand
    32			a = a[0:len1]
    33		} else {
    34			// not enough space - double capacity
    35			capb := cap(a) * 2
    36			if capb < len1 {
    37				// still not enough - use required length
    38				capb = len1
    39			}
    40			// capb >= len1
    41			a = p.realloc(len1, capb)
    42		}
    43	
    44		// make a hole
    45		for j := len0 - 1; j >= i; j-- {
    46			a[j+n] = a[j]
    47		}
    48	
    49		*p = a
    50	}
    51	
    52	// Insert n elements at the end of a vector.
    53	func (p *StringVector) Extend(n int) { p.Expand(len(*p), n) }
    54	
    55	// Resize changes the length and capacity of a vector.
    56	// If the new length is shorter than the current length, Resize discards
    57	// trailing elements. If the new length is longer than the current length,
    58	// Resize adds the respective zero values for the additional elements. The capacity
    59	// parameter is ignored unless the new length or capacity is longer than the current
    60	// capacity. The resized vector's capacity may be larger than the requested capacity.
    61	func (p *StringVector) Resize(length, capacity int) *StringVector {
    62		a := *p
    63	
    64		if length > cap(a) || capacity > cap(a) {
    65			// not enough space or larger capacity requested explicitly
    66			a = p.realloc(length, capacity)
    67		} else if length < len(a) {
    68			// clear trailing elements
    69			for i := range a[length:] {
    70				var zero string
    71				a[length+i] = zero
    72			}
    73		}
    74	
    75		*p = a[0:length]
    76		return p
    77	}
    78	
    79	// Len returns the number of elements in the vector.
    80	// Same as len(*p).
    81	func (p *StringVector) Len() int { return len(*p) }
    82	
    83	// Cap returns the capacity of the vector; that is, the
    84	// maximum length the vector can grow without resizing.
    85	// Same as cap(*p).
    86	func (p *StringVector) Cap() int { return cap(*p) }
    87	
    88	// At returns the i'th element of the vector.
    89	func (p *StringVector) At(i int) string { return (*p)[i] }
    90	
    91	// Set sets the i'th element of the vector to value x.
    92	func (p *StringVector) Set(i int, x string) { (*p)[i] = x }
    93	
    94	// Last returns the element in the vector of highest index.
    95	func (p *StringVector) Last() string { return (*p)[len(*p)-1] }
    96	
    97	// Copy makes a copy of the vector and returns it.
    98	func (p *StringVector) Copy() StringVector {
    99		arr := make(StringVector, len(*p))
   100		copy(arr, *p)
   101		return arr
   102	}
   103	
   104	// Insert inserts into the vector an element of value x before
   105	// the current element at index i.
   106	func (p *StringVector) Insert(i int, x string) {
   107		p.Expand(i, 1)
   108		(*p)[i] = x
   109	}
   110	
   111	// Delete deletes the i'th element of the vector.  The gap is closed so the old
   112	// element at index i+1 has index i afterwards.
   113	func (p *StringVector) Delete(i int) {
   114		a := *p
   115		n := len(a)
   116	
   117		copy(a[i:n-1], a[i+1:n])
   118		var zero string
   119		a[n-1] = zero // support GC, zero out entry
   120		*p = a[0 : n-1]
   121	}
   122	
   123	// InsertVector inserts into the vector the contents of the vector
   124	// x such that the 0th element of x appears at index i after insertion.
   125	func (p *StringVector) InsertVector(i int, x *StringVector) {
   126		b := *x
   127	
   128		p.Expand(i, len(b))
   129		copy((*p)[i:i+len(b)], b)
   130	}
   131	
   132	// Cut deletes elements i through j-1, inclusive.
   133	func (p *StringVector) Cut(i, j int) {
   134		a := *p
   135		n := len(a)
   136		m := n - (j - i)
   137	
   138		copy(a[i:m], a[j:n])
   139		for k := m; k < n; k++ { //TODO(bflm) don't zero out the elements unless it's a Vector.
   140			var zero string
   141			a[k] = zero // support GC, zero out entries
   142		}
   143	
   144		*p = a[0:m]
   145	}
   146	
   147	// Slice returns a new sub-vector by slicing the old one to extract slice [i:j].
   148	// The elements are copied. The original vector is unchanged.
   149	func (p *StringVector) Slice(i, j int) *StringVector {
   150		var s StringVector
   151		s.realloc(j-i, 0) // will fail in Init() if j < i
   152		copy(s, (*p)[i:j])
   153		return &s
   154	}
   155	
   156	// Convenience wrappers
   157	
   158	// Push appends x to the end of the vector.
   159	func (p *StringVector) Push(x string) { p.Insert(len(*p), x) }
   160	
   161	// Pop deletes the last element of the vector.
   162	func (p *StringVector) Pop() string {
   163		a := *p
   164	
   165		i := len(a) - 1
   166		x := a[i]
   167		var zero string
   168		a[i] = zero // support GC, zero out entry
   169		*p = a[0:i]
   170		return x
   171	}
   172	
   173	// AppendVector appends the entire vector x to the end of this vector.
   174	func (p *StringVector) AppendVector(x *StringVector) { p.InsertVector(len(*p), x) }
   175	
   176	// Swap exchanges the elements at indexes i and j.
   177	func (p *StringVector) Swap(i, j int) {
   178		a := *p
   179		a[i], a[j] = a[j], a[i]
   180	}
   181	
   182	// Do calls function f for each element of the vector, in order.
   183	// The behavior of Do is undefined if f changes *p.
   184	func (p *StringVector) Do(f func(elem string)) {
   185		for _, e := range *p {
   186			f(e)
   187		}
   188	}

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.