@@ -8,53 +8,64 @@ class Array2d<Type>(
88
99 private val array: MutableList <MutableList <Type >> = MutableList (height) { MutableList (width) { defaultValue } }
1010
11+ operator fun get (point : Point ) = get(point.x, point.y)
1112 operator fun get (x : Int , y : Int ): Type = array[y][x]
13+
14+ fun getSafe (point : Point ) = getSafe(point.x, point.y)
15+ fun getSafe (x : Int , y : Int ) = if (isInBounds(x, y)) array[y][x] else null
16+
17+ operator fun set (point : Point , value : Type ) = set(point.x, point.y, value)
1218 operator fun set (x : Int , y : Int , value : Type ) {
1319 array[y][x] = value
1420 }
1521
16- operator fun set (point : Point , value : Type ) = set(point.x, point.y, value)
17-
18- operator fun get (point : Point ) = get(point.x, point.y)
19-
2022 fun setSafe (point : Point , value : Type ) = setSafe(point.x, point.y, value)
2123 fun setSafe (x : Int , y : Int , value : Type ) {
2224 if (isInBounds(x, y)) array[y][x] = value
2325 }
2426
25- fun getSafe (point : Point ) = getSafe (point.x, point.y)
26- fun getSafe (x : Int , y : Int ) = if (isInBounds(x, y)) array[y][x] else null
27+ fun isInBounds (point : Point ) = isInBounds (point.x, point.y)
28+ fun isInBounds (x : Int , y : Int ) = x in 0 until width && y in 0 until height
2729
28- fun forEach ( action : (x: Int , y: Int , value: Type ) -> Unit ) {
30+ fun findAny ( filter : (Type ) -> Boolean ): Point ? {
2931 for (y in array.indices) {
3032 for (x in array[y].indices) {
31- action(x, y, array[y][x])
33+ if (filter( array[y][x])) return Point (x, y )
3234 }
3335 }
36+ return null
3437 }
3538
36- fun getPositions (value : Type ): List <Point > {
37- return array.mapIndexed { y, list -> list.mapIndexed { x, v -> if (v == value) Point (x, y) else null } }
38- .flatten()
39- .filterNotNull()
39+ fun findAll (filter : (Type ) -> Boolean ): List <Point > {
40+ val findings = mutableListOf<Point >()
41+ for (y in array.indices) {
42+ for (x in array[y].indices) {
43+ if (filter(array[y][x])) findings.add(Point (x, y))
44+ }
45+ }
46+ return findings
4047 }
4148
42- fun isInBounds (point : Point ) = isInBounds(point.x, point.y)
49+ fun forEach (action : (x: Int , y: Int , value: Type ) -> Unit ) {
50+ for (y in array.indices) {
51+ for (x in array[y].indices) {
52+ action(x, y, array[y][x])
53+ }
54+ }
55+ }
4356
44- fun isInBounds ( x : Int , y : Int ) = x in 0 until width && y in 0 until height
57+ fun count ( filter : ( Type ) -> Boolean ) : Int = array.sumOf { it.count { filter(it) } }
4558
4659 fun createMask (): Array2d <Boolean > = Array2d (width, height, false )
4760
48- fun count (filter : (Type ) -> Boolean ): Int = array.sumOf { it.count { filter(it) } }
49-
5061 fun print (mapper : (Type ) -> Char ) {
5162 for (y in array.indices) {
5263 for (x in array[y].indices) {
53- print (mapper(array[x][y ]))
64+ print (mapper(array[y][x ]))
5465 }
55- println (" " )
66+ println ()
5667 }
57- println (" " )
68+ println ()
5869 }
5970
6071 companion object {
0 commit comments