All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scalqa.lang.int.g.Lookup.scala Maven / Gradle / Ivy

There is a newer version: 1.0
Show newest version
package scalqa; package lang; package int; package g; import language.implicitConversions

import scala.collection.immutable.IntMap

trait Lookup[A<:RAW,+B] extends Val.Lookup[A,B]:
  @tn("get_Opt")             def get_?(key: A) : Val.Opt[B]
  /**/              override def get(v: A)     : B           = get_?(v) or (throw ZZ.ME(v.tag))
  @tn("key_Stream") override def key_~         : Stream[A]   = super.key_~.raw

object Lookup:
  /**
  * General void instance request 
  *  
  * It is possible to use general request \\/ to get void instance of this type, thanks to this implicit conversion.
  */
  implicit def implicitRequest[A<:RAW,B](v: \/): Lookup[A,B] = Stable.void

  trait Mutable[A<:RAW,B] extends Lookup[A,B] with Val.Lookup.Mutable[A,B]:
    def put(key: A, value: B) : Unit
    def remove(key: A)        : Val.Opt[B]

  object Mutable:
    implicit inline def implicitRequest[A<:RAW,B](inline v: NEW): Mutable[A,B] = new X.Basic(J.initSize)
    /**
    * ### 
    *  
    * Object [[X]] defines standard parent type extensions
    */
    object X:
      class Basic[A<:RAW,B](iSz: Int) extends Lookup.Mutable[A,B]:
        private val real = new collection.mutable.LongMap[B](iSz)
        /**/               def size           : Int          = real.size
        @tn("get_Opt")     def get_?(key: A)  : Val.Opt[B]   = Val.Opt.fromScala(real.get(key.real))
        @tn("pair_Stream") def pair_~         : ~[(A, B)]    = real.iterator.~.map((k,v)=>(k.cast[A],v))
        /**/               def put(k: A, v: B): Unit         = real.update(k.real,v)
        /**/               def clear          : Unit         = real.clear
        /**/               def remove(k: A)   : Val.Opt[B]   = Val.Opt.fromScala(real.remove(k.real))

  // ************************************************************************************************************
  class Stable[A<:RAW,B] private(real: IntMap[B]) extends Val.Lookup.Stable[A,B] with Lookup[A,B]:
    type THIS_TYPE = Stable[A,B]
    /**/                       def size                  : Int            = real.size
    @tn("get_Opt")             def get_?(key: A)         : Val.Opt[B]     = Val.Opt.fromScala(real.get(key.real))
    @tn("key_Stream") override def key_~                 : Stream[A]      = real.keysIterator.~.raw.map(_.cast[A])
    @tn("pair_Stream")         def pair_~                : ~[(A, B)]      = real.~.map(v => (v._1.cast[A],v._2))
    /**/                       def join(k: A, v: B)      : Stable[A,B] = new Stable(real.updated(k.real.Int, v))
    /**/                       def joinAll(v: ~[(A, B)]) : Stable[A,B] = new Stable(real.concat(v.map(v => (v._1.real.Int,v._2)).iterator))

  object Stable:
    /**/            def apply[A<:RAW,B](v: (A,B) *)      : Stable[A,B] = apply(v.~)
    /**/            def apply[A<:RAW,B](v: ~[(A,B)])     : Stable[A,B] = new Stable(IntMap.from(v.map(v => (v._1.real.Int,v._2)).iterator))
    /**
    * Get void instance
    */
    @tn("getVoid")  def void[A<:RAW,B]                   : Stable[A,B] = zVoid.cast[Stable[A,B]]; private object zVoid extends Stable(IntMap.empty) with Gen.Void
    implicit inline def implicitRequest[A<:RAW,B](inline v: \/): Stable[A,B] = void

/*___________________________________________________________________________
    __________ ____   __   ______  ____
   /  __/ ___// _  | / /  / __  / / _  |             Scala Quick API
 __\  \/ /___/ __  |/ /__/ /_/ /_/ __  |   (c) 2021, Scalqa.org Inc
/_____/\____/_/  |_/____/\______/_/  |_|             github.com/scalqa
___________________________________________________________________________*/




© 2015 - 2024 Weber Informatics LLC | Privacy Policy