Ejemplo n.º 1
0
        /// <summary>
        /// Adds the value after the last child of parent.
        /// </summary>
        public void AddAsChildOf(TKey key, TValue value, TKey parentKey)
        {
            NestedSetData <TValue> parentData = _values[parentKey];

            NestedSetData <TValue> newData = new NestedSetData <TValue>
            {
                Left  = parentData.Right,
                Right = parentData.Right + 1,
                Value = value
            };

            for (int i = parentData.Right; i < _positionCache.Length; i++)
            {
                NestedSetData <TValue> workingData = _values[_positionCache[i]];

                if (workingData.Left == i) // Messy but I couldn't think of a better way.
                {
                    workingData.Left += 2;
                }
                else
                {
                    workingData.Right += 2;
                }

                // Replace the value
                _values.Remove(_positionCache[i]);
                _values.Add(_positionCache[i], workingData);
            }

            _values.Add(key, newData);
            Rebuild();
        }
Ejemplo n.º 2
0
        public IEnumerable <T> GetChildren(T key)
        {
            NestedSetData parentData = _values[key];

            for (int i = parentData.Left + 1; i < parentData.Right; i++)
            {
                yield return(_positionCache[i]);
            }
        }
Ejemplo n.º 3
0
        public bool Remove(T item, NestedSetRemoveChildAction childAction)
        {
            // Does the item have child items?
            int correctionRequired = -2;

            if (_values[item].Left + 1 < _values[item].Right)
            {
                // Yes. What do we do?

                if (childAction == NestedSetRemoveChildAction.ThrowException)
                {
                    throw new NotSupportedException("Unable to remove item due to child items.");
                }
                if (childAction == NestedSetRemoveChildAction.ReturnFalse)
                {
                    return(false);
                }

                if (childAction == NestedSetRemoveChildAction.RecursiveDelete)
                {
                    correctionRequired = _values[item].Left - _values[item].Right - 1;

                    for (int i = _values[item].Left + 1; i < _values[item].Right; i++)
                    {
                        _values.Remove(_positionCache[i]);
                    }
                }
                else if (childAction == NestedSetRemoveChildAction.MoveUpGeneration)
                {
                    for (int i = _values[item].Left + 1; i < _values[item].Right; i++)
                    {
                        NestedSetData workingData = _values[_positionCache[i]];
                        workingData.Left  -= 1;
                        workingData.Right -= 1;

                        // Replace the value
                        _values.Remove(_positionCache[i]);
                        _values.Add(_positionCache[i], workingData);
                    }
                }
            }

            for (int i = _values[item].Right + 1; i < _positionCache.Length; i++)
            {
                NestedSetData workingData = _values[_positionCache[i]];
                workingData.Left  += correctionRequired;
                workingData.Right += correctionRequired;

                // Replace the value
                _values.Remove(_positionCache[i]);
                _values.Add(_positionCache[i], workingData);
            }
            Rebuild();

            return(true);
        }
Ejemplo n.º 4
0
        public ICollection <TValue> GetChildren(TKey key)
        {
            NestedSetData <TValue> parentData = _values[key];

            HashSet <TValue> children = new HashSet <TValue>();

            for (int i = parentData.Left + 1; i < parentData.Right; i++)
            {
                children.Add(_values[_positionCache[i]].Value);
            }
            return(children);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Gets or sets the element with the specified key.
 /// </summary>
 /// <returns>
 /// The element with the specified key.
 /// </returns>
 /// <param name="key">The key of the element to get or set.</param><exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null.</exception><exception cref="T:System.Collections.Generic.KeyNotFoundException">The property is retrieved and <paramref name="key"/> is not found.</exception><exception cref="T:System.NotSupportedException">The property is set and the <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only.</exception>
 public TValue this[TKey key]
 {
     get
     {
         return(_values[key].Value);
     }
     set
     {
         if (!_values.ContainsKey(key))
         {
             throw new NotSupportedException("Adding values is not supported using indexers. Use the supplied methods instead. Existing values may be changed with indexers however.");
         }
         NestedSetData <TValue> tempValue = _values[key];
         tempValue.Value = value;
         _values[key]    = tempValue;
     }
 }
Ejemplo n.º 6
0
        public void AddLeftOf(T value, T sibling)
        {
            for (int i = _values[sibling].Left; i < _positionCache.Length; i++)
            {
                NestedSetData workingData = _values[_positionCache[i]];
                workingData.Left  += 2;
                workingData.Right += 2;

                // Replace the value
                _values.Remove(_positionCache[i]);
                _values.Add(_positionCache[i], workingData);
            }
            _values.Add(value, new NestedSetData
            {
                Left  = _values[sibling].Right + 1,
                Right = _values[sibling].Right + 2
            });
            Rebuild();
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Adds the value after the last child of parent.
        /// </summary>
        public void AddAsChildOf(T value, T parent)
        {
            for (int i = _values[parent].Right; i < _positionCache.Length; i++)
            {
                NestedSetData workingData = _values[_positionCache[i]];
                workingData.Left  += 2;
                workingData.Right += 2;

                // Replace the value
                _values.Remove(_positionCache[i]);
                _values.Add(_positionCache[i], workingData);
            }
            _values.Add(value, new NestedSetData
            {
                Left  = _values[parent].Right - 2,
                Right = _values[parent].Right - 1
            });
            Rebuild();
        }