corrade-vassal – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | vero | 1 | /* |
2 | * Copyright (c) 2006-2014, openmetaverse.org |
||
3 | * All rights reserved. |
||
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 | * |
||
8 | * - Redistributions of source code must retain the above copyright notice, this |
||
9 | * list of conditions and the following disclaimer. |
||
10 | * - Neither the name of the openmetaverse.org nor the names |
||
11 | * of its contributors may be used to endorse or promote products derived from |
||
12 | * this software without specific prior written permission. |
||
13 | * |
||
14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||
15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
18 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
||
22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||
23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||
24 | * POSSIBILITY OF SUCH DAMAGE. |
||
25 | */ |
||
26 | |||
27 | using System; |
||
28 | |||
29 | namespace OpenMetaverse |
||
30 | { |
||
31 | public class CircularQueue<T> |
||
32 | { |
||
33 | public readonly T[] Items; |
||
34 | |||
35 | int first; |
||
36 | int next; |
||
37 | int capacity; |
||
38 | object syncRoot; |
||
39 | |||
40 | public int First { get { return first; } } |
||
41 | public int Next { get { return next; } } |
||
42 | |||
43 | public CircularQueue(int capacity) |
||
44 | { |
||
45 | this.capacity = capacity; |
||
46 | Items = new T[capacity]; |
||
47 | syncRoot = new object(); |
||
48 | } |
||
49 | |||
50 | /// <summary> |
||
51 | /// Copy constructor |
||
52 | /// </summary> |
||
53 | /// <param name="queue">Circular queue to copy</param> |
||
54 | public CircularQueue(CircularQueue<T> queue) |
||
55 | { |
||
56 | lock (queue.syncRoot) |
||
57 | { |
||
58 | capacity = queue.capacity; |
||
59 | Items = new T[capacity]; |
||
60 | syncRoot = new object(); |
||
61 | |||
62 | for (int i = 0; i < capacity; i++) |
||
63 | Items[i] = queue.Items[i]; |
||
64 | |||
65 | first = queue.first; |
||
66 | next = queue.next; |
||
67 | } |
||
68 | } |
||
69 | |||
70 | public void Clear() |
||
71 | { |
||
72 | lock (syncRoot) |
||
73 | { |
||
74 | // Explicitly remove references to help garbage collection |
||
75 | for (int i = 0; i < capacity; i++) |
||
76 | Items[i] = default(T); |
||
77 | |||
78 | first = next; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | public void Enqueue(T value) |
||
83 | { |
||
84 | lock (syncRoot) |
||
85 | { |
||
86 | Items[next] = value; |
||
87 | next = (next + 1) % capacity; |
||
88 | if (next == first) first = (first + 1) % capacity; |
||
89 | } |
||
90 | } |
||
91 | |||
92 | public T Dequeue() |
||
93 | { |
||
94 | lock (syncRoot) |
||
95 | { |
||
96 | T value = Items[first]; |
||
97 | Items[first] = default(T); |
||
98 | |||
99 | if (first != next) |
||
100 | first = (first + 1) % capacity; |
||
101 | |||
102 | return value; |
||
103 | } |
||
104 | } |
||
105 | |||
106 | public T DequeueLast() |
||
107 | { |
||
108 | lock (syncRoot) |
||
109 | { |
||
110 | // If the next element is right behind the first element (queue is full), |
||
111 | // back up the first element by one |
||
112 | int firstTest = first - 1; |
||
113 | if (firstTest < 0) firstTest = capacity - 1; |
||
114 | |||
115 | if (firstTest == next) |
||
116 | { |
||
117 | --next; |
||
118 | if (next < 0) next = capacity - 1; |
||
119 | |||
120 | --first; |
||
121 | if (first < 0) first = capacity - 1; |
||
122 | } |
||
123 | else if (first != next) |
||
124 | { |
||
125 | --next; |
||
126 | if (next < 0) next = capacity - 1; |
||
127 | } |
||
128 | |||
129 | T value = Items[next]; |
||
130 | Items[next] = default(T); |
||
131 | |||
132 | return value; |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | } |