1 22 23 package com.sosnoski.util.queue; 24 25 import java.lang.reflect.Array ; 26 27 import com.sosnoski.util.*; 28 29 48 49 public abstract class QueueBase extends GrowableBase 50 { 51 52 protected int m_fillOffset; 53 54 55 protected int m_emptyOffset; 56 57 64 65 public QueueBase(int size, int growth, Class type) { 66 super(size, growth, type); 67 } 68 69 75 76 public QueueBase(int size, Class type) { 77 this(size, Integer.MAX_VALUE, type); 78 } 79 80 85 86 public QueueBase(QueueBase base) { 87 super(base); 88 System.arraycopy(base.getArray(), 0, getArray(), 0, m_countLimit); 89 m_fillOffset = base.m_fillOffset; 90 m_emptyOffset = base.m_emptyOffset; 91 } 92 93 101 102 protected void resizeCopy(Object base, Object grown) { 103 104 if (m_fillOffset < m_emptyOffset) { 106 107 int size = Array.getLength(base); 109 int length = size - m_emptyOffset; 110 System.arraycopy(base, m_emptyOffset, grown, 0, length); 111 System.arraycopy(base, 0, grown, length, m_fillOffset); 112 m_fillOffset += size; 113 114 } else { 115 116 int length = m_fillOffset - m_emptyOffset; 118 System.arraycopy(base, m_emptyOffset, grown, 0, length); 119 } 120 121 m_fillOffset -= m_emptyOffset; 123 m_emptyOffset = 0; 124 } 125 126 134 135 protected int getAddIndex() { 136 137 int next = (m_fillOffset + 1) % m_countLimit; 139 140 if (next == m_emptyOffset) { 142 143 growArray(m_countLimit + 1); 145 next = m_fillOffset + 1; 146 } 147 148 int index = m_fillOffset; 150 m_fillOffset = next; 151 return index; 152 } 153 154 161 162 protected int getRemoveIndex() { 163 if (m_fillOffset == m_emptyOffset) { 164 throw new IllegalStateException 165 ("Attempted remove from empty queue"); 166 } else { 167 int offset = m_emptyOffset; 168 m_emptyOffset = (m_emptyOffset + 1) % m_countLimit; 169 return offset; 170 } 171 } 172 173 178 179 public int size() { 180 int diff = m_fillOffset - m_emptyOffset; 181 return (diff >= 0) ? diff : (diff + m_countLimit); 182 } 183 184 189 190 public boolean isEmpty() { 191 return m_fillOffset == m_emptyOffset; 192 } 193 194 197 198 public void clear() { 199 if (m_fillOffset >= m_emptyOffset) { 200 discardValues(m_emptyOffset, m_fillOffset); 201 } else { 202 discardValues(m_emptyOffset, m_countLimit); 203 discardValues(0, m_fillOffset); 204 } 205 m_fillOffset = m_emptyOffset = 0; 206 } 207 208 215 216 public void discard(int count) { 217 if (count > size()) { 218 throw new IllegalStateException 219 ("Attempted to discard more items than present on queue"); 220 } else { 221 int limit = m_emptyOffset + count; 222 if (limit > m_countLimit) { 223 limit -= m_countLimit; 224 discardValues(m_emptyOffset, m_countLimit); 225 discardValues(0, limit); 226 } else { 227 discardValues(m_emptyOffset, limit); 228 } 229 m_emptyOffset = limit; 230 } 231 } 232 233 240 241 protected Object buildArray(Class type) { 242 int length = size(); 243 Object copy = Array.newInstance(type, length); 244 if (m_fillOffset >= m_emptyOffset) { 245 System.arraycopy(getArray(), m_emptyOffset, copy, 0, length); 246 } else { 247 int offset = m_countLimit - m_emptyOffset; 248 System.arraycopy(getArray(), m_emptyOffset, copy, 0, offset); 249 System.arraycopy(getArray(), 0, copy, offset, m_fillOffset); 250 } 251 return copy; 252 } 253 } 254 | Popular Tags |