Пример #1
0
 public LRUCache(int capacity)
 {
     _capacity = capacity;
     _nodes    = new Dictionary <int, MutableNode>(capacity);
     First     = new MutableNode(int.MinValue, int.MinValue);
     Last      = new MutableNode(int.MinValue, int.MinValue);
 }
Пример #2
0
 private void MoveToFront(MutableNode node)
 {
     if (node == First.Next)
     {
         return;
     }
     node.Previous.Next = node.Next;
     node.Next.Previous = node.Previous;
     node.Previous      = First;
     node.Next          = First.Next;
     node.Next.Previous = node;
     First.Next         = node;
 }
        public void MutableNode_fails_on_replacing_unknown_child()
        {
            // ARRANGE
            // none of the child nodes are added to the root node.

            var child = new MutableNode <string, int>("a");
            var node  = MutableNode <string, int> .CreateRoot();

            var secondChild = new MutableNode <string, int>("a");

            // ACT

            var result = Assert.Throws <InvalidOperationException>(() => node.ReplaceChild(child, secondChild));

            // ASSERT

            Assert.Equal("The node (id=a) doesn't substutite any of the existing child nodes", result.Message);
            Assert.False(node.HasChildNodes);
        }
        public void MutableNode_removes_child_from_current_instance()
        {
            // ARRANGE

            var child = new MutableNode <string, int>("a");
            var node  = MutableNode <string, int> .CreateRoot().AddChild(child);

            // ACT

            var result = node.RemoveChild(child);

            // ASSERT

            Assert.Same(node, result);
            Assert.False(node.HasChildNodes);
            Assert.False(result.ChildNodes.Any());

            var(found, addedChild) = node.TryGetChildNode("a");

            Assert.False(found);
        }
        public void MutableNode_adds_child_to_current_instance()
        {
            // ARRANGE

            var child = new MutableNode <string, int>("a");
            var node  = MutableNode <string, int> .CreateRoot();

            // ACT

            var result = node.AddChild(child);

            // ASSERT

            Assert.Same(node, result);
            Assert.True(node.HasChildNodes);
            Assert.Same(child, result.ChildNodes.Single());

            var(found, addedChild) = node.TryGetChildNode("a");

            Assert.True(found);
            Assert.Same(child, addedChild);
        }
        public bool RemoveNode(HierarchyPath <TKey> path, bool recurse)
        {
            if (path.IsRoot)
            {
                if (!recurse && this.rootNode.HasChildNodes)
                {
                    // is recurse is not set, the root node can be exhanged if the root has no child nodes

                    return(false);
                }

                this.rootNode = MutableNode <TKey, TValue> .CreateRoot();

                return(true);
            }
            else
            {
                // this isn't a special case.
                // use the hierachy writer for inner nodes
                var writer = new RemoveNodeHierarchyWriter <TKey, MutableNode <TKey, TValue> >();
                var result = writer.RemoveNode(this.rootNode, path, recurse, out var nodeWasRemoved);
                return(nodeWasRemoved);
            }
        }
        public void MutableNode_fails_on_Replace_if_keys_are_different()
        {
            // ARRANGE

            var child = new MutableNode <string, int>("a");
            var node  = MutableNode <string, int> .CreateRoot().AddChild(child);

            var secondChild = new MutableNode <string, int>("b");

            // ACT

            var result = Assert.Throws <InvalidOperationException>(() => node.ReplaceChild(child, secondChild));

            // ASSERT

            Assert.Equal("Key of child to replace (key='a') and new child (key='b') must be equal", result.Message);
            Assert.True(node.HasChildNodes);
            Assert.Same(child, node.ChildNodes.Single());

            var(found, addedChild) = node.TryGetChildNode("a");

            Assert.True(found);
            Assert.Same(child, addedChild);
        }
        private MutableHierarchy(bool pruneOnUnsetValue)
        {
            this.rootNode = MutableNode <TKey, TValue> .CreateRoot();

            this.pruneOnUnsetValue = pruneOnUnsetValue;
        }