diff options
Diffstat (limited to 'OpenSim/Region/ScriptEngine/XMREngine/XMRInstQueue.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/XMREngine/XMRInstQueue.cs | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/XMREngine/XMRInstQueue.cs b/OpenSim/Region/ScriptEngine/XMREngine/XMRInstQueue.cs new file mode 100644 index 0000000..41acac8 --- /dev/null +++ b/OpenSim/Region/ScriptEngine/XMREngine/XMRInstQueue.cs | |||
@@ -0,0 +1,186 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSimulator Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | |||
30 | namespace OpenSim.Region.ScriptEngine.XMREngine | ||
31 | { | ||
32 | /** | ||
33 | * @brief Implements a queue of XMRInstance's. | ||
34 | * Do our own queue to avoid shitty little mallocs. | ||
35 | * | ||
36 | * Note: looping inst.m_NextInst and m_PrevInst back to itself | ||
37 | * when inst is removed from a queue is purely for debug. | ||
38 | */ | ||
39 | public class XMRInstQueue | ||
40 | { | ||
41 | private XMRInstance m_Head = null; | ||
42 | private XMRInstance m_Tail = null; | ||
43 | |||
44 | /** | ||
45 | * @brief Insert instance at head of queue (in front of all others) | ||
46 | * @param inst = instance to insert | ||
47 | */ | ||
48 | public void InsertHead(XMRInstance inst) | ||
49 | { | ||
50 | if ((inst.m_PrevInst != inst) || (inst.m_NextInst != inst)) { | ||
51 | throw new Exception("already in list"); | ||
52 | } | ||
53 | inst.m_PrevInst = null; | ||
54 | if ((inst.m_NextInst = m_Head) == null) { | ||
55 | m_Tail = inst; | ||
56 | } else { | ||
57 | m_Head.m_PrevInst = inst; | ||
58 | } | ||
59 | m_Head = inst; | ||
60 | } | ||
61 | |||
62 | /** | ||
63 | * @brief Insert instance at tail of queue (behind all others) | ||
64 | * @param inst = instance to insert | ||
65 | */ | ||
66 | public void InsertTail(XMRInstance inst) | ||
67 | { | ||
68 | if ((inst.m_PrevInst != inst) || (inst.m_NextInst != inst)) { | ||
69 | throw new Exception("already in list"); | ||
70 | } | ||
71 | inst.m_NextInst = null; | ||
72 | if ((inst.m_PrevInst = m_Tail) == null) { | ||
73 | m_Head = inst; | ||
74 | } else { | ||
75 | m_Tail.m_NextInst = inst; | ||
76 | } | ||
77 | m_Tail = inst; | ||
78 | } | ||
79 | |||
80 | /** | ||
81 | * @brief Insert instance before another element in queue | ||
82 | * @param inst = instance to insert | ||
83 | * @param after = element that is to come after one being inserted | ||
84 | */ | ||
85 | public void InsertBefore(XMRInstance inst, XMRInstance after) | ||
86 | { | ||
87 | if ((inst.m_PrevInst != inst) || (inst.m_NextInst != inst)) { | ||
88 | throw new Exception("already in list"); | ||
89 | } | ||
90 | if (after == null) { | ||
91 | InsertTail(inst); | ||
92 | } else { | ||
93 | inst.m_NextInst = after; | ||
94 | inst.m_PrevInst = after.m_PrevInst; | ||
95 | if (inst.m_PrevInst == null) { | ||
96 | m_Head = inst; | ||
97 | } else { | ||
98 | inst.m_PrevInst.m_NextInst = inst; | ||
99 | } | ||
100 | after.m_PrevInst = inst; | ||
101 | } | ||
102 | } | ||
103 | |||
104 | /** | ||
105 | * @brief Peek to see if anything in queue | ||
106 | * @returns first XMRInstance in queue but doesn't remove it | ||
107 | * null if queue is empty | ||
108 | */ | ||
109 | public XMRInstance PeekHead() | ||
110 | { | ||
111 | return m_Head; | ||
112 | } | ||
113 | |||
114 | /** | ||
115 | * @brief Remove first element from queue, if any | ||
116 | * @returns null if queue is empty | ||
117 | * else returns first element in queue and removes it | ||
118 | */ | ||
119 | public XMRInstance RemoveHead() | ||
120 | { | ||
121 | XMRInstance inst = m_Head; | ||
122 | if (inst != null) { | ||
123 | if ((m_Head = inst.m_NextInst) == null) { | ||
124 | m_Tail = null; | ||
125 | } else { | ||
126 | m_Head.m_PrevInst = null; | ||
127 | } | ||
128 | inst.m_NextInst = inst; | ||
129 | inst.m_PrevInst = inst; | ||
130 | } | ||
131 | return inst; | ||
132 | } | ||
133 | |||
134 | /** | ||
135 | * @brief Remove last element from queue, if any | ||
136 | * @returns null if queue is empty | ||
137 | * else returns last element in queue and removes it | ||
138 | */ | ||
139 | public XMRInstance RemoveTail() | ||
140 | { | ||
141 | XMRInstance inst = m_Tail; | ||
142 | if (inst != null) { | ||
143 | if ((m_Tail = inst.m_PrevInst) == null) { | ||
144 | m_Head = null; | ||
145 | } else { | ||
146 | m_Tail.m_NextInst = null; | ||
147 | } | ||
148 | inst.m_NextInst = inst; | ||
149 | inst.m_PrevInst = inst; | ||
150 | } | ||
151 | return inst; | ||
152 | } | ||
153 | |||
154 | /** | ||
155 | * @brief Remove arbitrary element from queue, if any | ||
156 | * @param inst = element to remove (assumed to be in the queue) | ||
157 | * @returns with element removed | ||
158 | */ | ||
159 | public void Remove(XMRInstance inst) | ||
160 | { | ||
161 | XMRInstance next = inst.m_NextInst; | ||
162 | XMRInstance prev = inst.m_PrevInst; | ||
163 | if ((prev == inst) || (next == inst)) { | ||
164 | throw new Exception("not in a list"); | ||
165 | } | ||
166 | if (next == null) { | ||
167 | if (m_Tail != inst) { | ||
168 | throw new Exception("not in this list"); | ||
169 | } | ||
170 | m_Tail = prev; | ||
171 | } else { | ||
172 | next.m_PrevInst = prev; | ||
173 | } | ||
174 | if (prev == null) { | ||
175 | if (m_Head != inst) { | ||
176 | throw new Exception("not in this list"); | ||
177 | } | ||
178 | m_Head = next; | ||
179 | } else { | ||
180 | prev.m_NextInst = next; | ||
181 | } | ||
182 | inst.m_NextInst = inst; | ||
183 | inst.m_PrevInst = inst; | ||
184 | } | ||
185 | } | ||
186 | } | ||