A more efficient List{Int32} using a primitive integer array.
Ejemplo n.º 1
0
 public void testEmpty_SpecificCapacity()
 {
     IntList i = new IntList(5);
     Assert.AreEqual(0, i.size());
     try
     {
         i.get(0);
         Assert.Fail("Accepted 0 index on empty list");
     }
     catch (IndexOutOfRangeException)
     {
         Assert.IsTrue(true);
     }
 }
Ejemplo n.º 2
0
        /**
         * Index the region between <code>[ptr, end)</code> to find line starts.
         * <para />
         * The returned list is 1 indexed. Index 0 contains
         * {@link Integer#MIN_VALUE} to pad the list out.
         * <para />
         * Using a 1 indexed list means that line numbers can be directly accessed
         * from the list, so <code>list.get(1)</code> (aka get line 1) returns
         * <code>ptr</code>.
         * <para />
         * The last element (index <code>map.size()-1</code>) always contains
         * <code>end</code>.
         *
         * @param buf
         *            buffer to scan.
         * @param ptr
         *            position within the buffer corresponding to the first byte of
         *            line 1.
         * @param end
         *            1 past the end of the content within <code>buf</code>.
         * @return a line map indexing the start position of each line.
         */
        public static IntList lineMap(byte[] buf, int ptr, int end)
        {
            // Experimentally derived from multiple source repositories
            // the average number of bytes/line is 36. Its a rough guess
            // to initially size our map close to the target.
            //
            IntList map = new IntList((end - ptr) / 36);

            map.fillTo(1, int.MinValue);
            for (; ptr < end; ptr = nextLF(buf, ptr))
            {
                map.add(ptr);
            }
            map.add(end);
            return(map);
        }
Ejemplo n.º 3
0
        public void testAdd_SmallGroup()
        {
            IntList i = new IntList();
            int n = 5;
            for (int v = 0; v < n; v++)
                i.add(10 + v);

            Assert.AreEqual(n, i.size());

            for (int v = 0; v < n; v++)
                Assert.AreEqual(10 + v, i.get(v));

            try
            {
                i.get(n);
                Assert.Fail("Accepted out of bound index on list");
            }
            catch (IndexOutOfRangeException)
            {
                Assert.IsTrue(true);
            }
        }
Ejemplo n.º 4
0
 public void testToString()
 {
     IntList i = new IntList();
     i.add(1);
     Assert.AreEqual("[1]", i.toString());
     i.add(13);
     i.add(5);
     Assert.AreEqual("[1, 13, 5]", i.toString());
 }
Ejemplo n.º 5
0
        public void testSet()
        {
            IntList i = new IntList();
            i.add(1);
            Assert.AreEqual(1, i.size());
            Assert.AreEqual(1, i.get(0));

            i.set(0, 5);
            Assert.AreEqual(5, i.get(0));

            AssertHelper.Throws<ArgumentException>(() => i.set(5, 5), "accepted set of 5 beyond end of list");

            i.set(1, 2);
            Assert.AreEqual(2, i.size());
            Assert.AreEqual(2, i.get(1));
        }
Ejemplo n.º 6
0
        public void testClear()
        {
            IntList i = new IntList();
            int n = 5;
            for (int v = 0; v < n; v++)
                i.add(10 + v);
            Assert.AreEqual(n, i.size());

            i.clear();
            Assert.AreEqual(0, i.size());

            try
            {
                i.get(0);
                Assert.Fail("Accepted 0 index on empty list");
            }
            catch (IndexOutOfRangeException)
            {
                Assert.IsTrue(true);
            }
        }
Ejemplo n.º 7
0
 public void testFillTo100()
 {
     IntList i = new IntList();
     i.fillTo(100, int.MinValue);
     Assert.AreEqual(100, i.size());
     i.add(3);
     Assert.AreEqual(int.MinValue, i.get(99));
     Assert.AreEqual(3, i.get(100));
 }
Ejemplo n.º 8
0
 public void testFillTo1()
 {
     IntList i = new IntList();
     i.fillTo(1, int.MinValue);
     Assert.AreEqual(1, i.size());
     i.add(0);
     Assert.AreEqual(int.MinValue, i.get(0));
     Assert.AreEqual(0, i.get(1));
 }
Ejemplo n.º 9
0
 public void testFillTo0()
 {
     IntList i = new IntList();
     i.fillTo(0, int.MinValue);
     Assert.AreEqual(0, i.size());
 }
Ejemplo n.º 10
0
 public void testAdd_ZeroCapacity()
 {
     IntList i = new IntList(0);
     Assert.AreEqual(0, i.size());
     i.add(1);
     Assert.AreEqual(1, i.get(0));
 }
Ejemplo n.º 11
0
 /**
  * Index the region between <code>[ptr, end)</code> to find line starts.
  * <para />
  * The returned list is 1 indexed. Index 0 contains
  * {@link Integer#MIN_VALUE} to pad the list out.
  * <para />
  * Using a 1 indexed list means that line numbers can be directly accessed
  * from the list, so <code>list.get(1)</code> (aka get line 1) returns
  * <code>ptr</code>.
  * <para />
  * The last element (index <code>map.size()-1</code>) always contains
  * <code>end</code>.
  *
  * @param buf
  *            buffer to scan.
  * @param ptr
  *            position within the buffer corresponding to the first byte of
  *            line 1.
  * @param end
  *            1 past the end of the content within <code>buf</code>.
  * @return a line map indexing the start position of each line.
  */
 public static IntList lineMap(byte[] buf, int ptr, int end)
 {
     // Experimentally derived from multiple source repositories
     // the average number of bytes/line is 36. Its a rough guess
     // to initially size our map close to the target.
     //
     IntList map = new IntList((end - ptr) / 36);
     map.fillTo(1, int.MinValue);
     for (; ptr < end; ptr = nextLF(buf, ptr))
         map.add(ptr);
     map.add(end);
     return map;
 }
Ejemplo n.º 12
0
 ///	<summary>
 /// Create a new sequence from an existing content byte array.
 ///	<para />
 ///	The entire array (indexes 0 through length-1) is used as the content.
 ///	</summary>
 ///	<param name="input">
 ///	the content array. The array is never modified, so passing
 ///	through cached arrays is safe.
 /// </param>
 public RawText(byte[] input)
 {
     content = input;
     lines = RawParseUtils.lineMap(content, 0, content.Length);
     hashes = computeHashes();
 }
Ejemplo n.º 13
0
 private IntList computeHashes()
 {
     var r = new IntList(lines.size());
     r.add(0);
     for (int lno = 1; lno < lines.size() - 1; lno++)
     {
         int ptr = lines.get(lno);
         int end = lines.get(lno + 1);
         r.add(HashLine(content, ptr, end));
     }
     r.add(0);
     return r;
 }