github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/util/intsRef.go (about)

     1  package util
     2  
     3  // util/IntsRef.java
     4  
     5  /* An empty integer array for convenience */
     6  var EMPTY_INTS = []int{}
     7  
     8  /*
     9  Represents []int, as a slice (offset + length) into an existing []int.
    10  The ints member should never be nil; use EMPTY_INTS if necessary.
    11  
    12  Go's native slice is always preferrable unless the reference pointer
    13  need to remain unchanged, in which case, this class is more useful.
    14  */
    15  type IntsRef struct {
    16  	// The contents of teh IntsRef. Should never be nil.
    17  	Ints []int
    18  	// Offset of first valid integer.
    19  	Offset int
    20  	// Length of used ints.
    21  	Length int
    22  }
    23  
    24  func NewEmptyIntsRef() *IntsRef {
    25  	return &IntsRef{Ints: EMPTY_INTS}
    26  }
    27  
    28  func (a *IntsRef) At(i int) int {
    29  	return a.Ints[a.Offset+i]
    30  }
    31  
    32  func (a *IntsRef) Value() []int {
    33  	return a.Ints[a.Offset : a.Offset+a.Length]
    34  }
    35  
    36  /* Signed int order comparison */
    37  func (a *IntsRef) Less(other *IntsRef) bool {
    38  	if a == other {
    39  		return false
    40  	}
    41  
    42  	aInts := a.Ints
    43  	aUpto := a.Offset
    44  	bInts := other.Ints
    45  	bUpto := other.Offset
    46  
    47  	var aStop int
    48  	if a.Length < other.Length {
    49  		aStop = aUpto + a.Length
    50  	} else {
    51  		aStop = aUpto + other.Length
    52  	}
    53  
    54  	for aUpto < aStop {
    55  		aInt := aInts[aUpto]
    56  		aUpto++
    57  		bInt := bInts[bUpto]
    58  		bUpto++
    59  		if aInt > bInt {
    60  			return false
    61  		} else if aInt < bInt {
    62  			return true
    63  		}
    64  	}
    65  
    66  	// one is a prefix of the other, or, they are equal:
    67  	return a.Length < other.Length
    68  }
    69  
    70  func (a *IntsRef) CopyInts(other *IntsRef) {
    71  	if len(a.Ints)-a.Offset < other.Length {
    72  		a.Ints = make([]int, other.Length)
    73  		a.Offset = 0
    74  	}
    75  	copy(a.Ints, other.Ints[other.Offset:other.Offset+other.Length])
    76  	a.Length = other.Length
    77  }
    78  
    79  /*
    80  Used to grow the reference slice.
    81  
    82  In general this should not be used as it does not take the offset into account.
    83  */
    84  func (a *IntsRef) Grow(newLength int) {
    85  	assert(a.Offset == 0)
    86  	if len(a.Ints) < newLength {
    87  		a.Ints = GrowIntSlice(a.Ints, newLength)
    88  	}
    89  }
    90  
    91  // util/IntsRefBuilder.java
    92  
    93  type IntsRefBuilder struct {
    94  	ref *IntsRef
    95  }
    96  
    97  func NewIntsRefBuilder() *IntsRefBuilder {
    98  	return &IntsRefBuilder{
    99  		ref: NewEmptyIntsRef(),
   100  	}
   101  }
   102  
   103  func (a *IntsRefBuilder) Length() int {
   104  	return a.ref.Length
   105  }
   106  
   107  func (a *IntsRefBuilder) Clear() {
   108  	a.ref.Length = 0
   109  }
   110  
   111  func (a *IntsRefBuilder) At(offset int) int {
   112  	return a.ref.Ints[offset]
   113  }
   114  
   115  func (a *IntsRefBuilder) Append(i int) {
   116  	a.Grow(a.ref.Length + 1)
   117  	a.ref.Ints[a.ref.Length] = i
   118  	a.ref.Length++
   119  }
   120  
   121  func (a *IntsRefBuilder) Grow(newLength int) {
   122  	a.ref.Ints = GrowIntSlice(a.ref.Ints, newLength)
   123  }
   124  
   125  func (a *IntsRefBuilder) CopyIntSlice(other []int) {
   126  	a.Grow(len(other))
   127  	copy(a.ref.Ints, other)
   128  	a.ref.Length = len(other)
   129  }
   130  
   131  func (a *IntsRefBuilder) CopyInts(ints *IntsRef) {
   132  	a.CopyIntSlice(ints.Ints[ints.Offset : ints.Offset+ints.Length])
   133  }
   134  
   135  func (a *IntsRefBuilder) Get() *IntsRef {
   136  	assert2(a.ref.Offset == 0, "Modifying the offset of the returned ref is illegal")
   137  	return a.ref
   138  }