Added more protocol for Behavior. Fleshed out HashedCollection classes.
[panda.git] / st / HashedCollection.st
blob0aaace4d4dbabd99bae70bdfb7283c3806da21bc
3 HashedCollection classMethod!
4 new
5         ^ self new: self defaultCapacity!
7 HashedCollection classMethod!
8 new: initialCapacity
9         ^ self basicNew initialize: initialCapacity!
11 HashedCollection classMethod!
12 with: an
13         ^ self new: self defaultCapacity!
15 HashedCollection method!
16 at: index include: anObject
17         | index |
19         array at: index put: anObject. 
20         size := size + 1.
22         self growIfNeeded.
24         ^ anObject!
26 HashedCollection method!
27 removeAtIndex: index
28         array at: index put: array.
29         size := size - 1.
30         deleted := deleted + 1!
33 HashedCollection method!
34 find: anObject
35         ^ self find: anObject in: array!
37 HashedCollection method!
38 find: anObject in: anArray
39         self subclassResponsibility!
41 HashedCollection method!
42 grow
43         | newArray i |
45         newArray := Array new: (array size + array size).
46         
47         self do: [ :object |
48                 newArray at: (self find: object in: newArray) put: object].
50         array := newArray.
51         deleted := 0!
54 HashedCollection method!
55 growIfNeeded
56         "maximum 50% load factor"
57         (self occupiedCount * 2) > array size
58                 ifTrue: [ self grow ]!
61 HashedCollection method!
62 size
63         ^ size!
65 HashedCollection method!
66 capacity
67         ^ array size // 2!
70 "enumerating"
71 HashedCollection method!
72 do: aBlock
73         1 to: array size do:
74                 [ :i | | object |
75                         object := array at: i.
76                         (object ~~ nil) & (object ~~ array) ifTrue: [ aBlock value: object ]]!
79 "private"
81 HashedCollection classMethod!
82 minimumCapacity
83         ^ 4!
85 HashedCollection classMethod!
86 defaultCapacity
87         ^ 8!
89 HashedCollection method!
90 sizeForCapacity: capacity
91         | minsize |
93         minsize := HashedCollection minimumCapacity.
95         [ minsize < capacity ]
96                 whileTrue: [ minsize := minsize + minsize ].
98         ^ minsize!
100 HashedCollection method!
101 occupiedCount
102         ^ size + deleted!
104 HashedCollection method!
105 initialize: initialCapacity
106         array := Array new: (self sizeForCapacity: initialCapacity).
107         size := 0.
108         deleted := 0!
110 HashedCollection method!
111 loadFactor
112         ^ self occupiedCount / array size!