@@ -144,11 +144,18 @@ interface Queue<A> : Enqueue<A>, Dequeue1<A>, Dequeue<A> {
144
144
fromStrategy(Strategy .boundedFifo(maxSize))
145
145
146
146
/* * Creates a queue which stores the last `maxSize` enqueued elements and which never blocks on enqueue. */
147
- suspend fun <A > circularBuffer (maxSize : Int ): Queue <A > =
148
- fromStrategy(Strategy .circularBuffer (maxSize))
147
+ suspend fun <A > sliding (maxSize : Int ): Queue <A > =
148
+ fromStrategy(Strategy .sliding (maxSize))
149
149
150
- fun <A > unsafeCircularBuffer (maxSize : Int ): Queue <A > =
151
- fromStrategy(Strategy .circularBuffer(maxSize))
150
+ fun <A > unsafeSliding (maxSize : Int ): Queue <A > =
151
+ fromStrategy(Strategy .sliding(maxSize))
152
+
153
+ /* * Creates a queue which stores the first `maxSize` enqueued elements and which never blocks on enqueue. */
154
+ suspend fun <A > dropping (maxSize : Int ): Queue <A > =
155
+ fromStrategy(Strategy .dropping(maxSize))
156
+
157
+ fun <A > unsafeDropping (maxSize : Int ): Queue <A > =
158
+ fromStrategy(Strategy .dropping(maxSize))
152
159
153
160
/* * Created a bounded queue that distributed always at max `fairSize` elements to any subscriber. */
154
161
suspend fun <A > fairBounded (maxSize : Int , fairSize : Int ): Queue <A > =
@@ -205,11 +212,17 @@ internal object Strategy {
205
212
fun <A > boundedLifo (maxSize : Int ): PubSub .Strategy <A , Chunk <A >, IQueue<A>, Int> =
206
213
PubSub .Strategy .bounded(maxSize, lifo()) { it.size }
207
214
208
- /* * Strategy for circular buffer, which stores the last `maxSize` enqueued elements and never blocks on enqueue. */
209
- fun <A > circularBuffer (maxSize : Int ): PubSub .Strategy <A , Chunk <A >, IQueue<A>, Int> =
215
+ /* * Strategy for sliding, which stores the last `maxSize` enqueued elements and never blocks on enqueue. */
216
+ fun <A > sliding (maxSize : Int ): PubSub .Strategy <A , Chunk <A >, IQueue<A>, Int> =
217
+ unbounded { q: IQueue <A >, a ->
218
+ if (q.size <= maxSize) q.enqueue(a)
219
+ else q.drop(1 ).enqueue(a)
220
+ }
221
+
222
+ /* * Strategy for dropping, which stores the first `maxSize` enqueued elements and never blocks on enqueue. */
223
+ fun <A > dropping (maxSize : Int ): PubSub .Strategy <A , Chunk <A >, IQueue<A>, Int> =
210
224
unbounded { q: IQueue <A >, a ->
211
- if (q.size < maxSize) q.enqueue(a)
212
- else q.tail().enqueue(a)
225
+ if (q.size <= maxSize) q.enqueue(a) else q
213
226
}
214
227
215
228
/* * Unbounded lifo strategy. */
0 commit comments