actor binary tree lab4
forward 与 ! (tell) 的差异,举个例子:
Main(当前actor): topNode ! Insert(requester, id=1, ele = 2)
topNode: root ! Insert(requester, id=1, ele = 2)
对于root而言,Insert消息的来源是topNode
假如第二行替换为
topNode: root forward Insert(requester, id=1, ele = 2)
此时,对于root而言,Insert消息的来源是Main
从上面的例子可以看出,! 和 forward之间是有差别的,但是差别本身比较tricky,会增加理解的成本,所以传递消息时,把消息的actorRef作为参数传递会简单很多。
在lab4中,就需要考虑到!与forward的异同点
testcase 的 code
test("proper inserts and lookups") {
val topNode = system.actorOf(Props[BinaryTreeSet])
topNode ! Contains(testActor, id = , )
expectMsg(ContainsResult(, false))
topNode ! Insert(testActor, id = , )
topNode ! Contains(testActor, id = , )
expectMsg(OperationFinished())
expectMsg(ContainsResult(, true))
}
结合topNode,与root的处理逻辑
val normal: Receive = {
// 这个forward非常重要,不能写成 !,不然第一个testcase都不过去
case operation: Operation => root ! operation
case GC => {
//garbageCollecting需要先变,因为节点的复制可能会很久
val newRoot = createRoot
context.become(garbageCollecting(newRoot))
root ! CopyTo(newRoot)
}
case _ => println("unknown operation")
}
// root 的处理逻辑
case Insert(requester, id, elem) =>
if(this.elem == elem) {
if(this.removed) this.removed = false
requester ! OperationFinished(id)
} else {
val child = if(this.elem > elem) Left else Right
if(subtrees contains child) subtrees(child) ! Insert(requester, id, elem)
else {
subtrees += child -> context.actorOf(props(elem, false))
requester ! OperationFinished(id)
}
}
在source code中,topNode收到消息后,把消息传递给root,root认为消息的来源是topNode。假如消息不绑定requester参数,那么通过sender获得actor是tiopNode,而不是main。我们在testcase中做assertion的话,肯定就是错的。
另外,上例中用到了testActor,它是implicit变量,用于处理发送到main中的消息。主要是测试方便。
一个debug了5个小时的bug
case msg: Operation =>
pendingQueue = pendingQueue.enqueue(msg)
pendingQueue.enqueue(msg) 并不能更新pendingQueue自己,必须重新赋值才行。
actor binary tree lab4的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode, construct binary tree from inorder and post order traversal
Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
- [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化
One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
随机推荐
- 百度网盘 http://pandownload.com/index.html
https://github.com/high-speed-downloader/high-speed-downloader
- 在虚拟机里面安装Linux操作系统
在这篇文章中以VMware14为例,Linux操作系统采用CentOS7进行讲解如何在虚拟机中安装Linux操作系统. 一.选择创建新的虚拟机 二.在选择虚拟机配置界面选择“自定义(高级)”选项,然后 ...
- 微信SDK 报错 invalid url domanin
刚开始我在安全JS域名下填写: Http://hgj123.8.yydns.pw 带了Http 在微信中开打自己写好demo.报invalid url domanin 说我的无效URL. 然后我在 ...
- Qt添加驱动——Qt数据库之添加MySQL驱动插件
Qt数据库之添加MySQL驱动插件(1) 现在可用的数据库驱动只有3种,在Qt中,我们需要自己编译其他数据库驱动的代码,让它们以插件的形式来使用.下面我们就以现在比较流行的MySQL数据库为例,说明一 ...
- TCP滑动窗口协议
TCP的首部中有一个很重要的字段就是16位长的窗口大小,它出现在每一个TCP数据报中,配合32位的确认序号,用于向对端通告本地socket的接收窗口大小.也就是说,如果本地socket发送一个TCP ...
- 关于Unity中GrabPass截屏的使用和Shader的组织优化
GrabPass截屏 可以用来截屏,截屏后把纹理传给下一个通道使用. 1:使用抓屏通道, GrabPass {} 或 GrabPass { “ 纹理名称”}; 使用GrabPass {}后,可以用_G ...
- Linux 下查看某个进程运行的堆栈信息
1. 根据进程名称查询进程ID ps -ef | grep processName 2. 将进程的堆栈信息写入log gstack processId > s.log 3. 查看log vim ...
- JAVA组成原理及使用方法编辑环境及实现过程
JAVA组成原理一.由四方面组成:1.Java编程语言2.Java类文件格式3.Java虚拟机4.Java应用程序接口 当编辑并运行一个Java程序时,需要同时涉及到这四种方面.二.使用文字编辑软件: ...
- SecureCRT连接linux,vim颜色显示问题
为什么使用VIM?由于具有颜色显示的功能.而且还支持很多程序语法.VIM能够帮助你直接进行程序除错.可是在SecureCRT连接Linux后显示就是黑白,感觉非常不爽! ! 怎么办? vim编敲代码也 ...
- C#页面前台<%%><%#%><%=%>
ASP.net前台绑定用的最多,今天小小总结一下. 1:<%#Eval("")%> 2:<%#Bind("")%> 3:<%=变量 ...