本来是对 CollisionCheckStack 数据结构的探究。
- Push/pop operation.
- Duplicate check. When an object that's already in the stack is pushed, this class will tell you so.
push/pop operation
使用一个数组,在数组尾端(tail)进行操作即可
Duplicate check
寻找一个元素,有如下几种策略
- loop all the element to find
- sort the elements, using binaray search
- B-tree
- build index, use the index to find
- use hashmap
CollisionCheckStack 使用hashmap来快速定位。我们知道hashmap最简单的实现方式是chain , 而chain最简单的实现方式是Linked list
Kohsuke Kawaguchi 采用的是array based linked list或者wiki中所说的hash linking
The link fields need not be physically part of the nodes. If the data records are stored in an array and referenced by their indices, the link field may be stored in a separate array with the same indices as the data records.
wiki 中在相关数据结构中提到
A hash table may use linked lists to store the chains of items that hash to the same position in the hash table.
A heap shares some of the ordering properties of a linked list, but is almost always implemented using an array. Instead of references from node to node, the next and previous data indexes are calculated using the current data's index.
使用数组来实现linked list, (A linked list could be implemented in an array )
上面的Linked list采用三元组表示为 Data (index) (next node index)
David (0) (4) -> Ioshua (4) (7)-> Leah (7) (2) -> Miriam (2) (6) -> Robert (6) (-1)
其要点
- 数组中的element有个index属性,得到index就得到element
- next[i] 即当前节点(第i个节点)的下一个元素在数组中的index
把array based linked list 运用到hash map中就是
一共使用了三个数组
- Object[] data, 如前,用来实现stack
- int[] initialHash, initialHash[i]用来存放hash值为i的头节点在data数组中的位置
- int[] next, next[i]用来存放与data[i]有相同hash值的下一元素在data数组中的位置
Suppose Find(data) return the index of data in the data array
Find(d6)的过程如下
hash(d6) = 1
initHash(1) = 6
data[6] == d6, return 6
Find(d4)的过程如下
hash(d4) = 1
initHash(1) = 6
data[6] != d4
next[6] = 4
data[4] == d4, return 4
Find(d3)的过程如下
hahs(d3) = 1
initHash(1) = 6
data[6] != d3
next[6] = 4
data[4] != d3
next[4] = 3
data[3] == d3, return 3
Find(e) 函数如下:
insert(e)函数如下: