Exemple #1
0
 private void  InitTermBuffer()
 {
     if (termBuffer == null)
     {
         termBuffer = new char[ArrayUtil.GetNextSize(MIN_BUFFER_SIZE)];
         termLength = 0;
     }
 }
Exemple #2
0
        public override int GetHashCode()
        {
            InitTermBuffer();
            int code = termLength;

            code = code * 31 + startOffset;
            code = code * 31 + endOffset;
            code = code * 31 + flags;
            code = code * 31 + positionIncrement;
            code = code * 31 + type.GetHashCode();
            code = (payload == null?code:code * 31 + payload.GetHashCode());
            code = code * 31 + ArrayUtil.HashCode(termBuffer, 0, termLength);
            return(code);
        }
Exemple #3
0
 /// <summary>Allocates a buffer char[] of at least newSize, without preserving the existing content.
 /// its always used in places that set the content
 /// </summary>
 /// <param name="newSize">minimum size of the buffer
 /// </param>
 private void  GrowTermBuffer(int newSize)
 {
     if (termBuffer == null)
     {
         // The buffer is always at least MIN_BUFFER_SIZE
         termBuffer = new char[ArrayUtil.GetNextSize(newSize < MIN_BUFFER_SIZE?MIN_BUFFER_SIZE:newSize)];
     }
     else
     {
         if (termBuffer.Length < newSize)
         {
             // Not big enough; create a new array with slight
             // over allocation:
             termBuffer = new char[ArrayUtil.GetNextSize(newSize)];
         }
     }
 }
Exemple #4
0
 /// <summary>Grows the termBuffer to at least size newSize, preserving the
 /// existing content. Note: If the next operation is to change
 /// the contents of the term buffer use
 /// <see cref="SetTermBuffer(char[], int, int)" />,
 /// <see cref="SetTermBuffer(String)" />, or
 /// <see cref="SetTermBuffer(String, int, int)" />
 /// to optimally combine the resize with the setting of the termBuffer.
 /// </summary>
 /// <param name="newSize">minimum size of the new termBuffer
 /// </param>
 /// <returns> newly created termBuffer with length >= newSize
 /// </returns>
 public virtual char[] ResizeTermBuffer(int newSize)
 {
     if (termBuffer == null)
     {
         termBuffer = new char[ArrayUtil.GetNextSize(newSize < MIN_BUFFER_SIZE ? MIN_BUFFER_SIZE : newSize)];
     }
     else
     {
         if (termBuffer.Length < newSize)
         {
             // Not big enough; create a new array with slight
             // over allocation and preserve content
             var newCharBuffer = new char[ArrayUtil.GetNextSize(newSize)];
             Array.Copy(termBuffer, 0, newCharBuffer, 0, termBuffer.Length);
             termBuffer = newCharBuffer;
         }
     }
     return(termBuffer);
 }
Exemple #5
0
 internal PerDoc GetPerDoc()
 {
     lock (this)
     {
         if (freeCount == 0)
         {
             allocCount++;
             if (allocCount > docFreeList.Length)
             {
                 // Grow our free list up front to make sure we have
                 // enough space to recycle all outstanding PerDoc
                 // instances
                 System.Diagnostics.Debug.Assert(allocCount == 1 + docFreeList.Length);
                 docFreeList = new PerDoc[ArrayUtil.GetNextSize(allocCount)];
             }
             return(new PerDoc(this));
         }
         else
         {
             return(docFreeList[--freeCount]);
         }
     }
 }
Exemple #6
0
        public virtual void TestParseInt()
        {
            int test;

            try
            {
                test = ArrayUtil.ParseInt32("".ToCharArray());
                Assert.IsTrue(false);
            }
#pragma warning disable 168
            catch (FormatException e)
#pragma warning restore 168
            {
                //expected
            }
            try
            {
                test = ArrayUtil.ParseInt32("foo".ToCharArray());
                Assert.IsTrue(false);
            }
#pragma warning disable 168
            catch (FormatException e)
#pragma warning restore 168
            {
                //expected
            }
            try
            {
                test = ArrayUtil.ParseInt32(Convert.ToString(long.MaxValue).ToCharArray());
                Assert.IsTrue(false);
            }
#pragma warning disable 168
            catch (FormatException e)
#pragma warning restore 168
            {
                //expected
            }
            try
            {
                test = ArrayUtil.ParseInt32("0.34".ToCharArray());
                Assert.IsTrue(false);
            }
#pragma warning disable 168
            catch (FormatException e)
#pragma warning restore 168
            {
                //expected
            }

            try
            {
                test = ArrayUtil.ParseInt32("1".ToCharArray());
                Assert.IsTrue(test == 1, test + " does not equal: " + 1);
                test = ArrayUtil.ParseInt32("-10000".ToCharArray());
                Assert.IsTrue(test == -10000, test + " does not equal: " + -10000);
                test = ArrayUtil.ParseInt32("1923".ToCharArray());
                Assert.IsTrue(test == 1923, test + " does not equal: " + 1923);
                test = ArrayUtil.ParseInt32("-1".ToCharArray());
                Assert.IsTrue(test == -1, test + " does not equal: " + -1);
                test = ArrayUtil.ParseInt32("foo 1923 bar".ToCharArray(), 4, 4);
                Assert.IsTrue(test == 1923, test + " does not equal: " + 1923);
            }
            catch (FormatException e)
            {
                Console.WriteLine(e.ToString());
                Console.Write(e.StackTrace);
                Assert.IsTrue(false);
            }
        }
 public override Sorter NewSorter(Entry[] arr)
 {
     return(new ArrayInPlaceMergeSorter <Entry>(arr, ArrayUtil.GetNaturalComparer <Entry>()));
 }
Exemple #8
0
 public override int[] Init()
 {
     return(bytesStart = new int[ArrayUtil.Oversize(m_initSize, RamUsageEstimator.NUM_BYTES_INT32)]);
 }
Exemple #9
0
 public override int[] Grow()
 {
     Debug.Assert(bytesStart != null);
     return(bytesStart = ArrayUtil.Grow(bytesStart, bytesStart.Length + 1));
 }
Exemple #10
0
 public override Sorter NewSorter(Entry[] arr)
 {
     return(new ArrayIntroSorter <Entry>(arr, ArrayUtil.naturalComparator <Entry>()));
 }
Exemple #11
0
 /// <summary>
 /// Used to grow the reference array.
 /// <para/>
 /// In general this should not be used as it does not take the offset into account.
 /// <para/>
 /// @lucene.internal
 /// </summary>
 public void Grow(int newLength)
 {
     Debug.Assert(Offset == 0); // NOTE: senseless if offset != 0
     bytes = ArrayUtil.Grow(bytes, newLength);
 }
Exemple #12
0
 /// <summary>
 /// Expand the <see cref="T:long[]"/> with the size given as a number of words (64 bit longs). </summary>
 public virtual void EnsureCapacityWords(int numWords)
 {
     m_bits = ArrayUtil.Grow(m_bits, numWords);
     m_wlen = numWords;
     Debug.Assert((this.numBits = Math.Max(this.numBits, numWords << 6)) >= 0);
 }
Exemple #13
0
 protected override void Swap(int i, int j)
 {
     ArrayUtil.Swap(arr, i, j);
 }
Exemple #14
0
 public override Sorter NewSorter(Entry[] arr)
 {
     return(new ArrayTimSorter <Entry>(arr, ArrayUtil.GetNaturalComparer <Entry>(), TestUtil.NextInt(Random(), 0, arr.Length)));
 }
Exemple #15
0
 /// <summary>
 /// Create a <seealso cref="GrowableByteArrayDataOutput"/> with the given initial capacity. </summary>
 public GrowableByteArrayDataOutput(int cp)
 {
     this.Bytes  = new sbyte[ArrayUtil.Oversize(cp, 1)];
     this.Length = 0;
 }
Exemple #16
0
        private void TestCase(int itrsWithVal, int specifiedValsOnItr, bool removeDups)
        {
            // Build a random number of lists
            IList <int> expected = new JCG.List <int>();
            Random      random   = new J2N.Randomizer(Random.NextInt64());
            int         numLists = itrsWithVal + random.Next(1000 - itrsWithVal);

            IList <int>[] lists = new IList <int> [numLists];
            for (int i = 0; i < numLists; i++)
            {
                lists[i] = new JCG.List <int>();
            }
            int start = random.Next(1000000);
            int end   = start + VALS_TO_MERGE / itrsWithVal / Math.Abs(specifiedValsOnItr);

            for (int i = start; i < end; i++)
            {
                int maxList      = lists.Length;
                int maxValsOnItr = 0;
                int sumValsOnItr = 0;
                for (int itrWithVal = 0; itrWithVal < itrsWithVal; itrWithVal++)
                {
                    int list      = random.Next(maxList);
                    int valsOnItr = specifiedValsOnItr < 0 ? (1 + random.Next(-specifiedValsOnItr)) : specifiedValsOnItr;
                    maxValsOnItr  = Math.Max(maxValsOnItr, valsOnItr);
                    sumValsOnItr += valsOnItr;
                    for (int valOnItr = 0; valOnItr < valsOnItr; valOnItr++)
                    {
                        lists[list].Add(i);
                    }
                    maxList = maxList - 1;
                    ArrayUtil.Swap(lists, list, maxList);
                }
                int maxCount = removeDups ? maxValsOnItr : sumValsOnItr;
                for (int count = 0; count < maxCount; count++)
                {
                    expected.Add(i);
                }
            }
            // Now check that they get merged cleanly
            IEnumerator <int>[] itrs = new IEnumerator <int> [numLists];
            for (int i = 0; i < numLists; i++)
            {
                itrs[i] = lists[i].GetEnumerator();
            }
            try
            {
                MergedEnumerator <int> mergedItr    = new MergedEnumerator <int>(removeDups, itrs);
                using IEnumerator <int> expectedItr = expected.GetEnumerator();
                while (expectedItr.MoveNext())
                {
                    Assert.IsTrue(mergedItr.MoveNext());
                    Assert.AreEqual(expectedItr.Current, mergedItr.Current);
                }
                Assert.IsFalse(mergedItr.MoveNext());
            }
            finally
            {
                IOUtils.Dispose(itrs);
            }
        }