Skip to content

Commit e444958

Browse files
authored
Merge pull request scala#6762 from pathikrit/2.13.x
DRY up some common remove utils from mutable.Queue to ArrayDeque
2 parents 8172a85 + 03f7bcf commit e444958

File tree

2 files changed

+57
-36
lines changed

2 files changed

+57
-36
lines changed

src/library/scala/collection/mutable/ArrayDeque.scala

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,43 @@ class ArrayDeque[A] protected (
367367
elems.result()
368368
}
369369

370+
/** Returns the first element which satisfies the given predicate after or at some start index
371+
* and removes this element from the collections
372+
*
373+
* @param p the predicate used for choosing the first element
374+
* @param from the start index
375+
* @return the first element of the queue for which p yields true
376+
*/
377+
def removeFirst(p: A => Boolean, from: Int = 0): Option[A] = {
378+
val i = indexWhere(p, from)
379+
if (i < 0) None else Some(remove(i))
380+
}
381+
382+
/** Returns all elements in this collection which satisfy the given predicate
383+
* and removes those elements from this collections.
384+
*
385+
* @param p the predicate used for choosing elements
386+
* @return a sequence of all elements in the queue for which
387+
* p yields true.
388+
*/
389+
def removeAll(p: A => Boolean): scala.collection.immutable.Seq[A] = {
390+
val res = scala.collection.immutable.Seq.newBuilder[A]
391+
var i, j = 0
392+
while (i < size) {
393+
if (p(this(i))) {
394+
res += this(i)
395+
} else {
396+
if (i != j) {
397+
this(j) = this(i)
398+
}
399+
j += 1
400+
}
401+
i += 1
402+
}
403+
if (i != j) takeInPlace(j)
404+
res.result()
405+
}
406+
370407
override def reverse: IterableCC[A] = {
371408
val n = length
372409
val arr = ArrayDeque.alloc(n)
@@ -399,7 +436,7 @@ class ArrayDeque[A] protected (
399436
}
400437

401438
/**
402-
* clears this buffer and shrinks to @param size
439+
* Clears this buffer and shrinks to @param size
403440
*
404441
* @param size
405442
* @return

src/library/scala/collection/mutable/Queue.scala

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -76,41 +76,25 @@ class Queue[A] protected (array: Array[AnyRef], start: Int, end: Int)
7676
* @return the first element of the queue for which p yields true
7777
*/
7878
def dequeueFirst(p: A => Boolean): Option[A] =
79-
if (isEmpty) None
80-
else if (p(head)) {
81-
val res = Some(head)
82-
removeHead()
83-
res
84-
} else {
85-
val i = indexWhere(p)
86-
if (i < 0) None
87-
else Some(remove(i))
88-
}
89-
90-
/** Returns all elements in the queue which satisfy the
91-
* given predicate, and removes those elements from the queue.
92-
*
93-
* @param p the predicate used for choosing elements
94-
* @return a sequence of all elements in the queue for which
95-
* p yields true.
96-
*/
97-
def dequeueAll(p: A => Boolean): scala.collection.immutable.Seq[A] = {
98-
val res = scala.collection.immutable.Seq.newBuilder[A]
99-
var i, j = 0
100-
while (i < size) {
101-
if (p(apply(i))) {
102-
res += this(i)
103-
} else {
104-
if (i != j) {
105-
this(j) = this(i)
106-
}
107-
j += 1
108-
}
109-
i += 1
110-
}
111-
if (i != j) takeInPlace(j)
112-
res.result()
113-
}
79+
removeFirst(p)
80+
81+
/** Returns all elements in the queue which satisfy the
82+
* given predicate, and removes those elements from the queue.
83+
*
84+
* @param p the predicate used for choosing elements
85+
* @return a sequence of all elements in the queue for which
86+
* p yields true.
87+
*/
88+
def dequeueAll(p: A => Boolean): scala.collection.immutable.Seq[A] =
89+
removeAll(p)
90+
91+
/**
92+
* Returns and dequeues all elements from the queue which satisfy the given predicate
93+
*
94+
* @param f the predicate used for choosing elements
95+
* @return The removed elements
96+
*/
97+
def dequeueWhile(f: A => Boolean): scala.collection.Seq[A] = removeHeadWhile(f)
11498

11599
/** Returns the first element in the queue, or throws an error if there
116100
* is no element contained in the queue.

0 commit comments

Comments
 (0)