Exemple #1
0
        public void Add(string key, int value)
        {
            var i = 0;

            while (i < key.Length && i < Key.Length && Key[i] == key[i])
            {
                i++;
            }

            if (i == key.Length && i == Key.Length)//Equals
            {
                Value = value;
            }
            else if (i == key.Length)//Key contains key
            {
                var oldChilds = new Dictionary <char, PatriciaTrieNode>(Childs);

                Childs.Clear();
                Childs.Add(Key[i], new PatriciaTrieNode(Key.Substring(i), Value, oldChilds));
                Key   = key.Substring(0, i);
                Value = value;
            }
            else if (i == Key.Length)//key contains Key
            {
                PatriciaTrieNode child;
                if (Childs.TryGetValue(key[i], out child))
                {
                    child.Add(key.Substring(i), value);
                }
                else
                {
                    Childs.Add(key[i], new PatriciaTrieNode(key.Substring(i), value));
                }
            }
            else//find a char not equals
            {
                PatriciaTrieNode child;
                if (Childs.TryGetValue(key[i], out child))
                {
                    child.Add(key.Substring(i), value);
                }
                else
                {
                    var oldChilds = new Dictionary <char, PatriciaTrieNode>(Childs);

                    Childs.Clear();
                    Childs.Add(Key[i], new PatriciaTrieNode(Key.Substring(i), Value, oldChilds));
                    Childs.Add(key[i], new PatriciaTrieNode(key.Substring(i), value));

                    Key   = Key.Substring(0, i);
                    Value = null;
                }
            }
        }
Exemple #2
0
        public bool Delete(string key)
        {
            PatriciaTrieNode child;

            if (!Childs.TryGetValue(key[Key.Length], out child))
            {
                return(false);
            }

            key = key.Substring(Key.Length);

            if (!key.StartsWith(child.Key))
            {
                return(false);
            }

            if (key.Length != child.Key.Length)
            {
                return(child.Delete(key));
            }

            if (!child.Value.HasValue)
            {
                return(false);
            }

            if (child.Childs.Count > 1)
            {
                child.Value = null;
            }
            else if (child.Childs.Count == 0)
            {
                Childs.Remove(key[0]);
            }
            else if (child.Childs.Count == 1)
            {
                var k = child.Childs.First().Key;
                child.Childs[k].Key = child.Key + child.Childs[k].Key;
                Childs.Clear();
                Childs.Add(child.Key[0], child.Childs[k]);
            }

            return(true);
        }
Exemple #3
0
 private bool UpdateSubscribtion(string id, IEnumerable <string> variablePath, bool remove, ref int subscriptions)
 {
     if (variablePath.Any())
     {
         var key = variablePath.First();
         SubscriptionTree entry;
         if (!Childs.TryGetValue(key, out entry) && !remove)
         {
             entry = new SubscriptionTree
             {
                 Name   = key,
                 Parent = this
             };
             if (!Childs.TryAdd(key, entry))
             {
                 entry = Childs[key];
             }
         }
         return(entry != null?entry.UpdateSubscribtion(id, variablePath.Skip(1), remove, ref subscriptions) : false);
     }
     else
     {
         if (remove)
         {
             object o;
             if (Subscribers.TryRemove(id, out o))
             {
                 Interlocked.Decrement(ref subscriptions);
                 return(true);
             }
             return(false);
         }
         else
         {
             if (Subscribers.TryAdd(id, null))
             {
                 Interlocked.Increment(ref subscriptions);
                 return(true);
             }
             return(false);
         }
     }
 }
Exemple #4
0
        public bool Find(string key)
        {
            if (!key.StartsWith(Key))
            {
                return(false);
            }

            if (key.Length == Key.Length)
            {
                return(Value.HasValue);
            }

            PatriciaTrieNode child;

            if (Childs.TryGetValue(key[Key.Length], out child))
            {
                return(child.Find(key.Substring(Key.Length)));
            }
            return(false);
        }