1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation.
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.
11 * You must not remove this notice, or any other, from this software.
14 * ***************************************************************************/
16 using System.Collections.Generic;
19 namespace Microsoft.Scripting.Ast.Compiler {
21 namespace System.Linq.Expressions.Compiler {
25 /// A simple dictionary of queues, keyed off a particular type
26 /// This is useful for storing free lists of variables
28 internal sealed class KeyedQueue<K, V> {
29 private readonly Dictionary<K, Queue<V>> _data;
31 internal KeyedQueue() {
32 _data = new Dictionary<K, Queue<V>>();
35 internal void Enqueue(K key, V value) {
37 if (!_data.TryGetValue(key, out queue)) {
38 _data.Add(key, queue = new Queue<V>());
43 internal V Dequeue(K key) {
45 if (!_data.TryGetValue(key, out queue)) {
46 throw Error.QueueEmpty();
48 V result = queue.Dequeue();
49 if (queue.Count == 0) {
55 internal bool TryDequeue(K key, out V value) {
57 if (_data.TryGetValue(key, out queue) && queue.Count > 0) {
58 value = queue.Dequeue();
59 if (queue.Count == 0) {
68 internal V Peek(K key) {
70 if (!_data.TryGetValue(key, out queue)) {
71 throw Error.QueueEmpty();
76 internal int GetCount(K key) {
78 if (!_data.TryGetValue(key, out queue)) {
84 internal void Clear() {