public Node RemoveEnd()
        {
            Node oldTail = _tail;
            if (_tail == null) // no nodes
            {
                return null;
            }
            else if (_head == _tail) //one node
            {
                _head = null;
                _tail = null;
            }
            else //two or more nodes
            {
                Node secondLast = _tail.Parent;
                _tail = secondLast;
                _tail.SetChild(null);
            }

            Size--;

            oldTail.SetChild(null);
            oldTail.SetParent(null);
            return oldTail;
        }
        public void GetTest()
        {
            LRUDoublyLinkedList dll = new LRUDoublyLinkedList();
            Assert.IsNull(dll.Get(0));
            Assert.IsNull(dll.Get(5));
            try
            {
                dll.Get(-1);
                Assert.Fail("Should throw an exception");
            }
            catch (InvalidElementIndexException) {}

            Node n0 = new Node(null, null, new Element(new byte[0]), 0);
            dll.Insert(n0);
            Assert.AreEqual(dll.Get(0), n0);
            Assert.IsNull(dll.Get(1));
            Assert.IsNull(dll.Get(2));

            Node n1 = new Node(null, null, new Element(new byte[0]), 1);
            dll.Insert(n1);
            Assert.AreEqual(dll.Get(0), n1);
            Assert.AreEqual(dll.Get(1), n0);
            Assert.AreEqual(dll.Get(0), n0);
            Assert.IsNull(dll.Get(2));
        }
Esempio n. 3
0
        public Node(Node child, Node parent, Element element, int eleIdx)
        {
            Child = child;
            Parent = parent;

            Element = element;
            ElementIndex = eleIdx;
        }
 public void NodeCtorTest()
 {
     Element ele = new Element(new byte[0]);
     int elementIdx = 0;
     Node parent = new Node(null, null, ele, elementIdx);
     Node child = new Node(null, null, ele, elementIdx);
     NodeCtorTestAssert(elementIdx, ele, parent, child);
 }
        public void SetChildTest()
        {
            Node child = new Node(null, null, new Element(new byte[0]), 0);

            Node n = new Node(null, null, new Element(new byte[0]), 1);

            n.SetChild(child);

            Assert.AreEqual(child, n.Child);
        }
        public void SetParentTest()
        {
            Node parent = new Node(null, null, new Element(new byte[0]), 0);

            Node n = new Node(null, null, new Element(new byte[0]), 1);

            n.SetParent(parent);

            Assert.AreEqual(parent, n.Parent);
        }
 public void AddToCache(Element element, int elementIdx)
 {
     Node cachedNode;
     if (_index.TryGetValue(elementIdx, out cachedNode))
     {
         cachedNode.SetElement(element);
         _cacheList.MoveToHead(cachedNode);
     }
     else
     {
         Node node = new Node(null, null, element, elementIdx);
         _cacheList.Insert(node);
         _index.Add(elementIdx, node);
     }
     if (_cacheList.Size > _cacheSize)
         RemoveLast();
 }
 public void MoveToHead(Node n)
 {
     if (n == _head)
     {
         //already the _head
     }
     else if (n == _tail) // the node to move is the tail
     {
         Debug.Assert(_tail.Parent != null);
         _tail.Parent.SetChild(null);
         _tail = _tail.Parent;
         InsertAtHead(n);
     }
     else // the node is in the middle
     {
         n.Parent.SetChild(n.Child);
         n.Child.SetParent(n.Parent);
         InsertAtHead(n);
     }
 }
        public void MoveToHeadTest()
        {
            LRUDoublyLinkedList dll = new LRUDoublyLinkedList();

            Node n0 = new Node(null, null, new Element(new byte[0]), 0);
            dll.Insert(n0);
            dll.MoveToHead(n0);
            Assert.AreEqual(n0, dll.Get(0));
            Assert.AreEqual(1, dll.Size);

            Node n1 = new Node(null, null, new Element(new byte[0]), 1);
            dll.Insert(n1);
            dll.MoveToHead(n0);
            Assert.AreEqual(n0, dll.Get(0));
            Assert.AreEqual(2, dll.Size);

            Node n2 = new Node(null, null, new Element(new byte[0]), 2);
            dll.Insert(n2);
            dll.MoveToHead(n0);
            Assert.AreEqual(n0, dll.Get(0));
            Assert.AreEqual(3, dll.Size);
        }
 public void Insert(Node n)
 {
     InsertAtHead(n);
     Size++;
 }
        public void RemoveEndTest()
        {
            LRUDoublyLinkedList dll = new LRUDoublyLinkedList();

            Assert.IsNull(dll.RemoveEnd());

            Node n0 = new Node(null, null, new Element(new byte[0]), 0);
            dll.Insert(n0);
            dll.RemoveEnd();
            Assert.IsNull(dll.Get(0));
            Assert.AreEqual(0, dll.Size);

            dll.Insert(n0);
            Node n1 = new Node(null, null, new Element(new byte[0]), 1);
            dll.Insert(n1);
            dll.RemoveEnd();

            Assert.AreEqual(dll.Get(0), n1);
            Assert.IsNull(dll.Get(1));
            Assert.AreEqual(1, dll.Size);
        }
        private void InsertOneTest()
        {
            LRUDoublyLinkedList dll = new LRUDoublyLinkedList();

            Node n0 = new Node(null, null, new Element(new byte[0]), 0);
            Assert.AreEqual(0, dll.Size);
            dll.Insert(n0);
            Assert.AreEqual(n0, dll.Get(0));
            Assert.AreEqual(1, dll.Size);
        }
Esempio n. 13
0
 public void SetParent(Node parent)
 {
     Parent = parent;
 }
Esempio n. 14
0
 public void SetChild(Node child)
 {
     Child = child;
 }
Esempio n. 15
0
        private static void NodeCtorTestAssert(int elementIdx, Element ele, Node parent, Node child)
        {
            Node n = new Node(child, parent, ele, elementIdx);

            Assert.AreEqual(n.Element, ele);
            Assert.AreEqual(n.ElementIndex, elementIdx);
            Assert.AreEqual(n.Child, child);
            Assert.AreEqual(n.Parent, parent);
        }
        private void InsertAtHead(Node n)
        {
            n.SetParent(null);
            n.SetChild(_head);

            if (_head == null) // no nodes yet
            {
                Debug.Assert(_tail == null);
                _tail = n;
            }
            if (_head != null) // at least one node in the list
            {
                _head.SetParent(n);
            }
            _head = n;
        }
        private void InsertMoreThanTwoTest()
        {
            LRUDoublyLinkedList dll = new LRUDoublyLinkedList();

            Node n0 = new Node(null, null, new Element(new byte[0]), 0);
            dll.Insert(n0);
            Node n1 = new Node(null, null, new Element(new byte[0]), 1);
            dll.Insert(n1);
            Node n2 = new Node(null, null, new Element(new byte[0]), 2);
            dll.Insert(n2);

            Assert.AreEqual(n2, dll.Get(0));
            Assert.AreEqual(n1, dll.Get(1));
            Assert.AreEqual(n0, dll.Get(2));
            Assert.AreEqual(3, dll.Size);
        }