Skip to content

Instantly share code, notes, and snippets.

@beyondeye
Created October 28, 2015 08:51
Show Gist options
  • Save beyondeye/0b682229c7759bd82caf to your computer and use it in GitHub Desktop.
Save beyondeye/0b682229c7759bd82caf to your computer and use it in GitHub Desktop.
A prototype 2D array implementation
class IntArray2D: Cloneable
{
val xsize:Int
val ysize:Int
val _array:IntArray
constructor(xsize:Int, ysize:Int){
this.xsize=xsize
this.ysize=ysize
_array = IntArray(xsize*ysize)
}
constructor(src:IntArray2D) {
this.xsize=src.xsize
this.ysize=src.ysize
this._array = src._array.clone()
}
/** Returns the array element at the given [index]. This method can be called using the index operator. */
public operator fun get(index: Int): Int = _array[index]
/** Returns the array element at the given [xIndex],[yIndex] position. This method can be called using the index operator. */
public operator fun get(xIndex: Int, yIndex:Int): Int = _array[xIndex *ysize+yIndex]
/** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */
public operator fun set(index: Int, value: Int): Unit {
_array[index]=value
}
/** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */
public operator fun set(xIndex: Int, yIndex: Int, value: Int): Unit {
_array[xIndex *ysize+yIndex]=value
}
/** Returns the number of elements in the array. */
public val size: Int
get() =_array.size
/** Creates an iterator over the elements of the array. */
public operator fun iterator(): IntIterator = _array.iterator()
public override fun clone(): IntArray2D {
return IntArray2D(this)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment