Skip to content

Commit

Permalink
Add Strip.Decomposition method. (#52)
Browse files Browse the repository at this point in the history
Adds a method `Strip.Decomposition` analogous to `Grid.Decomposition`.
  • Loading branch information
aaron-siegel authored Dec 23, 2023
1 parent 3e3c755 commit 7ea7938
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ system class Strip
*/
external def op[](index as Integer) as Integer;

/** The decomposition of this `Strip` into connected components separated by entries with the specified `boundaryValue`.
* The result will be a `List` of `Strip`s, each corresponding to a single nonempty connected component of the
* original `Strip`.
*/
external def Decomposition(boundaryValue as Integer) as List;

/** A `List` of all indices at which this `Strip` has the specified value. */
external def FindAll(value as Integer) as List;

Expand Down
14 changes: 14 additions & 0 deletions lib/core/src/main/scala/org/cgsuite/util/Strip.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ class Strip private[util] (private val values: Array[Byte]) extends Serializable
new Strip(newValues)
}

def decomposition(boundaryValue: Integer): IndexedSeq[Strip] = {
val boundaryValueInt = boundaryValue.intValue
val boundaryIndices = values.indices filter { values(_) == boundaryValueInt }
val tail = boundaryIndices.indices collect {
case k if boundaryIndices(k) + 1 < values.length && values(boundaryIndices(k) + 1) != boundaryValueInt =>
new Strip(values.slice(boundaryIndices(k) + 1, if (k + 1 < boundaryIndices.length) boundaryIndices(k + 1) else values.length))
}
if (values.isEmpty || values(0) == boundaryValueInt)
tail
else
new Strip(values.slice(0, if (boundaryIndices.isEmpty) values.length else boundaryIndices(0))) +: tail

}

def findAll(value: Integer): IndexedSeq[Integer] = {
val byte = value.intValue.toByte
var cnt = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class CgsuiteUtilTest extends CgscriptSpec {
("Empty", "Strip.Empty(8)", "Strip([0,0,0,0,0,0,0,0])"),
("Parse", """strip := Strip.Parse(".abbd.", ".abcd")""", "Strip([0,1,2,2,4,0])"),
("op []", "strip[3]", "2"),
("Decomposition", """strip.Decomposition(2)""", "[Strip([0,1]),Strip([4,0])]"),
("Decomposition edges", """strip.Decomposition(0)""", "[Strip([1,2,2,4])]"),
("Decomposition empty", """Strip.Empty(0).Decomposition(0)""", "[]"),
("FindAll", "strip.FindAll(2)", "[3,4]"),
("Length", "strip.Length", "6"),
("ToString", """strip.ToString(".abcd")""", "\".abbd.\""),
Expand Down

0 comments on commit 7ea7938

Please sign in to comment.