Esempio n. 1
0
        protected virtual ImmutableAvlTree <TKey, TValue> AddCore(TKey key, TValue value, InsertType type, out InsertType resultType)
        {
            ImmutableAvlTree <TKey, TValue> newTree = null;
            var comp = key.CompareTo(m_key);

            if (comp < 0)
            {
                newTree = CreateNew(m_left.AddCore(key, value, type, out resultType), m_right);
            }
            else if (comp > 0)
            {
                newTree = CreateNew(m_left, m_right.AddCore(key, value, type, out resultType));
            }
            else
            {
                if (type == InsertType.Add)
                {
                    throw new InvalidOperationException("Key already exists in the tree");
                }

                // Update the value
                newTree    = new ImmutableAvlTree <TKey, TValue>(key, value, Left, Right);
                resultType = InsertType.Update;
            }

            return(newTree.EnsureBalanced());
        }
Esempio n. 2
0
 private ImmutableAvlTree(TKey key, TValue value, ImmutableAvlTree <TKey, TValue> left, ImmutableAvlTree <TKey, TValue> right)
 {
     m_key    = key;
     m_value  = value;
     m_left   = left ?? Empty;
     m_right  = right ?? Empty;
     m_height = Math.Max(m_left.Height, m_right.Height) + 1;
     m_count  = m_left.Count + m_right.Count + 1;
 }
Esempio n. 3
0
        public Option <TValue> GreatestLowerBound(TKey key)
        {
            ImmutableAvlTree <TKey, TValue> ret = GreatestLowerBoundCore(key);

            if (ret == Empty)
            {
                return(Option <TValue> .Empty);
            }
            return(Option.Create(ret.Value));
        }
Esempio n. 4
0
        public ImmutableMap(IComparer <TKey> comparer)
        {
            if (comparer == null)
            {
                throw new ArgumentNullException("comparer");
            }

            m_comparer = comparer;
            m_tree     = ImmutableAvlTree <ComparerNode <TKey>, TValue> .Empty;
        }
Esempio n. 5
0
 protected override ImmutableAvlTree <TKey, TValue> AddCore(TKey key, TValue value, InsertType type, out InsertType resultType)
 {
     resultType = InsertType.Add;
     return(ImmutableAvlTree.Create(key, value));
 }
Esempio n. 6
0
 private ImmutableAvlTree <TKey, TValue> CreateNew(ImmutableAvlTree <TKey, TValue> left, ImmutableAvlTree <TKey, TValue> right)
 {
     return(new ImmutableAvlTree <TKey, TValue>(m_key, m_value, left, right));
 }
Esempio n. 7
0
 private ImmutableMap(IComparer <TKey> comparer, ImmutableAvlTree <ComparerNode <TKey>, TValue> tree)
 {
     m_comparer = comparer;
     m_tree     = tree;
 }