- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在实现一个类,该类充当依赖于同一数组的三个堆栈(来自 Gayle Laakmann 的 cracking the coding interview)。
我无法理解为什么这段代码会导致类型不匹配:
class ThreeStacks[A, B, C ](val stackSize:Int = 1000) {
/**
* Push a value on to the first stack
* @param value value of type A */
def push1[A](value :A): Unit = stack1.push(value)
/**
* Push a value on to the second stack
* @param value value of type B */
def push2[B](value: B): Unit = stack2.push(value)
/**
* Push a value on to the third stack
* @param value value of type C */
def push3[C](value: C): Unit = stack3.push(value)
/**
* Pops the top value off the first stack
* @return Some(value) at the top of the stack, None if the stack is empty */
def pop1[A](): Option[A] = stack1.pop()
/**
* Pops the top value off the second stack
* @return Some(value) at the top of the stack, None if the stack is empty */
def pop2[B](): Option[B] = stack2.pop()
/**
* Pops the top value off the second stack
* @return Some(value) at the top of the stack, None if the stack is empty */
def pop3[C](): Option[C] = stack3.pop()
// the shared stacks
private val stack1 = SharedStack[A]()
private val stack2 = SharedStack[B]()
private val stack3 = SharedStack[C]()
// the shared stack class is simply a container for push and pop functions
class SharedStack[T](val push: T => Unit, val pop: () => Option[T])
// the sharedstack object is a factory setups a shared stack object with its proper push/pop functions that are received
// from the stackmemory instance
private object SharedStack{
val stackMemory = new StackMemory(stackSize)
def apply[T](): SharedStack[T] = {
val stackTuple = stackMemory.registerStack[T]()
new SharedStack(stackTuple._1, stackTuple._2)
}
}
// the memory object responsible for handling the sharing of the array
private class StackMemory(val stackSize: Int = 1000){
// helps prevent the conflict warnings with immutable and mutable collections
private val MutableMap = scala.collection.mutable.Map
// the array that contains the stack nodes
private val array = new Array[Option[StackNode[Any]]](stackSize)
// keeps track of the heads of all the stacks
private val registeredStackHeads = MutableMap[Int, Int]()
// an index to help assign every new stack a unique key
private var nextRegistrationIndex = 1
// a list of indices in the array that have been freed
private var freedIndices = List[Int]()
// the next available free index, increments till it reaches the end of the array, different from the freeIndices list
// in that it only is concerned with free cells while they are available at the end of the array
private var nextFreeIndex = 0
/**
* Registers a stack within the memory object and returns the push/pop functions
* @return the push/pop functions relevant for that stack */
def registerStack[T](): (T => Unit, () => Option[T]) = {
val index = nextRegistrationIndex
val noPreviousValueIndex = -1
// register the index in the stack heads with an empty value
registeredStackHeads(index) = noPreviousValueIndex
nextRegistrationIndex += 1
(push[T](index), pop[T](index) )
}
/**
* Returns the push function for a stack at the given index
* @param stackIndex the index of the requesting stack
* @return the push function for the stack */
def push[T](stackIndex: Int) = {
(value: T) => {
// get the indices and initialize a stack node
val headIndex = registeredStackHeads(stackIndex)
val index = nextAvailableIndex()
val stackNode = new StackNode[T](headIndex, value)
// store the new stack head and place it in to the array
registeredStackHeads(stackIndex) = index
array(index) = Some(stackNode)
}
}
/**
* The pop function for the stack at a given index
* @param stackIndex the index of the requesting stack
* @return the pop function for the stack */
def pop[T](stackIndex: Int): () => Option[T] = {
() => {
val headIndex = registeredStackHeads(stackIndex)
if(headIndex < 0) None else {
array(headIndex) match {
case Some(s : StackNode[T]) => {
registeredStackHeads(stackIndex) = s.previousIndex
freeIndex(headIndex)
Some(s.value)
}
case _ => None
}
}
}
}
// retrieve the next available index, throws an exception if there are no available indices
private def nextAvailableIndex():Int = {
// return a new index if the array isn't full otherwise throw an exception
def newIndex() : Int = {
if(nextFreeIndex >= stackSize){
throw new Exception("No more room in stack array available")
}
else{
val index = nextFreeIndex
nextFreeIndex += 1
index
}
}
// first check if any of the old indices have been freed
freedIndices match {
case Nil => newIndex()
case head::tail => freedIndices = tail; head
}
}
// frees an index and adds it to the free indices list
private def freeIndex(i: Int): Unit = {
if (i >= array.length) throw new ArrayIndexOutOfBoundsException("The index to be freed does not exist in the array")
array(i) = None
freedIndices = i::freedIndices
}
// covariant node that helps keep track of the previous index of the stack
private class StackNode[+S](val previousIndex: Int, val value: S)
}
}
如果我调用 stack1.push 或 stack1.pop,代码会像它假设的那样工作,但它会导致包装器 push1 和 pop1 上的类型不匹配,为什么会这样?
example for push1:
type mismatch;
found : value.type (with underlying type A)
required: A
def push1[A](value :A): Unit = stack1.push(value)
example for pop1:
type mismatch;
found : Option[A(in class ThreeStacks)]
required: Option[A(in method pop1)]
def pop1[A](): Option[A] = stack1.pop()
最佳答案
您的代码中的问题是类型参数隐藏。
你有:
class ThreeStacks[A, B, C](val stackSize:Int = 1000) {
def push1[A](value: A): Unit = stack1.push(value)
...
}
应该是:
class ThreeStacks[A, B, C](val stackSize:Int = 1000) {
def push1(value: A): Unit = stack1.push(value)
...
}
按照您完成的方式,键入 ThreeStacks
类的参数 A
和方法 push1
的类型参数 A
> 是完全不同的,因此彼此不兼容。这同样适用于 B
和 C
。
关于Scala 类型不匹配底层类型和类与方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18433515/
我有以下功能: function addChange(result, bill) { for (var i=0;i
这是网站: www.wearethefirehouse.com/phasetest 如果您慢慢滚动,您会注意到一旦菜单栏完全不透明,nav li 元素就会全部从 Enzo 300 跳起来(如在没有导航
美好的一天。对于当前的项目,我需要知道数据类型如何表示为字节。例如,如果我使用: long three = 500;var bytes = BitConverter.GetBytes(three);
请解释 JVM 是如何在底层收集 ThreadDump 的。 我不明白它如何收集脱离 CPU 的线程的堆栈跟踪(等待磁盘 IO、网络、非自愿上下文切换)。 例如,linux perf 仅收集有关 on
开始学习 R,如果能帮助我理解 R 如何决定不同向量的类别,我将不胜感激。我初始化 vec <- c(1:6)当我执行 class(vec)我得到“整数”。为什么它不是“数字”,因为我认为 R 中的整
我有一个透明的 UIView,几乎覆盖了整个屏幕。我在顶部留下了 50 像素。它是 View Controller View 的 subview 。 在UIView下面有一个继承自UIView的MyV
我很好奇对象是如何在 Nodejs 中显示的,在本例中是 Promise。使用 console.log(promiseObject) 时,输出的类型为 {状态:待处理} 这对我来说似乎很奇怪,因为在该
当您在 Windows Azure 中使用表服务 API 时,幕后到底在做什么?我想我在某处读到这没有使用 SQL Server。它是否执行哈希表,然后过滤器真的像映射/减少操作一样运行?我对这些东西
如何查看函数 concat 中的代码?它是如何做的?有没有人有代码的副本或在浏览器控制台中查看它的方法? console.dir 不给我访问权限 console.dir(Array.prototype
我是 C++ 的新手,所以如果这个问题的答案显而易见,我深表歉意。 我一直在编写 STL 样式的自定义数据结构,以此来提高我的技能。 (我实际上也确实需要这种结构,但出于学习目的,我有点过分了。) 此
我正在尝试使用 log4j appender 将日志发送到 GrayLog2 (log4j2-gelf)。所以我将我的依赖项添加到我的 pom.xml 配置 log4j2.xml 来配置我的 appe
我正在使用带有 vector 的 priority_queue 作为底层容器。但是我希望堆的大小非常大。我知道动态 vector 容量调整大小的问题。所以我正在寻找方法来为我的priority_que
我有一个 SqlDataAdapter,它填充了 21 行数据(4 列)。驱动它的 sproc 在几秒钟内在 SQL Mgmt Studio 中返回,但 .Fill() 需要 5 分钟。 Ar
我想实现一个屏幕控制按钮,按下它可以作为 GUI 交互的修饰符。 这对于 MouseArea 是不可能的,因为该 API 只能处理一个鼠标区域中的一个触摸点。 该限制不适用于 MultiPointTo
我试图将图像和 div 层置于包含 div 的中心,但到目前为止我无法让它从列的左侧移动。我尝试了几种不同的方法,但就是无法让它移动。即使 margin auto 技巧也不起作用,我怀疑这是因为 bo
需要明确的是,我不是在询问 HDFS 中的权限设置,而是在 ext3 中或在 HDFS 运行于其上的各个数据节点机器上使用的任何文件系统中。 p> 我知道我们设置了 sudo chown hduser
我在服务器上创建了一个枚举,其中手动设置了整数值,而不是默认从 0 开始递增 public enum UserType { Anonymous = 0, Customer = 10,
如果显示框架图像,我们能否使以下 Google map 具有交互性。 Vie
我有一个顶部有自定义状态栏的布局 [在 Apple 的状态栏下方],然后是 UIScrollview 在中间部分从左到右分页,然后我有一个 UIView 底部有一些自定义按钮。一个简单的三 Pane
事情是这样的。我有一个 MVC 操作,在该操作上,我应用了自定义 ActionFilterAttribute 来使反序列化工作。现在,我想要做的是根据在此 View 中设置的 ViewBag.Titl
我是一名优秀的程序员,十分优秀!