unified-queues 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,95 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ require "unified-queues/single/driver"
5
+
6
+ ##
7
+ # Base +Unified Queues+ module.
8
+ #
9
+
10
+ module UnifiedQueues
11
+
12
+ ##
13
+ # Universal single queue interface.
14
+ #
15
+
16
+ class Single
17
+
18
+ ##
19
+ # Abstract single driver class.
20
+ # @abstract
21
+ #
22
+
23
+ class Driver
24
+
25
+ ##
26
+ # Implicit heap queue driver. Uses the +Depq+ class from +depq+ gem
27
+ # for queueing. Priority is supported.
28
+ #
29
+
30
+ class DepqDriver < Driver
31
+
32
+ ##
33
+ # Pushes the value into the queue. Priority is supported.
34
+ #
35
+ # @param [Object] value value for push
36
+ # @param [Object] key key for priority queues
37
+ #
38
+
39
+ def push(value, key = value)
40
+ @native.insert(value, key)
41
+ end
42
+
43
+ ##
44
+ # Pops value out of the queue. Note, value with minimal
45
+ # priority will be popped out. Blocking isn'ŧ supported.
46
+ #
47
+ # @param [Boolean|Integer] blocking +true+ or timeout if it should block, +false+ otherwise
48
+ # @return [Object] queued value
49
+ #
50
+
51
+ def pop(blocking = false)
52
+ @native.delete_min
53
+ end
54
+
55
+ ##
56
+ # Indicates queue is empty.
57
+ # @param [Boolean] +true+ if it's, +false+ otherwise
58
+ #
59
+
60
+ def empty?
61
+ @native.empty?
62
+ end
63
+
64
+ ##
65
+ # Clears the queue.
66
+ #
67
+
68
+ def clear!
69
+ @native.clear
70
+ end
71
+
72
+ ##
73
+ # Returns length of the queue.
74
+ # @return [Integer]
75
+ #
76
+
77
+ def length
78
+ @native.size
79
+ end
80
+
81
+ ##
82
+ # Returs type of the queue.
83
+ # @return [:linear]
84
+ #
85
+
86
+ def type
87
+ :linear
88
+ end
89
+
90
+ end
91
+ end
92
+
93
+ end
94
+
95
+ end
@@ -0,0 +1,96 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ require "unified-queues/single/driver"
5
+
6
+ ##
7
+ # Base +Unified Queues+ module.
8
+ #
9
+
10
+ module UnifiedQueues
11
+
12
+ ##
13
+ # Universal single queue interface.
14
+ #
15
+
16
+ class Single
17
+
18
+ ##
19
+ # Abstract single driver class.
20
+ # @abstract
21
+ #
22
+
23
+ class Driver
24
+
25
+ ##
26
+ # EventedQueue queue driver. Uses the +EventedQueue+ class
27
+ # from the +evented-queue+ gem. Priority is supported
28
+ # depending to arguments.
29
+ #
30
+
31
+ class EventedQueueDriver < Driver
32
+
33
+ ##
34
+ # Pushes the value into the queue. Priority is supported
35
+ # depending to arguments.
36
+ #
37
+ # @param [Object] value value for push
38
+ # @param [Object] key key for priority queues
39
+ #
40
+
41
+ def push(value, key = value, &block)
42
+ @native.push(value, key, &block)
43
+ end
44
+
45
+ ##
46
+ # Pops value out of the queue.
47
+ #
48
+ # @param [Boolean|Integer] blocking +true+ or timeout if it should block, +false+ otherwise
49
+ # @param [Object] queue value
50
+ #
51
+
52
+ def pop(blocking = false, &block)
53
+ @native.pop(&block)
54
+ end
55
+
56
+ ##
57
+ # Indicates queue is empty.
58
+ # @param [Boolean] +true+ if it's, +false+ otherwise
59
+ #
60
+
61
+ def empty?(&block)
62
+ @native.empty?(&block)
63
+ end
64
+
65
+ ##
66
+ # Clears the queue.
67
+ #
68
+
69
+ def clear!(&block)
70
+ @native.clear(&block)
71
+ end
72
+
73
+ ##
74
+ # Returns length of the queue.
75
+ # @return [Integer]
76
+ #
77
+
78
+ def length(&block)
79
+ @native.length(&block)
80
+ end
81
+
82
+ ##
83
+ # Returs type of the queue.
84
+ # @return [:linear]
85
+ #
86
+
87
+ def type
88
+ :linear
89
+ end
90
+
91
+ end
92
+ end
93
+
94
+ end
95
+
96
+ end
@@ -0,0 +1,221 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ require "unified-queues/single/driver"
5
+ require "abstract"
6
+
7
+ ##
8
+ # Base +Unified Queues+ module.
9
+ #
10
+
11
+ module UnifiedQueues
12
+
13
+ ##
14
+ # Universal single queue interface.
15
+ #
16
+
17
+ class Single
18
+
19
+ ##
20
+ # Abstract single driver class.
21
+ # @abstract
22
+ #
23
+
24
+ class Driver
25
+
26
+ ##
27
+ # Fibonacci heap queue driver. Uses the +CPriorityQueue+ class
28
+ # from +PriorityQueue+ gem. Priority is supported.
29
+ #
30
+ # It isn't implement equivalent of the +#clear+ method, so
31
+ # it falls backs to naive "popping" style clearing.
32
+ #
33
+
34
+ class CPriorityQueueDriver < Driver
35
+
36
+ ##
37
+ # Pushes the value into the queue. Priority is supported.
38
+ #
39
+ # @param [Object] value value for push
40
+ # @param [Object] key key for priority queues
41
+ #
42
+
43
+ def push(value, key = value)
44
+ key = (key.kind_of? Integer) ? key : 0
45
+ @native.push(value, key)
46
+ end
47
+
48
+ ##
49
+ # Pops value out of the queue. Note, value with minimal
50
+ # priority will be popped out. Blocking isn'ŧ supported.
51
+ #
52
+ # @param [Boolean|Integer] blocking +true+ or timeout if it should block, +false+ otherwise
53
+ # @return [Object] queued value
54
+ #
55
+
56
+ def pop(blocking = false)
57
+ @native.delete_min_return_key
58
+ end
59
+
60
+ ##
61
+ # Indicates queue is empty.
62
+ # @param [Boolean] +true+ if it's, +false+ otherwise
63
+ #
64
+
65
+ def empty?
66
+ @native.empty?
67
+ end
68
+
69
+ ##
70
+ # Returns length of the queue.
71
+ #
72
+ # @return [Integer]
73
+ #
74
+
75
+ def length
76
+ @native.length
77
+ end
78
+
79
+ ##
80
+ # Returs type of the queue.
81
+ # @return [:linear]
82
+ #
83
+
84
+ def type
85
+ :linear
86
+ end
87
+
88
+ end
89
+
90
+ ##
91
+ # Minimal priority queue driver. Uses the +PoorPriorityQueue+ class
92
+ # from +PriorityQueue+ gem. Priority is supported.
93
+ #
94
+ # It isn't implement equivalent of the +#clear+ method, so
95
+ # it falls backs to naive "popping" style clearing.
96
+ #
97
+ # Also note, it's very minimalistic implementation, so both calls
98
+ # to {PoorPriorityQueue#length} and {PoorPriorityQueue#empty?}
99
+ # will throw an exception.
100
+ #
101
+
102
+ class PoorPriorityQueueDriver < Driver
103
+
104
+ ##
105
+ # Pushes the value into the queue. Priority is supported.
106
+ #
107
+ # @param [Object] value value for push
108
+ # @param [Object] key key for priority queues
109
+ #
110
+
111
+ def push(value, key = value)
112
+ @native.push(value, key)
113
+ end
114
+
115
+ ##
116
+ # Pops value out of the queue. Note, value with minimal
117
+ # priority will be popped out. Blocking isn't supported.
118
+ #
119
+ # @param [Boolean|Integer] blocking +true+ or timeout if it should block, +false+ otherwise
120
+ # @return [Object] queued value
121
+ #
122
+
123
+ def pop(blocking = false)
124
+ @native.delete_min_return_key
125
+ end
126
+
127
+ ##
128
+ # Indicates queue is empty. Note, it isn't implemented
129
+ # in this implementation.
130
+ #
131
+ # @param [Boolean] +true+ if it's, +false+ otherwise
132
+ #
133
+
134
+ def empty?
135
+ not_implemented
136
+ end
137
+
138
+ ##
139
+ # Returns length of the queue. Note, it isn't implemented
140
+ # in this implementation.
141
+ #
142
+ # @return [Integer]
143
+ #
144
+
145
+ def length
146
+ not_implemented
147
+ end
148
+
149
+ end
150
+
151
+ ##
152
+ # Ruby fibonacci heap priority queue driver. Uses the +RubyPriorityQueue+ class
153
+ # from +PriorityQueue+ gem. Priority is supported.
154
+ #
155
+ # It isn't implement equivalent of the +#clear+ method, so
156
+ # it falls backs to naive "popping" style clearing.
157
+ #
158
+
159
+ class RubyPriorityQueueDriver < Driver
160
+
161
+ ##
162
+ # Pushes the value into the queue. Priority is supported.
163
+ #
164
+ # @param [Object] value value for push
165
+ # @param [Object] key key for priority queues
166
+ #
167
+
168
+ def push(value, key = value)
169
+ @native.push(value, key)
170
+ end
171
+
172
+ ##
173
+ # Pops value out of the queue. Note, value with minimal
174
+ # priority will be popped out. Blocking isn't supported.
175
+ #
176
+ # @param [Boolean|Integer] blocking +true+ or timeout if it should block, +false+ otherwise
177
+ # @return [Object] queued value
178
+ #
179
+
180
+ def pop(blocking = false)
181
+ @native.delete_min_return_key
182
+ end
183
+
184
+ ##
185
+ # Indicates queue is empty. Note, it isn't implemented
186
+ # in this implementation.
187
+ #
188
+ # @param [Boolean] +true+ if it's, +false+ otherwise
189
+ #
190
+
191
+ def empty?
192
+ @native.empty?
193
+ end
194
+
195
+ ##
196
+ # Returns length of the queue. Note, it isn't implemented
197
+ # in this implementation.
198
+ #
199
+ # @return [Integer]
200
+ #
201
+
202
+ def length
203
+ @native.length
204
+ end
205
+
206
+ ##
207
+ # Returs type of the queue.
208
+ # @return [:linear]
209
+ #
210
+
211
+ def type
212
+ :linear
213
+ end
214
+
215
+ end
216
+
217
+ end
218
+
219
+ end
220
+
221
+ end
@@ -0,0 +1,105 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ require "unified-queues/single/driver"
5
+ require "hash-utils"
6
+
7
+ ##
8
+ # Base +Unified Queues+ module.
9
+ #
10
+
11
+ module UnifiedQueues
12
+
13
+ ##
14
+ # Universal single queue interface.
15
+ #
16
+
17
+ class Single
18
+
19
+ ##
20
+ # Abstract single driver class.
21
+ # @abstract
22
+ #
23
+
24
+ class Driver
25
+
26
+ ##
27
+ # Queue queue driver. Uses standard library +Queue+ class
28
+ # for thread synchronized queueing. Priority isn't supported.
29
+ #
30
+
31
+ class QueueDriver < Driver
32
+
33
+ ##
34
+ # Pushes the value into the queue. Priority isn't supported.
35
+ #
36
+ # @param [Object] value value for push
37
+ # @param [Object] key key for priority queues
38
+ #
39
+
40
+ def push(value, key = value)
41
+ @native.push(value)
42
+ end
43
+
44
+ ##
45
+ # Pops value out of the queue.
46
+ #
47
+ # @param [Boolean|Integer] blocking +true+ or timeout if it should block, +false+ otherwise
48
+ # @param [Object] queue value
49
+ #
50
+
51
+ def pop(blocking = false)
52
+ if blocking.boolean?
53
+ timeout = !blocking
54
+ else
55
+ timeout = blocking
56
+ end
57
+
58
+ begin
59
+ @native.pop(blocking)
60
+ rescue ThreadError
61
+ nil
62
+ end
63
+ end
64
+
65
+ ##
66
+ # Indicates queue is empty.
67
+ # @param [Boolean] +true+ if it's, +false+ otherwise
68
+ #
69
+
70
+ def empty?
71
+ @native.empty?
72
+ end
73
+
74
+ ##
75
+ # Clears the queue.
76
+ #
77
+
78
+ def clear!
79
+ @native.clear
80
+ end
81
+
82
+ ##
83
+ # Returns length of the queue.
84
+ # @return [Integer]
85
+ #
86
+
87
+ def length
88
+ @native.length
89
+ end
90
+
91
+ ##
92
+ # Returs type of the queue.
93
+ # @return [:linear]
94
+ #
95
+
96
+ def type
97
+ :linear
98
+ end
99
+
100
+ end
101
+ end
102
+
103
+ end
104
+
105
+ end