| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.dev.util; |
| 7 |
|
| 8 |
import java.util.Iterator; |
| 9 |
|
| 10 |
/** |
| 11 |
* A ring buffer that supports concurrent access. |
| 12 |
* |
| 13 |
* @param <K> the key type |
| 14 |
*/ |
| 15 |
public class ConcurrentRing<K> { |
| 16 |
|
| 17 |
/** |
| 18 |
* The ring buffer. |
| 19 |
*/ |
| 20 |
K[] buffer; |
| 21 |
|
| 22 |
/** |
| 23 |
* The read position. |
| 24 |
*/ |
| 25 |
volatile int readPos; |
| 26 |
|
| 27 |
/** |
| 28 |
* The write position. |
| 29 |
*/ |
| 30 |
volatile int writePos; |
| 31 |
|
| 32 |
@SuppressWarnings("unchecked") |
| 33 |
public ConcurrentRing() { |
| 34 |
buffer = (K[]) new Object[4]; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Get the first element, or null if none. |
| 39 |
* |
| 40 |
* @return the first element |
| 41 |
*/ |
| 42 |
public K peekFirst() { |
| 43 |
return buffer[getIndex(readPos)]; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Get the last element, or null if none. |
| 48 |
* |
| 49 |
* @return the last element |
| 50 |
*/ |
| 51 |
public K peekLast() { |
| 52 |
return buffer[getIndex(writePos - 1)]; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Add an element at the end. |
| 57 |
* |
| 58 |
* @param obj the element |
| 59 |
*/ |
| 60 |
public void add(K obj) { |
| 61 |
buffer[getIndex(writePos)] = obj; |
| 62 |
writePos++; |
| 63 |
if (writePos - readPos >= buffer.length) { |
| 64 |
// double the capacity |
| 65 |
@SuppressWarnings("unchecked") |
| 66 |
K[] b2 = (K[]) new Object[buffer.length * 2]; |
| 67 |
for (int i = readPos; i < writePos; i++) { |
| 68 |
K x = buffer[getIndex(i)]; |
| 69 |
int i2 = i & b2.length - 1; |
| 70 |
b2[i2] = x; |
| 71 |
} |
| 72 |
buffer = b2; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Remove the first element, if it matches. |
| 78 |
* |
| 79 |
* @param obj the element to remove |
| 80 |
* @return true if the element matched and was removed |
| 81 |
*/ |
| 82 |
public boolean removeFirst(K obj) { |
| 83 |
int p = readPos; |
| 84 |
int idx = getIndex(p); |
| 85 |
if (buffer[idx] != obj) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
buffer[idx] = null; |
| 89 |
readPos = p + 1; |
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Remove the last element, if it matches. |
| 95 |
* |
| 96 |
* @param obj the element to remove |
| 97 |
* @return true if the element matched and was removed |
| 98 |
*/ |
| 99 |
public boolean removeLast(K obj) { |
| 100 |
int p = writePos; |
| 101 |
int idx = getIndex(p - 1); |
| 102 |
if (buffer[idx] != obj) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
buffer[idx] = null; |
| 106 |
writePos = p - 1; |
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Get the index in the array of the given position. |
| 112 |
* |
| 113 |
* @param pos the position |
| 114 |
* @return the index |
| 115 |
*/ |
| 116 |
int getIndex(int pos) { |
| 117 |
return pos & (buffer.length - 1); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Get an iterator over all entries. |
| 122 |
* |
| 123 |
* @return the iterator |
| 124 |
*/ |
| 125 |
public Iterator<K> iterator() { |
| 126 |
return new Iterator<K>() { |
| 127 |
|
| 128 |
int offset; |
| 129 |
|
| 130 |
@Override |
| 131 |
public boolean hasNext() { |
| 132 |
return readPos + offset < writePos; |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public K next() { |
| 137 |
if (buffer[getIndex(readPos + offset)] == null) { |
| 138 |
System.out.println("" + readPos); |
| 139 |
System.out.println("" + getIndex(readPos + offset)); |
| 140 |
System.out.println("null?"); |
| 141 |
} |
| 142 |
return buffer[getIndex(readPos + offset++)]; |
| 143 |
} |
| 144 |
|
| 145 |
}; |
| 146 |
} |
| 147 |
|
| 148 |
} |