aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/YEngine/XMRInstQueue.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/ScriptEngine/YEngine/XMRInstQueue.cs')
-rw-r--r--OpenSim/Region/ScriptEngine/YEngine/XMRInstQueue.cs192
1 files changed, 192 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/YEngine/XMRInstQueue.cs b/OpenSim/Region/ScriptEngine/YEngine/XMRInstQueue.cs
new file mode 100644
index 0000000..549fab5
--- /dev/null
+++ b/OpenSim/Region/ScriptEngine/YEngine/XMRInstQueue.cs
@@ -0,0 +1,192 @@
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
28using System;
29
30namespace OpenSim.Region.ScriptEngine.Yengine
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 {
94 inst.m_NextInst = after;
95 inst.m_PrevInst = after.m_PrevInst;
96 if(inst.m_PrevInst == null)
97 m_Head = inst;
98 else
99 inst.m_PrevInst.m_NextInst = inst;
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 {
124 if((m_Head = inst.m_NextInst) == null)
125 m_Tail = null;
126 else
127 m_Head.m_PrevInst = null;
128
129 inst.m_NextInst = inst;
130 inst.m_PrevInst = inst;
131 }
132 return inst;
133 }
134
135 /**
136 * @brief Remove last element from queue, if any
137 * @returns null if queue is empty
138 * else returns last element in queue and removes it
139 */
140 public XMRInstance RemoveTail()
141 {
142 XMRInstance inst = m_Tail;
143 if(inst != null)
144 {
145 if((m_Tail = inst.m_PrevInst) == null)
146 m_Head = null;
147 else
148 m_Tail.m_NextInst = null;
149
150 inst.m_NextInst = inst;
151 inst.m_PrevInst = inst;
152 }
153 return inst;
154 }
155
156 /**
157 * @brief Remove arbitrary element from queue, if any
158 * @param inst = element to remove (assumed to be in the queue)
159 * @returns with element removed
160 */
161 public void Remove(XMRInstance inst)
162 {
163 XMRInstance next = inst.m_NextInst;
164 XMRInstance prev = inst.m_PrevInst;
165 if((prev == inst) || (next == inst))
166 throw new Exception("not in a list");
167
168 if(next == null)
169 {
170 if(m_Tail != inst)
171 throw new Exception("not in this list");
172
173 m_Tail = prev;
174 }
175 else
176 next.m_PrevInst = prev;
177
178 if(prev == null)
179 {
180 if(m_Head != inst)
181 throw new Exception("not in this list");
182
183 m_Head = next;
184 }
185 else
186 prev.m_NextInst = next;
187
188 inst.m_NextInst = inst;
189 inst.m_PrevInst = inst;
190 }
191 }
192}