Scala 中以案例类作为键的高效映射?

发布于 2024-10-14 10:18:19 字数 391 浏览 6 评论 0原文

以下 C 代码使用枚举和数组作为从枚举到任何内容的高效“映射”:

enum Color { ColorRed, ColorGreen, ColorBlue, ColorSize};


void f() {
  int x[ColorSize];
  x[ColorRed]   = 12;
  x[ColorGreen] = 33;
  x[ColorBlue]  = 4;
  return x[ColorGreen];
}

这对于 Scala 来说可能吗?
即有一个从案例类到某事物的“映射”,以高效数组而不是树或散列图的形式实现。然而我希望能够仅使用特定类型而不是 Int 进行索引。

更新:简而言之,我希望 Scala 数组能够通过某种枚举(案例类或枚举)进行索引。

A following C code uses enum and array as efficient "map" from enum to anything:

enum Color { ColorRed, ColorGreen, ColorBlue, ColorSize};


void f() {
  int x[ColorSize];
  x[ColorRed]   = 12;
  x[ColorGreen] = 33;
  x[ColorBlue]  = 4;
  return x[ColorGreen];
}

Is this possible with Scala?
I.e. to have a "map" from case class to something, implemented as efficient array and not as tree or as hashmap. Yet I would like to be able to index only with a paricular type not with Int.

Update: In short I would like to have Scala Array indexed by some kind of enum (case class or Enumeration).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴情换悲伤 2024-10-21 10:18:19

对于小型枚举,您可以“模拟”C 行为:

abstract sealed class Color(val index: Int)

object Color {
  implicit def col2int(color:Color) = color.index
}

case object ColorRed extends Color(0)
case object ColorGreen extends Color(1)
case object ColorBlue extends Color(2)

...

import Color._
val array = Array(1,2,3)
array(ColorRed) = 12

但是,我怀疑这是否会被认为是好的风格,特别是因为它不安全。使用地图是更好的方法,或者您可以将数组包装在处理颜色索引的专用数据结构中:

class ColorArray[T:ClassManifest] {
  val array = new Array[T] (3)
  def apply(color: Color) = array(color.index)
  def update(color: Color, value: T) = array(color.index) = value
}

...

val cArray = new ColorArray[Int]()
cArray(ColorRed) = 12
println(cArray(ColorRed))

For small enumerations you can "simulate" the C behavior:

abstract sealed class Color(val index: Int)

object Color {
  implicit def col2int(color:Color) = color.index
}

case object ColorRed extends Color(0)
case object ColorGreen extends Color(1)
case object ColorBlue extends Color(2)

...

import Color._
val array = Array(1,2,3)
array(ColorRed) = 12

However, I doubt this would be considered good style, especially because it's unsafe. Using a map is a better approach, or you could wrap an array in a specialized data structure which deals with Color indizes:

class ColorArray[T:ClassManifest] {
  val array = new Array[T] (3)
  def apply(color: Color) = array(color.index)
  def update(color: Color, value: T) = array(color.index) = value
}

...

val cArray = new ColorArray[Int]()
cArray(ColorRed) = 12
println(cArray(ColorRed))
若水般的淡然安静女子 2024-10-21 10:18:19
object Color extends Enumeration{
  val ColorRed, ColorGreen, ColorBlue = Value
}

import Color._
def f:Map[Color.Value,Int] = 
  Map(ColorRed -> 12 , ColorGreen -> 33, ColorBlue -> 4)

object Color extends Enumeration{
  val ColorRed, ColorGreen, ColorBlue = Value
}

import Color._
def f:Map[Color.Value,Int] = 
  Map(ColorRed -> 12 , ColorGreen -> 33, ColorBlue -> 4)

过潦 2024-10-21 10:18:19

如果你想要完整的 C 性能,你可以这样做:

trait CEnum {
private var size = 0;
def value = { size += 1; size-1 }
}

object Color extends CEnum {
  val colorRed = value 
  val colorGreen = value 
  val colorBlue = value 
  val colorSize = 3
}

import Color._

def f() = {
  val x = Array[Int](colorSize)
  x(colorRed) = 12
  x(colorGreen) = 33
  x(colorBlue) = 4
  x(colorGreen)
}

它与 C & 中的方法同样不安全。一样高效。然而,这是非常不安全的。

If you want the full C performance you could do this:

trait CEnum {
private var size = 0;
def value = { size += 1; size-1 }
}

object Color extends CEnum {
  val colorRed = value 
  val colorGreen = value 
  val colorBlue = value 
  val colorSize = 3
}

import Color._

def f() = {
  val x = Array[Int](colorSize)
  x(colorRed) = 12
  x(colorGreen) = 33
  x(colorBlue) = 4
  x(colorGreen)
}

It's equally unsafe as the method in C & just as performant. It is however very unsafe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文