Lean  $LEAN_TAG$
FixedSizeHashQueue.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15 */
16 
17 using System.Collections;
18 using System.Collections.Generic;
19 
20 namespace QuantConnect.Util
21 {
22  /// <summary>
23  /// Provides an implementation of an add-only fixed length, unique queue system
24  /// </summary>
25  public class FixedSizeHashQueue<T> : IEnumerable<T>
26  {
27  private readonly int _size;
28  private readonly Queue<T> _queue;
29  private readonly HashSet<T> _hash;
30 
31  /// <summary>
32  /// Initializes a new instance of the <see cref="FixedSizeHashQueue{T}"/> class
33  /// </summary>
34  /// <param name="size">The maximum number of items to hold</param>
35  public FixedSizeHashQueue(int size)
36  {
37  _size = size;
38  _queue = new Queue<T>(size);
39  _hash = new HashSet<T>();
40  }
41 
42  /// <summary>
43  /// Returns true if the item was added and didn't already exists
44  /// </summary>
45  public bool Add(T item)
46  {
47  if (_hash.Add(item))
48  {
49  _queue.Enqueue(item);
50  if (_queue.Count > _size)
51  {
52  // remove the item from both
53  _hash.Remove(_queue.Dequeue());
54  }
55  return true;
56  }
57  return false;
58  }
59 
60  /// <summary>
61  /// Tries to inspect the first item in the queue
62  /// </summary>
63  public bool TryPeek(out T item)
64  {
65  return _queue.TryPeek(out item);
66  }
67 
68  /// <summary>
69  /// Dequeues and returns the next item in the queue
70  /// </summary>
71  public T Dequeue()
72  {
73  var item = _queue.Dequeue();
74  _hash.Remove(item);
75  return item;
76  }
77 
78  /// <summary>
79  /// Returns true if the specified item exists in the collection
80  /// </summary>
81  public bool Contains(T item)
82  {
83  return _hash.Contains(item);
84  }
85 
86  /// <summary>
87  /// Returns an enumerator that iterates through the collection.
88  /// </summary>
89  /// <returns>
90  /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
91  /// </returns>
92  /// <filterpriority>1</filterpriority>
93  public IEnumerator<T> GetEnumerator()
94  {
95  return _queue.GetEnumerator();
96  }
97 
98  /// <summary>
99  /// Returns an enumerator that iterates through a collection.
100  /// </summary>
101  /// <returns>
102  /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
103  /// </returns>
104  /// <filterpriority>2</filterpriority>
105  IEnumerator IEnumerable.GetEnumerator()
106  {
107  return GetEnumerator();
108  }
109  }
110 }