Sha256: e0992fafb21a5821cdf2ec897f3c5c6e50161158f6bfc03f7a900293afb756bb

Contents?: true

Size: 1.37 KB

Versions: 282

Compression:

Stored size: 1.37 KB

Contents

import scala.annotation.tailrec

object VariableLengthQuantity {
  private val sevenBitsMask = 0x7f
  private val eightBitMask = 0x80

  def encode(n: Int): List[Int] = {
    @tailrec
    def encode_internal(n: Int, acc: List[Int]): List[Int] = {
      if (n == 0) {
        acc
      } else {
        var tmp = n & sevenBitsMask
        if (acc.nonEmpty) {
          tmp = tmp | eightBitMask
        }

        encode_internal(n >>> 7, tmp :: acc)
      }
    }

    if (n == 0)
      List(0)
    else
      encode_internal(n, List())
  }

  def encode(nums: List[Int]): List[Int] = {
    nums.foldRight(List[Int]())((n, acc) => encode(n) ::: acc)
  }

  def decode(encoded: List[Int]): Either[String, List[Int]] = {
    @tailrec
    def decode_internal(encoded: List[Int], tmp: Int, acc: List[Int]): Either[String, List[Int]] = {
      encoded match {
        case Nil => if (tmp == 0 && acc.nonEmpty)
          Right(acc.reverse)
        else
          Left("Incomplete sequence")
        case x::xs =>
          if ((tmp & 0xfe000000) > 0) {
            Left("Overflow")
          } else {
            val newTmp = (tmp << 7) | (x & sevenBitsMask)
            if ((x & eightBitMask) == 0) {
              decode_internal(xs, 0, newTmp :: acc)
            } else {
              decode_internal(xs, newTmp, acc)
            }
          }
      }
    }

    decode_internal(encoded, 0, List())
  }
}

Version data entries

282 entries across 282 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/scala/exercises/variable-length-quantity/example.scala
trackler-2.2.1.179 tracks/scala/exercises/variable-length-quantity/example.scala
trackler-2.2.1.178 tracks/scala/exercises/variable-length-quantity/example.scala
trackler-2.2.1.177 tracks/scala/exercises/variable-length-quantity/example.scala
trackler-2.2.1.176 tracks/scala/exercises/variable-length-quantity/example.scala
trackler-2.2.1.175 tracks/scala/exercises/variable-length-quantity/example.scala
trackler-2.2.1.174 tracks/scala/exercises/variable-length-quantity/example.scala
trackler-2.2.1.173 tracks/scala/exercises/variable-length-quantity/example.scala
trackler-2.2.1.172 tracks/scala/exercises/variable-length-quantity/example.scala
trackler-2.2.1.171 tracks/scala/exercises/variable-length-quantity/example.scala
trackler-2.2.1.170 tracks/scala/exercises/variable-length-quantity/example.scala
trackler-2.2.1.169 tracks/scala/exercises/variable-length-quantity/example.scala
trackler-2.2.1.167 tracks/scala/exercises/variable-length-quantity/example.scala
trackler-2.2.1.166 tracks/scala/exercises/variable-length-quantity/example.scala
trackler-2.2.1.165 tracks/scala/exercises/variable-length-quantity/example.scala
trackler-2.2.1.164 tracks/scala/exercises/variable-length-quantity/example.scala
trackler-2.2.1.163 tracks/scala/exercises/variable-length-quantity/example.scala
trackler-2.2.1.162 tracks/scala/exercises/variable-length-quantity/example.scala
trackler-2.2.1.161 tracks/scala/exercises/variable-length-quantity/example.scala
trackler-2.2.1.160 tracks/scala/exercises/variable-length-quantity/example.scala