1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.Collections.Generic;
6
using System.Diagnostics;
8
namespace ICSharpCode.AvalonEdit.Utils
11
/// Double-ended queue.
13
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
15
public sealed class Deque<T> : ICollection<T>
17
T[] arr = Empty<T>.Array;
35
/// Gets/Sets an element inside the deque.
37
public T this[int index] {
39
ThrowUtil.CheckInRangeInclusive(index, "index", 0, size - 1);
40
return arr[(head + index) % arr.Length];
43
ThrowUtil.CheckInRangeInclusive(index, "index", 0, size - 1);
44
arr[(head + index) % arr.Length] = value;
49
/// Adds an element to the end of the deque.
51
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "PushBack")]
52
public void PushBack(T item)
54
if (size == arr.Length)
55
SetCapacity(Math.Max(4, arr.Length * 2));
57
if (tail == arr.Length) tail = 0;
62
/// Pops an element from the end of the deque.
67
throw new InvalidOperationException();
69
tail = arr.Length - 1;
73
arr[tail] = default(T); // allow GC to collect the element
79
/// Adds an element to the front of the deque.
81
public void PushFront(T item)
83
if (size == arr.Length)
84
SetCapacity(Math.Max(4, arr.Length * 2));
86
head = arr.Length - 1;
94
/// Pops an element from the end of the deque.
99
throw new InvalidOperationException();
101
arr[head] = default(T); // allow GC to collect the element
103
if (head == arr.Length) head = 0;
108
void SetCapacity(int capacity)
110
T[] newArr = new T[capacity];
113
tail = (size == capacity) ? 0 : size;
118
public IEnumerator<T> GetEnumerator()
121
for (int i = head; i < tail; i++)
124
for (int i = head; i < arr.Length; i++)
126
for (int i = 0; i < tail; i++)
131
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
133
return this.GetEnumerator();
136
bool ICollection<T>.IsReadOnly {
137
get { return false; }
140
void ICollection<T>.Add(T item)
146
public bool Contains(T item)
148
EqualityComparer<T> comparer = EqualityComparer<T>.Default;
149
foreach (T element in this)
150
if (comparer.Equals(item, element))
156
public void CopyTo(T[] array, int arrayIndex)
159
throw new ArgumentNullException("array");
161
Array.Copy(arr, head, array, arrayIndex, tail - head);
163
int num1 = arr.Length - head;
164
Array.Copy(arr, head, array, arrayIndex, num1);
165
Array.Copy(arr, 0, array, arrayIndex + num1, tail);
169
bool ICollection<T>.Remove(T item)
171
throw new NotSupportedException();