Ejemplo n.º 1
0
        public void Add(int key, T value)
        {
            var item = GetRef(key);

            if (item != null)
            {
                Console.WriteLine($"Refreshing: {item.key}");
                // Key is already present on the array
                var oldNewest = Newest;
                Newest = item;

                item.next.last = item.last;
                item.last.next = item.next;

                if (Oldest == item)
                {
                    Oldest      = item.last;
                    Oldest.next = Newest;
                }

                item.last           = Oldest;
                item.next           = oldNewest;
                item.value          = value;
                oldNewest.last.next = item;
                oldNewest.last      = item;
            }
            else
            {
                var newItem = new LRUItem <T>()
                {
                    key   = key,
                    value = value
                };


                if (_items.Length + 1 > _cacheSize)
                {
                    Console.WriteLine($"Adding {newItem.key} to a full Array (replacing {Oldest.key})");
                    // Adding items on a full array
                    var oldNewest = Newest;
                    var oldOldest = Oldest;
                    Newest = newItem;

                    newItem.last        = oldOldest.last;
                    newItem.next        = oldNewest;
                    oldOldest.last.next = Newest;
                    oldNewest.last      = Newest;
                    Oldest = oldOldest.last;

                    var index = Array.IndexOf(_items, oldOldest);

                    _items[index] = Newest;
                }
                else
                {
                    Console.WriteLine($"Adding {newItem.key}");
                    if (_items.Length == 0)
                    {
                        // Adding items when array is empty
                        Newest       = newItem;
                        Oldest       = newItem;
                        newItem.next = newItem;
                        newItem.last = newItem;
                    }

                    // Adding items on a non full array
                    var oldNewest = Newest;

                    newItem.last = Oldest;
                    newItem.next = oldNewest;

                    oldNewest.last.next = newItem;
                    oldNewest.last      = newItem;

                    Newest = newItem;

                    var newItemPosition = _items.Length;

                    _items = _items.EnsureCapacity(_items.Length + 1);

                    _items[newItemPosition] = newItem;
                }

                _items.QuickSort();
            }
        }
Ejemplo n.º 2
0
 private void RenewCache(LRUItem <T> item)
 {
     Add(item.key, item.value);
 }