Example #1
0
        public void Add(int index, object value)
        {
            if (_elements == null)
            {
                _elements = new Element[ElementsCapacity];
            }

            var newElement = new Element {
                Index = index, Value = value
            };

            var elementIndex = index & ElementIndexMask;
            var e            = _elements[elementIndex];

            if (e == null)
            {
                _elements[elementIndex] = newElement;
                return;
            }

            // This overload of IsTrue doesn't call String.Format unless required.
            // Do, not switch to other version as it affects performance.
            Assumes.IsTrue(e.Index != index, "An item with the key '{0}' has already been added.", index);

            for (int offset = 1; offset <= LocalOffsetMax; ++offset)
            {
                var nextIndex = (index + offset) & ElementIndexMask;
                e = _elements[nextIndex];
                if (e == null)
                {
                    _elements[nextIndex] = newElement;
                    return;
                }

                Assumes.IsTrue(e.Index != index, "An item with the key '{0}' has already been added.", index);
            }

            if (_overflow == null)
            {
                _overflow = new SmallSparseInitonlyArray();
            }

            _overflow.Add(index, value);
        }
        public void Add(int index, object value)
        {
            if (_elements == null)
            {
                _elements = new Element[ElementsCapacity];
            }

            var newElement = new Element {
                Index = index, Value = value
            };

            var elementIndex = index & ElementIndexMask;
            var e            = _elements[elementIndex];

            if (e == null)
            {
                _elements[elementIndex] = newElement;
                return;
            }

            Assumes.IsTrue(e.Index != index, string.Format("An item with the key '{0}' has already been added.", index));

            for (int offset = 1; offset <= LocalOffsetMax; ++offset)
            {
                var nextIndex = (index + offset) & ElementIndexMask;
                e = _elements[nextIndex];
                if (e == null)
                {
                    _elements[nextIndex] = newElement;
                    return;
                }

                Assumes.IsTrue(e.Index != index, string.Format("An item with the key '{0}' has already been added.", index));
            }

            if (_overflow == null)
            {
                _overflow = new SmallSparseInitonlyArray();
            }

            _overflow.Add(index, value);
        }