Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
FastListStruct.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections;
5 using System.Collections.Generic;
6 using System.Runtime.InteropServices;
7 
8 namespace SiliconStudio.Core.Collections
9 {
10  public struct FastListStruct<T> : IEnumerable<T>
11  {
12  public int Count;
13 
14  /// <summary>
15  /// Gets the items.
16  /// </summary>
17  public T[] Items;
18 
19  public T this[int index]
20  {
21  get { return Items[index]; }
22  set
23  {
24  Items[index] = value;
25  }
26  }
27 
28  public FastListStruct(FastList<T> fastList)
29  {
30  this.Count = fastList.Count;
31  this.Items = fastList.Items;
32  }
33 
34  public FastListStruct(T[] array)
35  {
36  this.Count = array.Length;
37  this.Items = array;
38  }
39 
40  public FastListStruct(int capacity)
41  {
42  this.Count = 0;
43  this.Items = new T[capacity];
44  }
45 
46  public void Add(T item)
47  {
48  if (this.Count == this.Items.Length)
49  this.EnsureCapacity(this.Count + 1);
50  this.Items[this.Count++] = item;
51  }
52 
53  public void Insert(int index, T item)
54  {
55  if (Count == Items.Length)
56  {
57  EnsureCapacity(Count + 1);
58  }
59  if (index < Count)
60  {
61  for (int i = Count; i > index; --i)
62  {
63  Items[i] = Items[i - 1];
64  }
65  }
66  Items[index] = item;
67  Count++;
68  }
69 
70  public void RemoveAt(int index)
71  {
72  Count--;
73  if (index < Count)
74  {
75  Array.Copy(Items, index + 1, Items, index, Count - index);
76  }
77  Items[Count] = default(T);
78  }
79 
80  public void Clear()
81  {
82  this.Count = 0;
83  }
84 
85  public void EnsureCapacity(int newCapacity)
86  {
87  if (this.Items.Length < newCapacity)
88  {
89  int newSize = this.Items.Length * 2;
90  if (newSize < newCapacity)
91  newSize = newCapacity;
92 
93  var destinationArray = new T[newSize];
94  Array.Copy(this.Items, 0, destinationArray, 0, this.Count);
95  this.Items = destinationArray;
96  }
97  }
98 
99  IEnumerator<T> IEnumerable<T>.GetEnumerator()
100  {
101  return new Enumerator(Items, Count);
102  }
103 
104  IEnumerator IEnumerable.GetEnumerator()
105  {
106  return new Enumerator(Items, Count);
107  }
108 
109  public Enumerator GetEnumerator()
110  {
111  return new Enumerator(Items, Count);
112  }
113 
114  public static implicit operator FastListStruct<T>(FastList<T> fastList)
115  {
116  return new FastListStruct<T>(fastList);
117  }
118 
119  public static implicit operator FastListStruct<T>(T[] array)
120  {
121  return new FastListStruct<T>(array);
122  }
123 
124  public bool Contains(T item)
125  {
126  return IndexOf(item) >= 0;
127  }
128 
129  public int IndexOf(T item)
130  {
131  return Array.IndexOf(Items, item, 0, Count);
132  }
133 
134  #region Nested type: Enumerator
135 
136  [StructLayout(LayoutKind.Sequential)]
137  public struct Enumerator : IEnumerator<T>, IDisposable, IEnumerator
138  {
139  private T[] items;
140  private int count;
141  private int index;
142  private T current;
143 
144  internal Enumerator(T[] items, int count)
145  {
146  this.items = items;
147  this.count = count;
148  index = 0;
149  current = default(T);
150  }
151 
152  public void Dispose()
153  {
154  }
155 
156  public bool MoveNext()
157  {
158  if (index < count)
159  {
160  current = items[index];
161  index++;
162  return true;
163  }
164  return MoveNextRare();
165  }
166 
167  private bool MoveNextRare()
168  {
169  index = count + 1;
170  current = default(T);
171  return false;
172  }
173 
174  public T Current
175  {
176  get { return current; }
177  }
178 
179  object IEnumerator.Current
180  {
181  get { return Current; }
182  }
183 
184  void IEnumerator.Reset()
185  {
186  index = 0;
187  current = default(T);
188  }
189  }
190 
191  #endregion
192  }
193 }
Similar to List{T}, with direct access to underlying array.
Definition: FastList.cs:19
_In_ size_t count
Definition: DirectXTexP.h:174