alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (Matrix.scala)

This example Scala source code file (Matrix.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

a, a, array, flatseq, indexoutofboundsexception, indexoutofboundsexception, int, int, matrix, matrix, matrixtoseqs, randomaccessseq, seq, unit

The Scala Matrix.scala source code

/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2007-2011, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */


package scala.swing
package model

// Dummy to keep ant from recompiling on every run.
trait Matrix { }

/*trait Matrix[A] extends Function2[Int, Int, A] {
  
  val width: Int
  val height: Int
  
  assert(width > 0 && height > 0)
  
  private val delegate = new Array[A](width * height)
  
  override def apply(col: Int, row: Int): A =
    delegate(col * height + row)
    
  def apply(coord: (Int, Int)): A = 
    apply(coord._1, coord._2)
  
  def col(index: Int): Matrix.FlatSeq[A] =
    new Matrix.SubArray[A](delegate, index * height, height)
  
  def row(index: Int): Matrix.FlatSeq[A] =
    new Matrix.SparseArray[A](delegate, index, height)
  
  def update(xpos: Int, ypos: Int, elem: A) {
    delegate(xpos % width * height + ypos % height) = elem
  }
  
  def update(coord: (Int, Int), elem: A) {
    update(coord._1, coord._2, elem)
  }
  
  def initializeWith(f: (Int, Int) => A): this.type = {
    for (index <- 0 until (width * height))
      delegate(index) = f(index / height, index % height)
    this
  }
  
  def initializeTo(v: => A): this.type = {
    for (index <- 0 until (width * height))
      delegate(index) = v
    this
  }
  
  def size: (Int, Int) = (width, height)
  
  /** A flattened view of the matrix. The flattening is done on columns i.e.
    * the first values of the flattened sequence are the cells of the first
    * column. As this is a view of the matrix, any change to the matrix will
    * also be visible in the flattened array, and vice-versa. */
  def flat: Array[A] = delegate
  
}

object Matrix {
  
  def apply[A](columns: Int, rows: Int) = new Matrix[A] {
    val width = columns
    val height = rows
  }
  
  def apply[A](default: (Int, Int) => A, columns: Int, rows: Int) = new Matrix[A] {
    val width = columns
    val height = rows
    initializeWith(default)
  }
  
  def apply[A](default: => A, columns: Int, rows: Int) = new Matrix[A] {
    val width = columns
    val height = rows
    initializeTo(default)
  }
  
  trait FlatSeq[A] extends RandomAccessSeq[A] {
    def        update (index: Int, elem: A): Unit
  }
  
  private class SubArray[A](delegate: Array[A], start: Int, val length: Int) extends FlatSeq[A] {
    def apply(index: Int): A =
      if (index < length)
        delegate(index + start)
      else throw new IndexOutOfBoundsException
    def update(index: Int, elem: A): Unit =
      if (index < length)
        delegate(index + start) = elem
      else throw new IndexOutOfBoundsException
  }
  
  private class SparseArray[A](delegate: Array[A], start: Int, span: Int) extends FlatSeq[A] {
    def apply(index: Int): A = {
      if (index < length)
        delegate((index * span) + start)
      else throw new IndexOutOfBoundsException
    }
    def length: Int = delegate.length / span
    def update(index: Int, elem: A): Unit =
      if (index < length)
        delegate((index * span) + start) = elem
      else throw new IndexOutOfBoundsException
  }
  
  implicit def MatrixToSeqs[A](matrix: Matrix[A]): Seq[Seq[A]] = {
    val result = new Array[SubArray[A]](matrix.width)
    for (col <- 0 until matrix.width)
      result(col) = new SubArray[A](matrix.delegate, col * matrix.height, matrix.height)
    result
  }
  
}*/

Other Scala examples (source code examples)

Here is a short list of links related to this Scala Matrix.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.