1 /**
2  * An internal class for a linked list of entities. Used inside the framework for
3  * managing the entities.
4  */
5 
6 module ashd.core.entitylist;
7 
8 import ashd.core.entity: Entity;
9 
10 class EntityList
11 {
12     private
13     {
14         Entity mHead = null;
15         Entity mTail = null;
16     }
17 
18     @property Entity head() { return mHead; }
19 
20 
21     public void add( Entity entity_a )
22     {
23         if ( ! mHead )
24         {
25             mHead = mTail = entity_a;
26             entity_a.next = null;
27             entity_a.previous = null;
28         }
29         else
30         {
31             mTail.next = entity_a;
32             entity_a.previous = mTail;
33             entity_a.next = null;
34             mTail = entity_a;
35         }
36     }
37     
38     public void remove( Entity entity_a )
39     {
40         if ( mHead == entity_a )
41         {
42             mHead = mHead.next;
43         }
44         if ( mTail == entity_a )
45         {
46             mTail = mTail.previous;
47         }
48         
49         if ( entity_a.previous )
50         {
51             entity_a.previous.next = entity_a.next;
52         }
53         
54         if ( entity_a.next )
55         {
56             entity_a.next.previous = entity_a.previous;
57         }
58         // N.B. Don't set node.next and node.previous to null because that will break the list iteration if node is the current node in the iteration.
59     }
60     
61     public void removeAll()
62     {
63         while( mHead )
64         {
65             Entity entity = mHead;
66             mHead = mHead.next;
67             entity.previous = null;
68             entity.next = null;
69         }
70         mTail = null;
71     }
72 }