Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Scripting.Core / Compiler / KeyedQueue.cs
1 /* ****************************************************************************
2  *
3  * Copyright (c) Microsoft Corporation. 
4  *
5  * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
6  * copy of the license can be found in the License.html file at the root of this distribution. If 
7  * you cannot locate the  Apache License, Version 2.0, please send an email to 
8  * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
9  * by the terms of the Apache License, Version 2.0.
10  *
11  * You must not remove this notice, or any other, from this software.
12  *
13  *
14  * ***************************************************************************/
15
16 using System.Collections.Generic;
17
18 #if !FEATURE_CORE_DLR
19 namespace Microsoft.Scripting.Ast.Compiler {
20 #else
21 namespace System.Linq.Expressions.Compiler {
22 #endif
23     
24     /// <summary>
25     /// A simple dictionary of queues, keyed off a particular type
26     /// This is useful for storing free lists of variables
27     /// </summary>
28     internal sealed class KeyedQueue<K, V> {
29         private readonly Dictionary<K, Queue<V>> _data;
30
31         internal KeyedQueue() {
32             _data = new Dictionary<K, Queue<V>>();
33         }
34
35         internal void Enqueue(K key, V value) {
36             Queue<V> queue;
37             if (!_data.TryGetValue(key, out queue)) {
38                 _data.Add(key, queue = new Queue<V>());
39             }
40             queue.Enqueue(value);
41         }
42
43         internal V Dequeue(K key) {
44             Queue<V> queue;
45             if (!_data.TryGetValue(key, out queue)) {
46                 throw Error.QueueEmpty();
47             }
48             V result = queue.Dequeue();
49             if (queue.Count == 0) {
50                 _data.Remove(key);
51             }
52             return result;
53         }
54
55         internal bool TryDequeue(K key, out V value) {
56             Queue<V> queue;
57             if (_data.TryGetValue(key, out queue) && queue.Count > 0) {
58                 value = queue.Dequeue();
59                 if (queue.Count == 0) {
60                     _data.Remove(key);
61                 }
62                 return true;
63             }
64             value = default(V);
65             return false;
66         }
67
68         internal V Peek(K key) {
69             Queue<V> queue;
70             if (!_data.TryGetValue(key, out queue)) {
71                 throw Error.QueueEmpty();
72             }
73             return queue.Peek();
74         }
75
76         internal int GetCount(K key) {
77             Queue<V> queue;
78             if (!_data.TryGetValue(key, out queue)) {
79                 return 0;
80             }
81             return queue.Count;
82         }
83
84         internal void Clear() {
85             _data.Clear();
86         }
87     }
88 }