Exemplo n.º 1
0
        public V Put(V value, params K[] cascadedKeys)
        {
            if (value == null)
            {
                return(Remove(cascadedKeys));
            }

            if (cascadedKeys == null || cascadedKeys.Length == 0)
            {
                throw new ArgumentException("cascadedKeys is null or empty");
            }

            lock (_lock)
            {
                CascadedKeyValuePairNode <K, V> node = GetOrAddNode(cascadedKeys);
                CascadedKeyValuePair <K, V>     pair = node.Pair;
                V oldValue = pair == null ? default(V) : pair.Value;
                if (pair == null)
                {
                    pair      = new CascadedKeyValuePair <K, V>(value, cascadedKeys);
                    node.Pair = pair;
                    Interlocked.Increment(ref _count);
                }
                else
                {
                    pair.Value = value;
                }

                return(oldValue);
            }
        }
Exemplo n.º 2
0
        public V Remove(params K[] cascadedKeys)
        {
            if (cascadedKeys == null || cascadedKeys.Length == 0)
            {
                throw new ArgumentException("cascadedKeys is null or empty");
            }

            lock (_lock)
            {
                Stack <CascadedKeyValuePairNode <K, V> > nodes = new Stack <CascadedKeyValuePairNode <K, V> >();
                nodes.Push(_rootNode);
                CascadedKeyValuePairNode <K, V> node = _rootNode;
                int i = 0;
                foreach (K key in cascadedKeys)
                {
                    node.Children.TryGetValue(key, out node);
                    if (node == null)
                    {
                        break;
                    }

                    nodes.Push(node);
                    i++;
                }

                V value = default(V);
                if (i == cascadedKeys.Length)
                {
                    node = nodes.Pop();
                    CascadedKeyValuePair <K, V> pair = node.Pair;
                    if (pair != null)
                    {
                        value = pair.Value;
                        Interlocked.Decrement(ref _count);
                    }

                    if (!node.Children.IsEmpty)
                    {
                        node.Pair = null;
                        return(value);
                    }

                    nodes.Peek().Children.TryRemove(node.Key, out node);
                }

                for (node = nodes.Pop(); node != _rootNode; node = nodes.Pop())
                {
                    if (node.Pair == null && node.Children.IsEmpty)
                    {
                        nodes.Peek().Children.TryRemove(node.Key, out node);
                    }
                }

                return(value);
            }
        }
Exemplo n.º 3
0
        public V Get(params K[] cascadedKeys)
        {
            if (cascadedKeys == null || cascadedKeys.Length == 0)
            {
                throw new ArgumentException("cascadedKeys is null or empty");
            }

            CascadedKeyValuePairNode <K, V> node = GetNode(cascadedKeys);

            if (node == null)
            {
                return(default(V));
            }

            CascadedKeyValuePair <K, V> pair = node.Pair;

            return(pair == null ? default(V) : pair.Value);
        }
Exemplo n.º 4
0
        public V GetOrAdd(Func <K[], V> valueFacTory, params K[] cascadedKeys)
        {
            if (valueFacTory == null)
            {
                throw new ArgumentNullException("valueFacTory is null");
            }

            if (cascadedKeys == null || cascadedKeys.Length == 0)
            {
                throw new ArgumentException("cascadedKeys is null or empty");
            }

            V value = Get(cascadedKeys);

            if (value != null)
            {
                return(value);
            }

            lock (_lock)
            {
                CascadedKeyValuePairNode <K, V> node = GetOrAddNode(cascadedKeys);
                CascadedKeyValuePair <K, V>     pair = node.Pair;
                if (pair != null)
                {
                    return(pair.Value);
                }

                value = valueFacTory(cascadedKeys);
                if (value == null)
                {
                    throw new ArgumentException("valueFacTory return null");
                }

                pair      = new CascadedKeyValuePair <K, V>(value, cascadedKeys);
                node.Pair = pair;
                Interlocked.Increment(ref _count);
                return(value);
            }
        }