Scoop API

scoop:scoop.std:0.1.0

src/collections/array_deque.scoop

Selected production source · src/collections/array_deque.scoop

1package scoop.collections
3/**
4 * A growable double-ended queue with constant-time indexed ring-buffer access at either end.
5 * Structural changes invalidate active iterators.
6 */
7public class ArrayDeque<T> public constructor() : Collection<T> {
8 private var storage: ElementBuffer<T> = ElementBuffer<T>(8L)
9 private var head: Long = 0L
10 private var elementCount: Long = 0L
11 private val structure: ModificationCounter = ModificationCounter()
12 /**
13 * The number of queued elements.
14 */
15 public override val size: Long get() = elementCount
17 private fun position(index: Long): Long {
18 val tailSpace = storage.capacity - head
19 return if (index < tailSpace) head + index else index - tailSpace
20 }
22 internal fun elementAt(index: Long): T = storage.read(position(index))
24 private fun ensureRoom() {
25 if (elementCount < storage.capacity) return
26 val replacement = ElementBuffer<T>(expandedCollectionCapacity(storage.capacity))
27 var index = 0L
28 while (index < elementCount) { replacement.write(index, elementAt(index)); index += 1L }
29 storage = replacement
30 head = 0L
31 }
33 /**
34 * Prepends an element to the queue.
35 */
36 public fun addFirst(element: T) {
37 ensureRoom()
38 head = if (head == 0L) storage.capacity - 1L else head - 1L
39 storage.write(head, element)
40 elementCount += 1L
41 structure.advance()
42 }
44 /**
45 * Appends an element to the queue.
46 */
47 public fun addLast(element: T) {
48 ensureRoom()
49 storage.write(position(elementCount), element)
50 elementCount += 1L
51 structure.advance()
52 }
54 /**
55 * Returns the front element without removing it, or None when empty.
56 */
57 public fun firstOrNone(): T? = if (elementCount == 0L) None else Some(elementAt(0L))
58 /**
59 * Returns the back element without removing it, or None when empty.
60 */
61 public fun lastOrNone(): T? = if (elementCount == 0L) None else Some(elementAt(elementCount - 1L))
63 /**
64 * Removes and returns the front element; an empty queue throws NoSuchElementException.
65 */
66 public fun removeFirst(): T {
67 if (elementCount == 0L) throw NoSuchElementException("empty deque")
68 val value = storage.read(head)
69 storage.erase(head)
70 head = if (head + 1L == storage.capacity) 0L else head + 1L
71 elementCount -= 1L
72 if (elementCount == 0L) head = 0L
73 structure.advance()
74 return value
75 }
77 /**
78 * Removes and returns the back element; an empty queue throws NoSuchElementException.
79 */
80 public fun removeLast(): T {
81 if (elementCount == 0L) throw NoSuchElementException("empty deque")
82 val index = position(elementCount - 1L)
83 val value = storage.read(index)
84 storage.erase(index)
85 elementCount -= 1L
86 if (elementCount == 0L) head = 0L
87 structure.advance()
88 return value
89 }
91 /**
92 * Removes the front element, or returns None when empty.
93 */
94 public fun removeFirstOrNone(): T? = if (elementCount == 0L) None else Some(removeFirst())
95 /**
96 * Removes the back element, or returns None when empty.
97 */
98 public fun removeLastOrNone(): T? = if (elementCount == 0L) None else Some(removeLast())
100 /**
101 * Removes all queued elements.
102 */
103 public fun clear() {
104 if (elementCount == 0L) return
105 var index = 0L
106 while (index < elementCount) { storage.erase(position(index)); index += 1L }
107 head = 0L
108 elementCount = 0L
109 structure.advance()
110 }
112 /**
113 * Iterates from front to back; structural changes invalidate the iterator.
114 */
115 public override operator fun iterator(): Iterator<T> = DequeIterator<T>(this, structure)
117 /**
118 * Serialization factories using explicit element or key/value codecs.
119 */
120 public companion object {
121 /**
122 * Encodes entries or elements in iteration order using the supplied component codecs.
123 */
124 public fun encoder(element: ValueEncoder<T>): ValueEncoder<ArrayDeque<T>> =
125 ValueEncoderFunction<ArrayDeque<T>> { values, encoder -> encodeElements(values, element, encoder) }
126 /**
127 * Decodes entries or elements into a new collection using the supplied component codecs
128 * and comparator where required.
129 */
130 public fun decoder(element: ValueDecoder<T>): ValueDecoder<ArrayDeque<T>> = ValueDecoderFunction<ArrayDeque<T>> { decoder ->
131 val result = ArrayDeque<T>()
132 decodeElements(decoder, element) { value -> result.addLast(value) }
133 result
134 }
135 }
138internal class DequeIterator<T> internal constructor(
139 private val source: ArrayDeque<T>, private val structure: ModificationCounter
140) : Iterator<T> {
141 private val stamp: RevisionStamp = structure.snapshot()
142 private var index: Long = 0L
143 private var exhausted: Boolean = false
145 public override fun next(): T? {
146 if (exhausted) return None
147 structure.check(stamp)
148 if (index == source.size) { exhausted = true; return None }
149 val value = source.elementAt(index)
150 index += 1L
151 return Some(value)
152 }