CSE116的学习笔记-Lec5-3:Actors
Actorsactors again!
Stock Trader Example
Simulate stock changing prices and a trader purchasing stocks
Stock
Receives a Tick message and changes its price
Price changes are random for this simulation
Tick messages are sent to all stocks at regular intervals
Receives a GetPrice message and responds with its current price
Trader
Knows each stock’s ticker symbol and actor reference
Receives a CheckStocks message and checks the price of all known stocks
Receives Price messages from stock ...
CSE116的学习笔记-Lec5-2:Testing_Actors
Testing Actorstesting the actors:)
Traits and Mixins
We’ve seen inheritance by extending abstract class
What if we want to extend multiple classes?
Example: You want an object class that extends PhysicalObject to have physics applied and extend Actor to run concurrently
This is not allowed
Can avoid this need by using composition
Make a class that extends actor and stores a PhysicalObject in a state variable
Traits
Similar to abstract classes
Cannot have a constructor
No limit to the n ...
CSE116的学习笔记-Lec5-1:Actors
Concurrency and Actorsnew chapter :)
Concurrency
Most programs we’ve written execute code sequentially
Each statement of code is executed in the order they are written
Can have control flow to decide which statements are executed and in which order
What if we want multiple pieces of code to execute at the same time?
We’ve written 2 types of concurrent software already
In CSE115, you wrote a web server
What if 2 users are visiting your site at the same time?
Server waits for requests and ha ...
CSE116的学习笔记-Lec4_6_Pathfinding
Pathfinding with BFSlittle hard :/, but just a little:)
Paths
Path: A sequence of nodes with each adjacent pair of nodes connected by an edge
The length of a path is the number of edges it contains (number of nodes - 1)
[LINCOLN, MIT, UTAH, SDC, RAND, UCSB, SRI] <– Path of length 6
Distance
Distance between two nodes: The lenght of the shortest path between the nodes
Distance between LINCOLN and SRI == 3 [See the video]
Distance between RAND and BBN == 1 [See the ...
CSE116的学习笔记-Lec4-5:Graphs
GraphsIt has cycle, so you know: tree + cycle :/
Data Structures: Review
Sequential Data Structures
Elements stored in a specific order
Ex: Array, List
Key-Value Store
Stores pairs of elements with no particular order
Each key is associated with one value
Ex: Map, Dictionary, Object
Tree
Non-linear structure
Each element can be associated with multiple other elements
Data Structures
How do we store datat with multiple interconnected associations?
A [station, intersection, city] ...
CSE116的学习笔记-Lec4-4:BST
Binary Search Tree (BST)Sorted Tree :/
BST - Definition
For each node:
All values in the left subtree are less than the node’s value
All values in the right subtree are greater than the node’s value
Duplicate values handled differently based on implementation
Sometims not allowed at all
BST - Code
To make the BST generic
Take a type poarameter
Take a comparator to decide the sorted order
Store a reference to the root node
1234567class BinarySearchTree[A](comparator: (A, A) => B ...
CSE116的学习笔记-Lec4-3:Trees
Binary Trees and TraversalsYou would also learn this on CSE191 @.@
Binary Trees
Similar in structure to Linked List
Consists of Nodes
A Tree is only a reference to the first node (Called the root node)
Trees have 2 references to nodes
Each node has left and right reference
Vocab: These are called its child nodes
Vocab: The node is the parent to these children
The Code123class BinaryTreeNode[A](var value: A, var left: BinaryTreeNode[A], var right: BinaryTreeNode[A]) {}
1234567 ...
CSE116的学习笔记-Lec4-2:Stack_Queue
Stack and Queue
Date structures with specific purposes
Restricted features
All operations are very efficient
Inefficient operations are not allowed
We’ll se a stack and queue using linked liss
*Scala has builtin Stack and Queue classes
Stack
LIFO
Last in First out
The last element pushed onto the stack is the first element to be popped off the stack
Only the element on the top of the stack can be accessed
Stack Methods
Push
Add an element to the top of the stack
Pop
Remove the top elem ...
CSE116的学习笔记-Lec4-1:Linked_List
Linked ListList “Pro” version:)
Recall - Array
Sequential
One continuous block of memory
Random access based on memory address
address = first_address + (element_size * index)
Fixed Size
Since memory adjacent to the block may be used
Efficient when you know many elements you’ll need to store
Array
Arrays are stored on the heap
Pointer to index 0 goes on the stack
add index * sizeOfElement to 1503 to find each element
This is called random access
Recall - Linked List
Sequential
S ...
CSE116的学习笔记-Lec3-5:Immutability
Immutability
Immutable Objects
Values stored in state variables cannot change
Immutable objects are stored on the heap just like any other object
But we don’t worry about the state changing when we pass the reference to a method / function
What if an immutable object needs to change state?
Create a copy of the object with the change applied
This ImmutableCounter class takes an initial value in its constructor and has methods to increment and decrement this value
The internal Int is ...