Exemple #1
0
            /// <summary>
            /// adds a pair of id and value to the buffer
            /// </summary>
            /// <param name="id"></param>
            /// <param name="val"></param>
            /// <returns></returns>
            public bool Add(int id, int val)
            {
                int ptr = m_info.Get(id << 1);

                if (ptr == EOD)
                {
                    // 1st insert
                    m_info.Add(id << 1, val);
                    return(true);
                }

                int cnt = m_info.Get((id << 1) + 1);

                if (cnt == EOD)
                {
                    // 2nd insert
                    m_info.Add((id << 1) + 1, val);
                    return(true);
                }

                if (ptr >= 0)
                {
                    // this id has two values stored in-line.
                    int firstVal  = ptr;
                    int secondVal = cnt;

                    ptr = m_buffer.Alloc(SEGSIZE);
                    m_buffer.Set(ptr++, EOD);
                    m_buffer.Set(ptr++, firstVal);
                    m_buffer.Set(ptr++, secondVal);
                    m_buffer.Set(ptr++, val);
                    cnt = 3;
                }
                else
                {
                    ptr = (-ptr);
                    if (cnt >= m_maxItems) // exceeded the limit
                    {
                        return(false);
                    }

                    if ((ptr % SEGSIZE) == 0)
                    {
                        int oldPtr = ptr;
                        ptr = m_buffer.Alloc(SEGSIZE);
                        m_buffer.Set(ptr++, (-oldPtr));
                    }
                    m_buffer.Set(ptr++, val);
                    cnt++;
                }

                m_info.Add(id << 1, (-ptr));
                m_info.Add((id << 1) + 1, cnt);

                return(true);
            }
        public static BigSegmentedArray FromArray(int[] original)
        {
            BigInt32Array result = new BigInt32Array(original.Length);
            int           i      = 0;

            foreach (int c in original)
            {
                result.Add(i++, c);
            }
            return(result);
        }
        public void TestBigIntArray()
        {
            int count = 5000000;
            var test  = new BigInt32Array(count);
            var test2 = new int[count];

            for (int i = 0; i < count; i++)
            {
                test.Add(i, i);
                test2[i] = i;
            }

            for (int i = 0; i < count; i++)
            {
                Assert.AreEqual(0, test.Get(0));
            }

            long start = System.Environment.TickCount;

            for (int i = 0; i < count; i++)
            {
                test.Get(i);
            }
            long end = System.Environment.TickCount;

            Console.WriteLine("Big array took: " + (end - start));

            start = System.Environment.TickCount;
            int k = 0;

            for (int i = 0; i < count; i++)
            {
                k = test2[i];
            }
            end = System.Environment.TickCount;
            Console.WriteLine("int[] took: " + (end - start));
        }