Beispiel #1
0
 public override int GetHashCode()
 {
     unchecked
     {
         return(((FullKey != null ? FullKey.GetHashCode() : 0) * 397) ^ (Value != null ? Value.GetHashCode() : 0));
     }
 }
            private KeyValuePair <FullKey, Branch> FindFirstBranch(Range range, ref FullKey nearFullKey, ref bool hasNearFullKey)
            {
                int idx;

                if (!range.IsBaseLocator)
                {
                    idx = range.LastIndex;
                }
                else
                {
                    idx = range.FirstIndex;
                    if (idx > 0)
                    {
                        idx--;
                    }
                }

                if (idx + 1 < Branches.Count)
                {
                    hasNearFullKey = true;
                    nearFullKey    = Branches[idx + 1].Key;
                }

                return(Branches[idx]);
            }
            private KeyValuePair <FullKey, Branch> FindLastBranch(Range range, ref FullKey nearFullKey, ref bool hasNearFullKey)
            {
                int idx = range.LastIndex; //no matter of range.IsBaseLocator

                if (idx > 0)
                {
                    hasNearFullKey = true;
                    nearFullKey    = Branches[idx - 1].Key;
                }

                return(Branches[idx]);
            }
            public Branch FindLastBranch(Locator locator, ref FullKey nearFullKey, ref bool hasNearFullKey)
            {
                Range range = Optimizator.FindRange(locator);
                int   idx   = range.LastIndex; //no matter of range.IsBaseLocator

                if (idx > 0)
                {
                    hasNearFullKey = true;
                    nearFullKey    = Branches[idx - 1].Key;
                }

                return(Branches[idx].Value);
            }
