aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/XMREngine/XMRInstQueue.cs
blob: 41acac86fbff01483de7214bfa52161a54ca0fad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright (c) Contributors, http://opensimulator.org/
 * See CONTRIBUTORS.TXT for a full list of copyright holders.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSimulator Project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;

namespace OpenSim.Region.ScriptEngine.XMREngine
{
    /**
     * @brief Implements a queue of XMRInstance's.
     *        Do our own queue to avoid shitty little mallocs.
     *
     * Note: looping inst.m_NextInst and m_PrevInst back to itself
     *       when inst is removed from a queue is purely for debug.
     */
    public class XMRInstQueue
    {
        private XMRInstance m_Head = null;
        private XMRInstance m_Tail = null;

        /**
         * @brief Insert instance at head of queue (in front of all others)
         * @param inst = instance to insert
         */
        public void InsertHead(XMRInstance inst)
        {
            if ((inst.m_PrevInst != inst) || (inst.m_NextInst != inst)) {
                throw new Exception("already in list");
            }
            inst.m_PrevInst = null;
            if ((inst.m_NextInst = m_Head) == null) {
                m_Tail = inst;
            } else {
                m_Head.m_PrevInst = inst;
            }
            m_Head = inst;
        }

        /**
         * @brief Insert instance at tail of queue (behind all others)
         * @param inst = instance to insert
         */
        public void InsertTail(XMRInstance inst)
        {
            if ((inst.m_PrevInst != inst) || (inst.m_NextInst != inst)) {
                throw new Exception("already in list");
            }
            inst.m_NextInst = null;
            if ((inst.m_PrevInst = m_Tail) == null) {
                m_Head = inst;
            } else {
                m_Tail.m_NextInst = inst;
            }
            m_Tail = inst;
        }

        /**
         * @brief Insert instance before another element in queue
         * @param inst  = instance to insert
         * @param after = element that is to come after one being inserted
         */
        public void InsertBefore(XMRInstance inst, XMRInstance after)
        {
            if ((inst.m_PrevInst != inst) || (inst.m_NextInst != inst)) {
                throw new Exception("already in list");
            }
            if (after == null) {
                InsertTail(inst);
            } else {
                inst.m_NextInst = after;
                inst.m_PrevInst = after.m_PrevInst;
                if (inst.m_PrevInst == null) {
                    m_Head = inst;
                } else {
                    inst.m_PrevInst.m_NextInst = inst;
                }
                after.m_PrevInst = inst;
            }
        }

        /**
         * @brief Peek to see if anything in queue
         * @returns first XMRInstance in queue but doesn't remove it
         *          null if queue is empty
         */
        public XMRInstance PeekHead()
        {
            return m_Head;
        }

        /**
         * @brief Remove first element from queue, if any
         * @returns null if queue is empty
         *          else returns first element in queue and removes it
         */
        public XMRInstance RemoveHead()
        {
            XMRInstance inst = m_Head;
            if (inst != null) {
                if ((m_Head = inst.m_NextInst) == null) {
                    m_Tail = null;
                } else {
                    m_Head.m_PrevInst = null;
                }
                inst.m_NextInst = inst;
                inst.m_PrevInst = inst;
            }
            return inst;
        }

        /**
         * @brief Remove last element from queue, if any
         * @returns null if queue is empty
         *          else returns last element in queue and removes it
         */
        public XMRInstance RemoveTail()
        {
            XMRInstance inst = m_Tail;
            if (inst != null) {
                if ((m_Tail = inst.m_PrevInst) == null) {
                    m_Head = null;
                } else {
                    m_Tail.m_NextInst = null;
                }
                inst.m_NextInst = inst;
                inst.m_PrevInst = inst;
            }
            return inst;
        }

        /**
         * @brief Remove arbitrary element from queue, if any
         * @param inst = element to remove (assumed to be in the queue)
         * @returns with element removed
         */
        public void Remove(XMRInstance inst)
        {
            XMRInstance next = inst.m_NextInst;
            XMRInstance prev = inst.m_PrevInst;
            if ((prev == inst) || (next == inst)) {
                throw new Exception("not in a list");
            }
            if (next == null) {
                if (m_Tail != inst) {
                    throw new Exception("not in this list");
                }
                m_Tail = prev;
            } else {
                next.m_PrevInst = prev;
            }
            if (prev == null) {
                if (m_Head != inst) {
                    throw new Exception("not in this list");
                }
                m_Head = next;
            } else {
                prev.m_NextInst = next;
            }
            inst.m_NextInst = inst;
            inst.m_PrevInst = inst;
        }
    }
}