-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,105 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/main/scala/com/raquo/airstream/split/MutableSplittable.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.raquo.airstream.split | ||
|
||
import com.raquo.ew.JsArray | ||
|
||
import scala.collection.mutable | ||
import scala.scalajs.js | ||
|
||
trait MutableSplittable[M[_]] { | ||
|
||
val splittable: Splittable[M] | ||
|
||
def size[A](items: M[A]): Int | ||
|
||
/** Equivalent of array(index) */ | ||
def getByIndex[A](items: M[A], index: Int): A | ||
|
||
/** Equivalent of array.update(index, newValue) */ | ||
def updateAtIndex[A](items: M[A], index: Int, newItem: A): Unit | ||
|
||
/** Equivalent of array.find(predicate).foreach(array.update(foundIndex, newItem)) */ | ||
def findUpdateInPlace[A](items: M[A], predicate: A => Boolean, newItem: A): Boolean = { | ||
// #Warning: This implementation assumes cheap O(1) index access. | ||
// Override this method for efficiency as needed (e.g. for ListBuffer – see below). | ||
var found = false | ||
var index = 0 | ||
val len = size(items) | ||
while (!found && index < len) { | ||
if (predicate(getByIndex(items, index))) { | ||
updateAtIndex(items, index, newItem) | ||
found = true | ||
} | ||
index += 1 | ||
} | ||
found | ||
} | ||
} | ||
|
||
object MutableSplittable { | ||
|
||
implicit object JsArrayMutableSplittable extends MutableSplittable[JsArray] { | ||
|
||
override val splittable: Splittable[JsArray] = Splittable.JsArraySplittable | ||
|
||
override def size[A](items: JsArray[A]): Int = items.length | ||
|
||
override def getByIndex[A](items: JsArray[A], index: Int): A = items(index) | ||
|
||
override def updateAtIndex[A](items: JsArray[A], index: Int, newItem: A): Unit = { | ||
items.update(index, newItem) | ||
} | ||
} | ||
|
||
implicit object ScalaJsArrayMutableSplittable extends MutableSplittable[js.Array] { | ||
|
||
override val splittable: Splittable[js.Array] = Splittable.ScalaJsArraySplittable | ||
|
||
override def size[A](items: js.Array[A]): Int = items.length | ||
|
||
override def getByIndex[A](items: js.Array[A], index: Int): A = items(index) | ||
|
||
override def updateAtIndex[A](items: js.Array[A], index: Int, newItem: A): Unit = { | ||
items.update(index, newItem) | ||
} | ||
} | ||
|
||
implicit object BufferMutableSplittable extends MutableSplittable[mutable.Buffer] { | ||
|
||
override val splittable: Splittable[mutable.Buffer] = Splittable.BufferSplittable | ||
|
||
override def size[A](items: mutable.Buffer[A]): Int = items.size | ||
|
||
override def getByIndex[A](items: mutable.Buffer[A], index: Int): A = items(index) | ||
|
||
override def updateAtIndex[A](items: mutable.Buffer[A], index: Int, newItem: A): Unit = { | ||
items.update(index, newItem) | ||
} | ||
|
||
override def findUpdateInPlace[A]( | ||
items: mutable.Buffer[A], | ||
predicate: A => Boolean, | ||
newItem: A | ||
): Boolean = { | ||
items match { | ||
case _: mutable.ListBuffer[A @unchecked] => | ||
// This implementation is more efficient when accessing elements by index is costly. | ||
val iterator = items.iterator | ||
var found = false | ||
var index = 0 | ||
while (!found && iterator.hasNext) { | ||
if (predicate(iterator.next())) { | ||
updateAtIndex(items, index, newItem) | ||
found = true | ||
} | ||
index += 1 | ||
} | ||
found | ||
case _ => | ||
// All other Buffer implementations are indexed (I think) | ||
super.findUpdateInPlace(items, predicate, newItem) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.