Beispiel #5
0
 public override int GetHashCode()
 {
     unchecked {
         var hashCode = _hashCode;
         hashCode = (hashCode * 397) ^ FullKey.GetHashCode();
         hashCode = (hashCode * 397) ^ (LoadtypeGuid != null ? LoadtypeGuid.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ DeviceCategory.GetHashCode();
         hashCode = (hashCode * 397) ^ LocationGuid.GetHashCode();
         hashCode = (hashCode * 397) ^ DeviceGuid.GetHashCode();
         hashCode = (hashCode * 397) ^ HouseholdKey.GetHashCode();
         hashCode = (hashCode * 397) ^ (int)ThisDeviceType;
         return(hashCode);
     }
 }
Beispiel #6
0
            public RegKey(string fullKey, int id)
            {
                ID      = id;
                FullKey = fullKey.TrimStart();

                //Detect if Key must be removed
                if (FullKey[FullKey.IndexOf('[') + 1] == '-')
                {
                    Action = KeyAction.Remove;
                }
                else
                {
                    Action = KeyAction.Add;
                }
            }
Beispiel #7
0
        public static List <FullKey> ReadTrainingKeys(string path)
        {
            List <FullKey> keys = new List <FullKey>();

            try
            {
                using (StreamReader reader = new StreamReader(path))
                {
                    while (!reader.EndOfStream)
                    {
                        var line        = reader.ReadLine();
                        var produceKey  = line.Split(':');
                        var fragments   = produceKey[1].Split('-');
                        var subKeysList = new List <SubKey>();

                        foreach (var fragment in fragments)
                        {
                            var subkey = new SubKey {
                                Length = fragment.Length, Value = fragment
                            };
                            subKeysList.Add(subkey);
                        }

                        var key = new FullKey
                        {
                            SubkeyLength = subKeysList.Count,
                            SubKeys      = subKeysList
                        };
                        keys.Add(key);
                    }

                    return(keys);
                }
            }
            catch (IOException e)
            {
                Console.WriteLine("The file could not be read!");
                Console.WriteLine(e.Message);
                throw;
            }
        }
Beispiel #8
0
 private bool Equals(BaseKey other)
 {
     return(FullKey.Equals(other.FullKey));
 }
Beispiel #9
0
            public IOrderedSet<IData, IData> FindData(Locator locator, Direction direction, ref FullKey nearFullKey, ref bool hasNearFullKey)
            {
                IOrderedSet<IData, IData> data = null;
                Container.TryGetValue(locator, out data);
                if (direction == Direction.None)
                    return data;

                if (Container.Count == 1 && data != null)
                    return data;

                IOrderedSet<IData, IData> nearData = null;
                if (direction == Direction.Backward)
                {
                    bool havePrev = false;
                    Locator prev = default(Locator);

                    foreach (var kv in Container)
                    {
                        if (kv.Key.CompareTo(locator) < 0)
                        {
                            if (!havePrev || kv.Key.CompareTo(prev) > 0)
                            {
                                prev = kv.Key;
                                nearData = kv.Value;
                                havePrev = true;
                            }
                        }
                    }

                    if (havePrev)
                    {
                        hasNearFullKey = true;
                        nearFullKey = new FullKey(prev, nearData.Last.Key);
                    }
                }
                else //if (direction == Direction.Forward)
                {
                    bool haveNext = false;
                    Locator next = default(Locator);

                    foreach (var kv in Container)
                    {
                        if (kv.Key.CompareTo(locator) > 0)
                        {
                            if (!haveNext || kv.Key.CompareTo(next) < 0)
                            {
                                next = kv.Key;
                                nearData = kv.Value;
                                haveNext = true;
                            }
                        }
                    }

                    if (haveNext)
                    {
                        hasNearFullKey = true;
                        nearFullKey = new FullKey(next, nearData.First.Key);
                    }
                }

                return data;
            }
            /// <summary>
            /// The hook.
            /// </summary>
            public KeyValuePair <FullKey, Branch> FindBranch(Locator locator, IData key, Direction direction, ref FullKey nearFullKey, ref bool hasNearFullKey)
            {
                Range range = Optimizator.FindRange(locator);

                if (key == null)
                {
                    switch (direction)
                    {
                    case Direction.Forward:
                        return(FindFirstBranch(range, ref nearFullKey, ref hasNearFullKey));

                    case Direction.Backward:
                        return(FindLastBranch(range, ref nearFullKey, ref hasNearFullKey));

                    default:
                        throw new NotSupportedException(direction.ToString());
                    }
                }

                int idx = Optimizator.FindIndex(range, locator, key);

                Debug.Assert(idx >= 0);

                switch (direction)
                {
                case Direction.Backward:
                {
                    if (idx > 0)
                    {
                        nearFullKey    = Branches[idx - 1].Key;
                        hasNearFullKey = true;
                    }
                }
                break;

                case Direction.Forward:
                {
                    if (idx < Branches.Count - 1)
                    {
                        hasNearFullKey = true;
                        nearFullKey    = Branches[idx + 1].Key;
                    }
                }
                break;
                }

                return(Branches[idx]);
            }
Beispiel #11
0
            /// <summary>
            /// The hook.
            /// </summary>
            public KeyValuePair<FullKey, Branch> FindBranch(Locator locator, IData key, Direction direction, ref FullKey nearFullKey, ref bool hasNearFullKey)
            {
                Range range = Optimizator.FindRange(locator);
                
                if (key == null)
                {
                    switch (direction)
                    {
                        case Direction.Forward:
                            return FindFirstBranch(range, ref nearFullKey, ref hasNearFullKey);
                        case Direction.Backward:
                            return FindLastBranch(range, ref nearFullKey, ref hasNearFullKey);
                        default:
                            throw new NotSupportedException(direction.ToString());
                    }
                }

                int idx = Optimizator.FindIndex(range, locator, key);
                Debug.Assert(idx >= 0);

                switch (direction)
                {
                    case Direction.Backward:
                        {
                            if (idx > 0)
                            {
                                nearFullKey = Branches[idx - 1].Key;
                                hasNearFullKey = true;
                            }
                        }
                        break;
                    case Direction.Forward:
                        {
                            if (idx < Branches.Count - 1)
                            {
                                hasNearFullKey = true;
                                nearFullKey = Branches[idx + 1].Key;
                            }
                        }
                        break;
                }

                return Branches[idx];
            }
Beispiel #12
0
            public Branch FindLastBranch(Locator locator, ref FullKey nearFullKey, ref bool hasNearFullKey)
            {
                Range range = Optimizator.FindRange(locator);
                int idx = range.LastIndex; //no matter of range.IsBaseLocator

                if (idx > 0)
                {
                    hasNearFullKey = true;
                    nearFullKey = Branches[idx - 1].Key;
                }

                return Branches[idx].Value;
            }
Beispiel #13
0
            private KeyValuePair<FullKey, Branch> FindLastBranch(Range range, ref FullKey nearFullKey, ref bool hasNearFullKey)
            {
                int idx = range.LastIndex; //no matter of range.IsBaseLocator

                if (idx > 0)
                {
                    hasNearFullKey = true;
                    nearFullKey = Branches[idx - 1].Key;
                }

                return Branches[idx]; 
            }
Beispiel #14
0
            private KeyValuePair<FullKey, Branch> FindFirstBranch(Range range, ref FullKey nearFullKey, ref bool hasNearFullKey)
            {
                int idx;
                if (!range.IsBaseLocator)
                    idx = range.LastIndex;
                else
                {
                    idx = range.FirstIndex;
                    if (idx > 0)
                        idx--;
                }

                if (idx + 1 < Branches.Count)
                {
                    hasNearFullKey = true;
                    nearFullKey = Branches[idx + 1].Key;
                }

                return Branches[idx]; 
            }
Beispiel #15
0
 public bool IsDescendentOf(string prefix)
 {
     return(FullKey.StartsWith(prefix));
 }
Beispiel #16
0
            public IOrderedSet <IData, IData> FindData(Locator locator, Direction direction, ref FullKey nearFullKey, ref bool hasNearFullKey)
            {
                IOrderedSet <IData, IData> data = null;

                Container.TryGetValue(locator, out data);
                if (direction == Direction.None)
                {
                    return(data);
                }

                if (Container.Count == 1 && data != null)
                {
                    return(data);
                }

                IOrderedSet <IData, IData> nearData = null;

                if (direction == Direction.Backward)
                {
                    bool    havePrev = false;
                    Locator prev     = default(Locator);

                    foreach (var kv in Container)
                    {
                        if (kv.Key.CompareTo(locator) < 0)
                        {
                            if (!havePrev || kv.Key.CompareTo(prev) > 0)
                            {
                                prev     = kv.Key;
                                nearData = kv.Value;
                                havePrev = true;
                            }
                        }
                    }

                    if (havePrev)
                    {
                        hasNearFullKey = true;
                        nearFullKey    = new FullKey(prev, nearData.Last.Key);
                    }
                }
                else //if (direction == Direction.Forward)
                {
                    bool    haveNext = false;
                    Locator next     = default(Locator);

                    foreach (var kv in Container)
                    {
                        if (kv.Key.CompareTo(locator) > 0)
                        {
                            if (!haveNext || kv.Key.CompareTo(next) < 0)
                            {
                                next     = kv.Key;
                                nearData = kv.Value;
                                haveNext = true;
                            }
                        }
                    }

                    if (haveNext)
                    {
                        hasNearFullKey = true;
                        nearFullKey    = new FullKey(next, nearData.First.Key);
                    }
                }

                return(data);
            }
Beispiel #17
0
        /// <summary>
        /// The hook.
        /// </summary>
        public IDataContainer FindData(ILocator originalLocator, ILocator locator, IData key, Direction direction, out FullKey nearFullKey, out bool hasNearFullKey, ref FullKey lastVisitedFullKey)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("WTree");
            }

            nearFullKey    = default(FullKey);
            hasNearFullKey = false;

            var branch = RootBranch;

            Monitor.Enter(branch);

            Params param;

            if (key != null)
            {
                param = new Params(WalkMethod.Cascade, WalkAction.None, null, true, locator, key);
            }
            else
            {
                switch (direction)
                {
                case Direction.Forward:
                    param = new Params(WalkMethod.CascadeFirst, WalkAction.None, null, true, locator);
                    break;

                case Direction.Backward:
                    param = new Params(WalkMethod.CascadeLast, WalkAction.None, null, true, locator);
                    break;

                default:
                    throw new NotSupportedException(direction.ToString());
                }
            }

            branch.Fall(Depth + 1, new Token(CacheSemaphore, CancellationToken.None), param);
            branch.WaitFall();

            switch (direction)
            {
            case Direction.Forward:
            {
                while (branch.NodeType == NodeType.Internal)
                {
                    KeyValuePair <FullKey, Branch> newBranch = ((InternalNode)branch.Node).FindBranch(locator, key, direction, ref nearFullKey, ref hasNearFullKey);

                    Monitor.Enter(newBranch.Value);
                    newBranch.Value.WaitFall();
                    Debug.Assert(!newBranch.Value.Cache.Contains(originalLocator));
                    Monitor.Exit(branch);

                    branch = newBranch.Value;
                }
            }
            break;

            case Direction.Backward:
            {
                int depth = Depth;
                KeyValuePair <FullKey, Branch> newBranch = default(KeyValuePair <FullKey, Branch>);
                while (branch.NodeType == NodeType.Internal)
                {
                    InternalNode node = (InternalNode)branch.Node;
                    newBranch = node.Branches[node.Branches.Count - 1];

                    int cmp = newBranch.Key.Locator.CompareTo(lastVisitedFullKey.Locator);
                    if (cmp == 0)
                    {
                        if (lastVisitedFullKey.Key == null)
                        {
                            cmp = -1;
                        }
                        else
                        {
                            cmp = newBranch.Key.Locator.KeyComparer.Compare(newBranch.Key.Key, lastVisitedFullKey.Key);
                        }
                    }
                    //else
                    //{
                    //    Debug.WriteLine("");
                    //}

                    //newBranch.Key.CompareTo(lastVisitedFullKey) >= 0
                    if (cmp >= 0)
                    {
                        newBranch = node.FindBranch(locator, key, direction, ref nearFullKey, ref hasNearFullKey);
                    }
                    else
                    {
                        if (node.Branches.Count >= 2)
                        {
                            hasNearFullKey = true;
                            nearFullKey    = node.Branches[node.Branches.Count - 2].Key;
                        }
                    }

                    Monitor.Enter(newBranch.Value);
                    depth--;
                    newBranch.Value.WaitFall();
                    if (newBranch.Value.Cache.Contains(originalLocator))
                    {
                        newBranch.Value.Fall(depth + 1, new Token(CacheSemaphore, CancellationToken.None), new Params(WalkMethod.Current, WalkAction.None, null, true, originalLocator));
                        newBranch.Value.WaitFall();
                    }
                    Debug.Assert(!newBranch.Value.Cache.Contains(originalLocator));
                    Monitor.Exit(branch);

                    branch = newBranch.Value;
                }

                //if (lastVisitedFullKey.Locator.Equals(newBranch.Key.Locator) &&
                //    (lastVisitedFullKey.Key != null && lastVisitedFullKey.Locator.KeyEqualityComparer.Equals(lastVisitedFullKey.Key, newBranch.Key.Key)))
                //{
                //    Monitor.Exit(branch);
                //    return null;
                //}

                lastVisitedFullKey = newBranch.Key;
            }
            break;

            default:
                throw new NotSupportedException(direction.ToString());
            }

            IDataContainer data = ((LeafNode)branch.Node).FindData(originalLocator, direction, ref nearFullKey, ref hasNearFullKey);

            Monitor.Exit(branch);

            return(data);
        }
