forked from SeasideSt/Grease
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGRSmallOrderedSet.class.st
More file actions
141 lines (121 loc) · 3.09 KB
/
GRSmallOrderedSet.class.st
File metadata and controls
141 lines (121 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"
I am an implementation of a dictionary. Compared to other dictionaries I am very efficient for small sizes, speed- and space-wise. I also mantain the order in which elements are added when iterating.
"
Class {
#name : #GRSmallOrderedSet,
#superclass : #GRObject,
#instVars : [
'size',
'table'
],
#category : #'Grease-Core-Collections'
}
{ #category : #'instance creation' }
GRSmallOrderedSet class >> new [
^ self new: 3
]
{ #category : #'instance creation' }
GRSmallOrderedSet class >> new: anInteger [
^ self basicNew initialize: anInteger; yourself
]
{ #category : #'instance creation' }
GRSmallOrderedSet class >> withAll: aDictionary [
^ (self new: aDictionary size)
addAll: aDictionary;
yourself
]
{ #category : #adding }
GRSmallOrderedSet >> add: newObject [
(self findIndexFor: newObject) = 0
ifTrue: [ self privateAdd: newObject ].
^ newObject
]
{ #category : #adding }
GRSmallOrderedSet >> addAll: aCollection [
aCollection do: [ :each |
self add: each ].
^ aCollection
]
{ #category : #enumerating }
GRSmallOrderedSet >> do: aOneArgumentBlock [
1 to: size do: [ :i |
aOneArgumentBlock value: (table at: i) ]
]
{ #category : #enumerating }
GRSmallOrderedSet >> do: aOneArgumentBlock separatedBy: aNiladicBlock [
1 to: size do: [ :i |
i > 1 ifTrue: [ aNiladicBlock value ].
aOneArgumentBlock value: (table at: i) ]
]
{ #category : #'private ' }
GRSmallOrderedSet >> errorNotFound [
self error: 'Not found'
]
{ #category : #'private ' }
GRSmallOrderedSet >> findIndexFor: aKey [
1 to: size do: [ :index |
(table at: index) = aKey
ifTrue: [ ^ index ] ].
^ 0
]
{ #category : #'private ' }
GRSmallOrderedSet >> grow [
| newTable |
"#replaceFrom:to:with:startingAt: would be better but not portable"
newTable := Array new: 2 * size.
1 to: size do: [ :index |
newTable at: index put: (table at: index) ].
table := newTable
]
{ #category : #testing }
GRSmallOrderedSet >> includes: anObject [
^ (self findIndexFor: anObject) ~= 0
]
{ #category : #initialization }
GRSmallOrderedSet >> initialize: anInteger [
self initialize.
size := 0.
table := Array new: anInteger
]
{ #category : #testing }
GRSmallOrderedSet >> isCollection [
^ true
]
{ #category : #testing }
GRSmallOrderedSet >> isEmpty [
^ size = 0
]
{ #category : #copying }
GRSmallOrderedSet >> postCopy [
super postCopy.
table := table copy
]
{ #category : #'private ' }
GRSmallOrderedSet >> privateAdd: newObject [
size = table size ifTrue: [ self grow ].
table at: (size := size + 1) put: newObject.
]
{ #category : #removing }
GRSmallOrderedSet >> remove: anObject [
^ self remove: anObject ifAbsent: [ self errorNotFound ]
]
{ #category : #removing }
GRSmallOrderedSet >> remove: anObject ifAbsent: aNiladicBlock [
| index |
index := self findIndexFor: anObject.
index = 0
ifTrue: [ ^ aNiladicBlock value ]
ifFalse: [ self removeIndex: index ].
^ anObject
]
{ #category : #'private ' }
GRSmallOrderedSet >> removeIndex: index [
table at: index put: nil.
index to: size - 1 do: [ :i |
table at: i put: (table at: i + 1) ].
size := size - 1
]
{ #category : #accessing }
GRSmallOrderedSet >> size [
^ size
]