Пример #1
0
        public RefData Add(TKey key, TValue value, int weight, bool commit = true)
        {
            this.CheckWeight(weight, null);

            if (0 > weight)
            {
                weight = 0;
            }

            var refData = new RefData(key, value, weight);

            if (commit)
            {
                refData.AddRef();
                refData.ResetGeneration();
                this.foreground.Add(key, refData);
            }
            else
            {
                refData.SetBackgroundNode(this.background.AddFirst(refData));
            }

            this.weight += weight;
            return(refData);
        }
Пример #2
0
        public bool Remove(TKey key)
        {
            RefData refData = null;

            switch (this.TryGetValue(key, out refData))
            {
            default:
            case Flag.Null:
                return(false);

            case Flag.Committed:
                refData.Discard();
                this.foreground.Remove(key);
                break;

            case Flag.Uncommitted:
                this.background.Remove(refData.BackgroundNode);
                refData.SetBackgroundNode(null);
                break;
            }
            this.DecreaseWeight(refData);
            return(true);
        }
Пример #3
0
        protected Flag TryGetValue(TKey key, out RefData refData)
        {
            if (this.foreground.TryGetValue(key, out refData))
            {
                return(Flag.Committed);
            }

            LinkedListNode <RefData> node = this.background.First;

            while (null != node)
            {
                refData = node.Value;
                if (refData.Key.Equals(key))
                {
                    refData.SetBackgroundNode(node);
                    return(Flag.Uncommitted);
                }
                node = node.Next;
            }

            refData = null;
            return(Flag.Null);
        }