Beispiel #18
0
 public override int GetHashCode()
 {
     return(7 * FullKey.GetHashCode());
 }
Beispiel #19
0
        /// <summary>
        /// The hook.
        /// </summary>
        public IOrderedSet<IData, IData> FindData(Locator originalLocator, Locator locator, IData key, Direction direction, out FullKey nearFullKey, out bool hasNearFullKey, ref FullKey lastVisitedFullKey)
        {
            if (disposed)
                throw new ObjectDisposedException("WTree");

            nearFullKey = default(FullKey);
            hasNearFullKey = false;

            var branch = RootBranch;
            Monitor.Enter(branch);

            if (!isRootCacheLoaded)
                LoadRootCache();

            Params param;
            if (key != null)
                param = new Params(WalkMethod.Cascade, WalkAction.None, null, true, locator, key);
            else
            {
                switch (direction)
                {
                    case Direction.Forward:
                        param = new Params(WalkMethod.CascadeFirst, WalkAction.None, null, true, locator);
                        break;
                    case Direction.Backward:
                        param = new Params(WalkMethod.CascadeLast, WalkAction.None, null, true, locator);
                        break;
                    default:
                        throw new NotSupportedException(direction.ToString());
                }
            }

            branch.Fall(Depth + 1, new Token(CacheSemaphore, CancellationToken.None), param);
            branch.WaitFall();

            switch (direction)
            {
                case Direction.Forward:
                    {
                        while (branch.NodeType == NodeType.Internal)
                        {
                            KeyValuePair<FullKey, Branch> newBranch = ((InternalNode)branch.Node).FindBranch(locator, key, direction, ref nearFullKey, ref hasNearFullKey);

                            Monitor.Enter(newBranch.Value);
                            newBranch.Value.WaitFall();
                            Debug.Assert(!newBranch.Value.Cache.Contains(originalLocator));
                            Monitor.Exit(branch);

                            branch = newBranch.Value;
                        }
                    }
                    break;
                case Direction.Backward:
                    {
                        int depth = Depth;
                        KeyValuePair<FullKey, Branch> newBranch = default(KeyValuePair<FullKey, Branch>);
                        while (branch.NodeType == NodeType.Internal)
                        {
                            InternalNode node = (InternalNode)branch.Node;
                            newBranch = node.Branches[node.Branches.Count - 1];

                            int cmp = newBranch.Key.Locator.CompareTo(lastVisitedFullKey.Locator);
                            if (cmp == 0)
                            {
                                if (lastVisitedFullKey.Key == null)
                                    cmp = -1;
                                else
                                    cmp = newBranch.Key.Locator.KeyComparer.Compare(newBranch.Key.Key, lastVisitedFullKey.Key);
                            }
                            //else
                            //{
                            //    Debug.WriteLine("");
                            //}

                            //newBranch.Key.CompareTo(lastVisitedFullKey) >= 0
                            if (cmp >= 0)
                                newBranch = node.FindBranch(locator, key, direction, ref nearFullKey, ref hasNearFullKey);
                            else
                            {
                                if (node.Branches.Count >= 2)
                                {
                                    hasNearFullKey = true;
                                    nearFullKey = node.Branches[node.Branches.Count - 2].Key;
                                }
                            }

                            Monitor.Enter(newBranch.Value);
                            depth--;
                            newBranch.Value.WaitFall();
                            if (newBranch.Value.Cache.Contains(originalLocator))
                            {
                                newBranch.Value.Fall(depth + 1, new Token(CacheSemaphore, CancellationToken.None), new Params(WalkMethod.Current, WalkAction.None, null, true, originalLocator));
                                newBranch.Value.WaitFall();
                            }
                            Debug.Assert(!newBranch.Value.Cache.Contains(originalLocator));
                            Monitor.Exit(branch);

                            branch = newBranch.Value;
                        }

                        //if (lastVisitedFullKey.Locator.Equals(newBranch.Key.Locator) &&
                        //    (lastVisitedFullKey.Key != null && lastVisitedFullKey.Locator.KeyEqualityComparer.Equals(lastVisitedFullKey.Key, newBranch.Key.Key)))
                        //{
                        //    Monitor.Exit(branch);
                        //    return null;
                        //}

                        lastVisitedFullKey = newBranch.Key;
                    }
                    break;
                default:
                    throw new NotSupportedException(direction.ToString());
            }

            IOrderedSet<IData, IData> data = ((LeafNode)branch.Node).FindData(originalLocator, direction, ref nearFullKey, ref hasNearFullKey);

            Monitor.Exit(branch);

            return data;
        }