public override void BuildTree(CacheNode <TKey, TValue> root, int outPath)
 {
     for (int i = 1; i < _nodes.Length; i++)
     {
         int depth = 0;
         var node  = _nodes[i];
         node._leftFix  = null;
         node._rightFix = null;
         root.AddNode(node, outPath, ref depth);
     }
 }
        public void Add(TKey key, TValue value)
        {
            bool lockTaken = false;

            try
            {
                _writeLock.TryEnter(_writeTimeout, ref lockTaken);
                if (lockTaken)
                {
                    if (Settings.CheckCycles && _root != null)
                    {
                        if (!_root.CheckTree(_currentPath, _cacheNodeCount + 1))
                        {
                            Debugger.Break();
                        }
                    }

                    if (_root == null)
                    {
                        _root = new CacheNode <TKey, TValue>(key, value, _maxFixedBranchDepth, _comparer, Channel,
                                                             DecCount, _frequencyCalc);
                    }
                    else
                    {
                        _root.AddNode(key, value, _maxFixedBranchDepth, _currentPath);
                    }

                    if (Settings.CheckCycles && _root != null)
                    {
                        if (!_root.CheckTree(_currentPath, _cacheNodeCount + 1))
                        {
                            Debugger.Break();
                        }
                    }
                    Interlocked.Increment(ref _cacheNodeCount);
                }
                else
                {
#if DEBUG
                    if (NodeLostEvent != null)
                    {
                        NodeLostEvent(this, new EventArgs());
                    }
#endif
                }
            }
            finally
            {
                if (lockTaken)
                {
                    _writeLock.Exit();
                }
            }
        }