Jypeli  9
The simple game programming library
SynchronousList.cs
Siirry tämän tiedoston dokumentaatioon.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Collections;
5 
6 namespace Jypeli
7 {
14  public class SynchronousList<T> : IEnumerable<T>, Updatable
15  {
16  #region Item actions
17 
18  protected abstract class ListAction
19  {
21 
23  {
24  this.list = list;
25  }
26 
27  public abstract void Execute();
28  }
29 
30  protected sealed class AddItemAction : ListAction
31  {
32  internal T newItem;
33 
34  public AddItemAction( SynchronousList<T> list, T itemToAdd )
35  : base( list )
36  {
37  this.newItem = itemToAdd;
38  }
39 
40  public override void Execute()
41  {
42  if ( list.Contains( newItem ) )
43  //throw new ArgumentException( "The object has already been added." );
44  return;
45 
46  list.items.Add( newItem );
47  list.OnItemAdded( newItem );
48  }
49  }
50 
51  protected sealed class RemoveItemAction : ListAction
52  {
53  internal T removeItem;
54 
55  public RemoveItemAction( SynchronousList<T> items, T itemToRemove )
56  : base( items )
57  {
58  this.removeItem = itemToRemove;
59  }
60 
61  public override void Execute()
62  {
63  if ( list.items.Remove( removeItem ) )
64  list.OnItemRemoved( removeItem );
65  }
66  }
67 
68  protected sealed class ClearAction : ListAction
69  {
71  : base( items )
72  {
73  }
74 
75  public override void Execute()
76  {
77  foreach ( var item in list )
78  {
79  list.OnItemRemoved( item );
80  }
81 
82  list.items.Clear();
83  }
84  }
85 
86  #endregion
87 
88  internal List<T> items = new List<T>();
89  Queue<ListAction> actions = new Queue<ListAction>();
90 
96  public T this[int index]
97  {
98  get { return items[index - FirstIndex]; }
99  set { items[index - FirstIndex] = value; }
100  }
101 
105  public int FirstIndex { get; set; }
106 
110  public int LastIndex
111  {
112  get { return FirstIndex + items.Count - 1; }
113  }
114 
119  public int Count
120  {
121  get { return items.Count; }
122  }
123 
124  public bool IsUpdated
125  {
126  get { return true; }
127  }
128 
132  public event Action<T> ItemAdded;
133 
137  public event Action<T> ItemRemoved;
138 
143  public SynchronousList( int firstIndex )
144  {
145  FirstIndex = firstIndex;
146  }
147 
152  : this( 0 )
153  {
154  }
155 
156  private void OnItemAdded( T item )
157  {
158  if ( ItemAdded != null )
159  ItemAdded( item );
160  }
161 
162  private void OnItemRemoved( T item )
163  {
164  if ( ItemRemoved != null )
165  ItemRemoved( item );
166  }
167 
168  #region INotifyList<T> Members
169 
173  public event Action Changed;
174 
175  private void OnChanged()
176  {
177  if ( Changed != null )
178  Changed();
179  }
180 
181  #endregion
182 
183  #region IEnumerable Members
184 
185  public IEnumerator<T> GetEnumerator()
186  {
187  return items.GetEnumerator();
188  }
189 
190  IEnumerator IEnumerable.GetEnumerator()
191  {
192  return items.GetEnumerator();
193  }
194 
195  #endregion
196 
197  public void Add( T item )
198  {
199  actions.Enqueue( new AddItemAction( this, item ) );
200  }
201 
202  public void Remove( T item )
203  {
204  actions.Enqueue( new RemoveItemAction( this, item ) );
205  }
206 
207  public void Clear()
208  {
209  actions.Enqueue( new ClearAction( this ) );
210  }
211 
212  public bool Contains( T item )
213  {
214  return items.Contains( item );
215  }
216 
217  public bool WillContain( T item )
218  {
219  ListAction[] actionArray = actions.ToArray();
220  bool exists = Contains(item);
221 
222  for (int i = 0; i < actionArray.Length; i++)
223  {
224  if (actionArray[i] is AddItemAction && ((AddItemAction)actionArray[i]).newItem.Equals( item ))
225  exists = true;
226 
227  else if (actionArray[i] is RemoveItemAction && ((RemoveItemAction)actionArray[i]).removeItem.Equals( item ))
228  exists = false;
229 
230  else if (actionArray[i] is ClearAction)
231  exists = false;
232  }
233 
234  return exists;
235  }
236 
237  public int IndexOf( T item )
238  {
239  return FirstIndex + items.IndexOf( item );
240  }
241 
242  public T Find( Predicate<T> pred )
243  {
244  return items.Find( pred );
245  }
246 
247  public List<T> FindAll( Predicate<T> pred )
248  {
249  return items.FindAll( pred );
250  }
251 
257  public bool UpdateChanges()
258  {
259  if ( actions.Count == 0 ) return false;
260 
261  while ( actions.Count > 0 )
262  actions.Dequeue().Execute();
263 
264  return true;
265  }
266 
272  public void Update( Time time )
273  {
274  bool changed = UpdateChanges();
275 
276  foreach ( var item in items )
277  {
278  var DestroyableItem = item as Destroyable;
279  var UpdatableItem = item as Updatable;
280 
281  if ( DestroyableItem != null && DestroyableItem.IsDestroyed )
282  Remove( item );
283  if ( UpdatableItem != null && UpdatableItem.IsUpdated )
284  UpdatableItem.Update( time );
285  }
286 
287  changed |= UpdateChanges();
288  if ( changed ) OnChanged();
289  }
290 
297  public void Update( Time time, Predicate<T> isUpdated )
298  {
299  bool changed = UpdateChanges();
300 
301  foreach ( var item in items )
302  {
303  var DestroyableItem = item as Destroyable;
304  var UpdatableItem = item as Updatable;
305 
306  if ( DestroyableItem != null && DestroyableItem.IsDestroyed )
307  Remove( item );
308  else if ( UpdatableItem != null && UpdatableItem.IsUpdated && isUpdated(item) )
309  UpdatableItem.Update( time );
310  }
311 
312  changed |= UpdateChanges();
313  if ( changed ) OnChanged();
314  }
315 
320  public void ForEach( Action<T> action )
321  {
322  for ( int i = 0; i < items.Count; i++ )
323  {
324  action( items[i] );
325  }
326  }
327 
334  public void ForEach<T1>( Action<T, T1> action, T1 p1 )
335  {
336  for ( int i = 0; i < items.Count; i++ )
337  {
338  action( items[i], p1 );
339  }
340  }
341 
342  internal IEnumerable<T> GetObjectsAboutToBeAdded()
343  {
344  return from a in actions
345  where a is AddItemAction
346  select (a as AddItemAction).newItem;
347  }
348  }
349 }
Jypeli.SynchronousList.RemoveItemAction.Execute
override void Execute()
Definition: SynchronousList.cs:61
Jypeli.SynchronousList.RemoveItemAction.RemoveItemAction
RemoveItemAction(SynchronousList< T > items, T itemToRemove)
Definition: SynchronousList.cs:55
Jypeli.SynchronousList.ClearAction.ClearAction
ClearAction(SynchronousList< T > items)
Definition: SynchronousList.cs:70
Jypeli.SynchronousList.SynchronousList
SynchronousList(int firstIndex)
Luo uuden synkronisen listan.
Definition: SynchronousList.cs:143
Jypeli.SynchronousList.AddItemAction
Definition: SynchronousList.cs:31
Jypeli.SynchronousList.Changed
Action Changed
Tapahtuu kun lista on muuttunut.
Definition: SynchronousList.cs:173
Jypeli
Definition: Automobile.cs:5
Jypeli.SynchronousList.RemoveItemAction
Definition: SynchronousList.cs:52
Jypeli.SynchronousList.Update
void Update(Time time, Predicate< T > isUpdated)
Lisää ja poistaa jonossa olevat elementit sekä kutsuu niiden Update-metodia tietyllä ehdolla.
Definition: SynchronousList.cs:297
Jypeli.SynchronousList.OnItemAdded
void OnItemAdded(T item)
Definition: SynchronousList.cs:156
Jypeli.SynchronousList.ItemRemoved
Action< T > ItemRemoved
Tapahtuu kun elementti on poistettu listasta.
Definition: SynchronousList.cs:137
Jypeli.SynchronousList.Add
void Add(T item)
Definition: SynchronousList.cs:197
Jypeli.SynchronousList.GetObjectsAboutToBeAdded
IEnumerable< T > GetObjectsAboutToBeAdded()
Definition: SynchronousList.cs:342
Jypeli.SynchronousList.Contains
bool Contains(T item)
Definition: SynchronousList.cs:212
Jypeli.SynchronousList.IsUpdated
bool IsUpdated
Definition: SynchronousList.cs:125
Jypeli.SynchronousList.ListAction.ListAction
ListAction(SynchronousList< T > list)
Definition: SynchronousList.cs:22
Jypeli.SynchronousList.Remove
void Remove(T item)
Definition: SynchronousList.cs:202
Jypeli.SynchronousList.IndexOf
int IndexOf(T item)
Definition: SynchronousList.cs:237
Jypeli.SynchronousList.GetEnumerator
IEnumerator< T > GetEnumerator()
Definition: SynchronousList.cs:185
Jypeli.SynchronousList.ClearAction.Execute
override void Execute()
Definition: SynchronousList.cs:75
Jypeli.Updatable
Rajapinta päivittyville olioille.
Definition: Updatable.cs:7
Jypeli.SynchronousList.Count
int Count
Kuinka monta elementtiä listassa nyt on. Ei laske mukaan samalla päivityskierroksella tehtyjä muutoks...
Definition: SynchronousList.cs:120
Jypeli.SynchronousList.OnItemRemoved
void OnItemRemoved(T item)
Definition: SynchronousList.cs:162
Jypeli.SynchronousList.ForEach< T1 >
void ForEach< T1 >(Action< T, T1 > action, T1 p1)
Suorittaa annetun toimenpiteen kaikille (nykyisille) listan alkioille.
Definition: SynchronousList.cs:334
Jypeli.SynchronousList.Clear
void Clear()
Definition: SynchronousList.cs:207
Jypeli.SynchronousList.RemoveItemAction.removeItem
T removeItem
Definition: SynchronousList.cs:53
Jypeli.Time
Sisältää tiedon ajasta, joka on kulunut pelin alusta ja viime päivityksestä.
Definition: Time.cs:14
Jypeli.SynchronousList.actions
Queue< ListAction > actions
Definition: SynchronousList.cs:89
Jypeli.SynchronousList.WillContain
bool WillContain(T item)
Definition: SynchronousList.cs:217
Jypeli.SynchronousList.ListAction.list
SynchronousList< T > list
Definition: SynchronousList.cs:20
Jypeli.Destroyable
Rajapinta olioille, jotka ovat tuhottavissa.
Definition: Destroyable.cs:9
Jypeli.SynchronousList.OnChanged
void OnChanged()
Definition: SynchronousList.cs:175
Jypeli.SynchronousList.items
List< T > items
Definition: SynchronousList.cs:88
Jypeli.SynchronousList.ListAction
Definition: SynchronousList.cs:19
Jypeli.SynchronousList.ItemAdded
Action< T > ItemAdded
Tapahtuu kun uusi elementti on lisätty listaan.
Definition: SynchronousList.cs:132
Jypeli.SynchronousList.AddItemAction.Execute
override void Execute()
Definition: SynchronousList.cs:40
Jypeli.SynchronousList.Find
T Find(Predicate< T > pred)
Definition: SynchronousList.cs:242
Jypeli.SynchronousList.ListAction.Execute
abstract void Execute()
Jypeli.SynchronousList.FindAll
List< T > FindAll(Predicate< T > pred)
Definition: SynchronousList.cs:247
Jypeli.SynchronousList.SynchronousList
SynchronousList()
Luo uuden synkronisen listan.
Definition: SynchronousList.cs:151
System
Definition: CFFauxAttributes.cs:29
Jypeli.SynchronousList.UpdateChanges
bool UpdateChanges()
Lisää ja poistaa jonossa olevat elementit, mutta ei kutsu elementtien Update-metodia.
Definition: SynchronousList.cs:257
Jypeli.SynchronousList.LastIndex
int LastIndex
Viimeisen elementin indeksi.
Definition: SynchronousList.cs:111
Jypeli.SynchronousList
Synkroninen lista, eli lista joka päivittyy vasta kun sen Update-metodia kutsutaan....
Definition: SynchronousList.cs:15
Jypeli.SynchronousList.ClearAction
Definition: SynchronousList.cs:69
Jypeli.SynchronousList.ForEach
void ForEach(Action< T > action)
Suorittaa annetun toimenpiteen kaikille (nykyisille) listan alkioille.
Definition: SynchronousList.cs:320
Jypeli.SynchronousList.FirstIndex
int FirstIndex
Ensimmäisen elementin indeksi. Muutettavissa.
Definition: SynchronousList.cs:105
Jypeli.SynchronousList.AddItemAction.newItem
T newItem
Definition: SynchronousList.cs:32
Jypeli.SynchronousList.AddItemAction.AddItemAction
AddItemAction(SynchronousList< T > list, T itemToAdd)
Definition: SynchronousList.cs:34
Jypeli.SynchronousList.Update
void Update(Time time)
Lisää ja poistaa jonossa olevat elementit sekä kutsuu niiden Update-metodia.
Definition: SynchronousList.cs:272