1 /**
2  * Used internally, this is an ordered list of Systems for use by the engine update loop.
3  */
4 module ashd.core.systemlist;
5 
6 import ashd.core.system: System;
7 
8 
9 class SystemList
10 {
11     private
12     {
13         System mHead;
14         System mTail;
15     } 
16 
17     @property System head() { return mHead; }
18 
19     public void add( System system_a )
20     {
21         if ( ! mHead )
22         {
23             mHead = mTail = system_a;
24             system_a.next = null;
25             system_a.previous = null;
26         }
27         else
28         {
29             System node;
30             for( node = mTail; node; node = node.previous )
31             {
32                 if ( node.priority <= system_a.priority )
33                 {
34                     break;
35                 }
36             }
37             if ( node == mTail )
38             {
39                 mTail.next = system_a;
40                 system_a.previous = mTail;
41                 system_a.next = null;
42                 mTail = system_a;
43             }
44             else if ( !node )
45             {
46                 system_a.next = mHead;
47                 system_a.previous = null;
48                 mHead.previous = system_a;
49                 mHead = system_a;
50             }
51             else
52             {
53                 system_a.next = node.next;
54                 system_a.previous = node;
55                 node.next.previous = system_a;
56                 node.next = system_a;
57             }
58         }
59     }
60     
61     public void remove( System system_a )
62     {
63         if ( mHead == system_a )
64         {
65             mHead = mHead.next;
66         }
67         if ( mTail == system_a )
68         {
69             mTail = mTail.previous;
70         }
71         
72         if ( system_a .previous )
73         {
74             system_a.previous.next = system_a.next;
75         }
76         
77         if ( system_a.next )
78         {
79             system_a.next.previous = system_a.previous;
80         }
81         // N.B. Don't set system.next and system.previous to null because that will break the list iteration if node is the current node in the iteration.
82     }
83     
84     public void removeAll()
85     {
86         while( mHead )
87         {
88             System system = mHead;
89             mHead = mHead.next;
90             system.previous = null;
91             system.next = null;
92         }
93         mTail = null;
94     }
95 
96     public System get(T)()
97     {
98         for ( System system = head; system; system = system.next )
99         {
100             if ( system.type == T.classinfo )
101             {
102                 return system;
103             }
104         }
105         return null;
106     }
107 }