|
| 1 | +/* |
| 2 | + * Copyright contributors to Hyperledger Besu |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + * |
| 13 | + * SPDX-License-Identifier: Apache-2.0 |
| 14 | + * |
| 15 | + */ |
| 16 | +package org.hyperledger.besu.collections.trie; |
| 17 | + |
| 18 | +import java.util.AbstractSet; |
| 19 | +import java.util.ArrayDeque; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Deque; |
| 22 | +import java.util.Iterator; |
| 23 | +import java.util.NoSuchElementException; |
| 24 | +import java.util.Objects; |
| 25 | + |
| 26 | +import org.apache.tuweni.bytes.Bytes; |
| 27 | + |
| 28 | +/** |
| 29 | + * A Bytes optimized set that stores values in a trie by byte |
| 30 | + * |
| 31 | + * @param <E> Type of trie |
| 32 | + */ |
| 33 | +public class BytesTrieSet<E extends Bytes> extends AbstractSet<E> { |
| 34 | + |
| 35 | + record Node<E extends Bytes>(byte[] leafArray, E leafObject, Node<E>[] children) { |
| 36 | + |
| 37 | + @Override |
| 38 | + public boolean equals(final Object o) { |
| 39 | + if (this == o) return true; |
| 40 | + if (!(o instanceof Node<?> node)) return false; |
| 41 | + return Arrays.equals(leafArray, node.leafArray) |
| 42 | + && Objects.equals(leafObject, node.leafObject) |
| 43 | + && Arrays.equals(children, node.children); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public int hashCode() { |
| 48 | + int result = Objects.hash(leafObject); |
| 49 | + result = 31 * result + Arrays.hashCode(leafArray); |
| 50 | + result = 31 * result + Arrays.hashCode(children); |
| 51 | + return result; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public String toString() { |
| 56 | + final StringBuilder sb = new StringBuilder("Node{"); |
| 57 | + sb.append("leaf="); |
| 58 | + if (leafObject == null) sb.append("null"); |
| 59 | + else { |
| 60 | + sb.append('['); |
| 61 | + System.out.println(leafObject.toHexString()); |
| 62 | + sb.append(']'); |
| 63 | + } |
| 64 | + sb.append(", children="); |
| 65 | + if (children == null) sb.append("null"); |
| 66 | + else { |
| 67 | + sb.append('['); |
| 68 | + for (int i = 0; i < children.length; ++i) { |
| 69 | + if (children[i] == null) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + sb.append(i == 0 ? "" : ", ").append(i).append("=").append(children[i]); |
| 73 | + } |
| 74 | + sb.append(']'); |
| 75 | + } |
| 76 | + sb.append('}'); |
| 77 | + return sb.toString(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + Node<E> root; |
| 82 | + |
| 83 | + int size = 0; |
| 84 | + final int byteLength; |
| 85 | + |
| 86 | + /** |
| 87 | + * Create a BytesTrieSet with a fixed length |
| 88 | + * |
| 89 | + * @param byteLength length in bytes of the stored types |
| 90 | + */ |
| 91 | + public BytesTrieSet(final int byteLength) { |
| 92 | + this.byteLength = byteLength; |
| 93 | + } |
| 94 | + |
| 95 | + static class NodeWalker<E extends Bytes> { |
| 96 | + final Node<E> node; |
| 97 | + int lastRead; |
| 98 | + |
| 99 | + NodeWalker(final Node<E> node) { |
| 100 | + this.node = node; |
| 101 | + this.lastRead = -1; |
| 102 | + } |
| 103 | + |
| 104 | + NodeWalker<E> nextNodeWalker() { |
| 105 | + if (node.children == null) { |
| 106 | + return null; |
| 107 | + } |
| 108 | + while (lastRead < 255) { |
| 109 | + lastRead++; |
| 110 | + Node<E> child = node.children[lastRead]; |
| 111 | + if (child != null) { |
| 112 | + return new NodeWalker<>(child); |
| 113 | + } |
| 114 | + } |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + E thisNode() { |
| 119 | + return node.leafObject; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public Iterator<E> iterator() { |
| 125 | + var result = |
| 126 | + new Iterator<E>() { |
| 127 | + final Deque<NodeWalker<E>> stack = new ArrayDeque<>(); |
| 128 | + E next; |
| 129 | + E last; |
| 130 | + |
| 131 | + @Override |
| 132 | + public boolean hasNext() { |
| 133 | + return next != null; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public E next() { |
| 138 | + if (next == null) { |
| 139 | + throw new NoSuchElementException(); |
| 140 | + } |
| 141 | + last = next; |
| 142 | + advance(); |
| 143 | + return last; |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public void remove() { |
| 148 | + BytesTrieSet.this.remove(last); |
| 149 | + } |
| 150 | + |
| 151 | + void advance() { |
| 152 | + while (!stack.isEmpty()) { |
| 153 | + NodeWalker<E> thisStep = stack.peek(); |
| 154 | + var nextStep = thisStep.nextNodeWalker(); |
| 155 | + if (nextStep == null) { |
| 156 | + stack.pop(); |
| 157 | + if (thisStep.thisNode() != null) { |
| 158 | + next = thisStep.thisNode(); |
| 159 | + return; |
| 160 | + } |
| 161 | + } else { |
| 162 | + stack.push(nextStep); |
| 163 | + } |
| 164 | + } |
| 165 | + next = null; |
| 166 | + } |
| 167 | + }; |
| 168 | + if (root != null) { |
| 169 | + result.stack.add(new NodeWalker<>(root)); |
| 170 | + } |
| 171 | + result.advance(); |
| 172 | + return result; |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public int size() { |
| 177 | + return size; |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public boolean contains(final Object o) { |
| 182 | + if (!(o instanceof Bytes bytes)) { |
| 183 | + throw new IllegalArgumentException( |
| 184 | + "Expected Bytes, got " + (o == null ? "null" : o.getClass().getName())); |
| 185 | + } |
| 186 | + byte[] array = bytes.toArrayUnsafe(); |
| 187 | + if (array.length != byteLength) { |
| 188 | + throw new IllegalArgumentException( |
| 189 | + "Byte array is size " + array.length + " but set is size " + byteLength); |
| 190 | + } |
| 191 | + if (root == null) { |
| 192 | + return false; |
| 193 | + } |
| 194 | + int level = 0; |
| 195 | + Node<E> current = root; |
| 196 | + while (current != null) { |
| 197 | + if (current.leafObject != null) { |
| 198 | + return Arrays.compare(current.leafArray, array) == 0; |
| 199 | + } |
| 200 | + current = current.children[array[level] & 0xff]; |
| 201 | + level++; |
| 202 | + } |
| 203 | + return false; |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + public boolean remove(final Object o) { |
| 208 | + // Two base cases, size==0 and size==1; |
| 209 | + if (!(o instanceof Bytes bytes)) { |
| 210 | + throw new IllegalArgumentException( |
| 211 | + "Expected Bytes, got " + (o == null ? "null" : o.getClass().getName())); |
| 212 | + } |
| 213 | + byte[] array = bytes.toArrayUnsafe(); |
| 214 | + if (array.length != byteLength) { |
| 215 | + throw new IllegalArgumentException( |
| 216 | + "Byte array is size " + array.length + " but set is size " + byteLength); |
| 217 | + } |
| 218 | + // Two base cases, size==0 and size==1; |
| 219 | + if (root == null) { |
| 220 | + // size==0 is easy, empty |
| 221 | + return false; |
| 222 | + } |
| 223 | + if (root.leafObject != null) { |
| 224 | + // size==1 just check and possibly remove the root |
| 225 | + if (Arrays.compare(array, root.leafArray) == 0) { |
| 226 | + root = null; |
| 227 | + size--; |
| 228 | + return true; |
| 229 | + } else { |
| 230 | + return false; |
| 231 | + } |
| 232 | + } |
| 233 | + int level = 0; |
| 234 | + Node<E> current = root; |
| 235 | + do { |
| 236 | + int index = array[level] & 0xff; |
| 237 | + Node<E> next = current.children[index]; |
| 238 | + if (next == null) { |
| 239 | + return false; |
| 240 | + } |
| 241 | + if (next.leafObject != null) { |
| 242 | + if (Arrays.compare(array, next.leafArray) == 0) { |
| 243 | + // TODO there is no cleanup of empty branches |
| 244 | + current.children[index] = null; |
| 245 | + size--; |
| 246 | + return true; |
| 247 | + } else { |
| 248 | + return false; |
| 249 | + } |
| 250 | + } |
| 251 | + current = next; |
| 252 | + |
| 253 | + level++; |
| 254 | + } while (true); |
| 255 | + } |
| 256 | + |
| 257 | + @SuppressWarnings({"unchecked", "rawtypes"}) |
| 258 | + @Override |
| 259 | + public boolean add(final E bytes) { |
| 260 | + byte[] array = bytes.toArrayUnsafe(); |
| 261 | + if (array.length != byteLength) { |
| 262 | + throw new IllegalArgumentException( |
| 263 | + "Byte array is size " + array.length + " but set is size " + byteLength); |
| 264 | + } |
| 265 | + // Two base cases, size==0 and size==1; |
| 266 | + if (root == null) { |
| 267 | + // size==0 is easy, just add |
| 268 | + root = new Node<>(array, bytes, null); |
| 269 | + size++; |
| 270 | + return true; |
| 271 | + } |
| 272 | + if (root.leafObject != null) { |
| 273 | + // size==1 first check then if no match make it look like n>1 |
| 274 | + if (Arrays.compare(array, root.leafArray) == 0) { |
| 275 | + return false; |
| 276 | + } |
| 277 | + Node<E> oldRoot = root; |
| 278 | + root = new Node<>(null, null, new Node[256]); |
| 279 | + root.children[oldRoot.leafArray[0] & 0xff] = oldRoot; |
| 280 | + } |
| 281 | + int level = 0; |
| 282 | + Node<E> current = root; |
| 283 | + do { |
| 284 | + int index = array[level] & 0xff; |
| 285 | + Node<E> next = current.children[index]; |
| 286 | + if (next == null) { |
| 287 | + next = new Node<>(array, bytes, null); |
| 288 | + current.children[index] = next; |
| 289 | + size++; |
| 290 | + return true; |
| 291 | + } |
| 292 | + if (next.leafObject != null) { |
| 293 | + if (Arrays.compare(array, next.leafArray) == 0) { |
| 294 | + return false; |
| 295 | + } |
| 296 | + Node<E> newLeaf = new Node<>(null, null, new Node[256]); |
| 297 | + newLeaf.children[next.leafArray[level + 1] & 0xff] = next; |
| 298 | + current.children[index] = newLeaf; |
| 299 | + next = newLeaf; |
| 300 | + } |
| 301 | + level++; |
| 302 | + |
| 303 | + current = next; |
| 304 | + |
| 305 | + } while (true); |
| 306 | + } |
| 307 | + |
| 308 | + @Override |
| 309 | + public void clear() { |
| 310 | + root = null; |
| 311 | + } |
| 312 | +} |
0 commit comments