penguin_queue 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +16 -12
- data/ext/penguin_queue/penguin_queue.c +158 -92
- data/lib/penguin_queue/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a4fd3cc82db722d18ca04d5041814b763bb57b2c
|
4
|
+
data.tar.gz: ef46be32133f2656159e46f7adf8ddf7960386ee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f23b0a538c39f318e66a28e37d5182d619182070ad71db09599f6d0dc286b1426c6d10a631708e93803dd5ee517ba65c3d163d4734496e5c86c85754965df064
|
7
|
+
data.tar.gz: 7e75329a8025a5d5090b40d053b23d884831d35d68b29f297a88e2fd6fa2d08c67474670a3aabe6dbefdd79c03124adf2eb7cefb90106775621e7527c57cb8d0
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -5,9 +5,13 @@
|
|
5
5
|
```
|
6
6
|
|
7
7
|
# Install
|
8
|
+
```
|
9
|
+
gem install penguin_queue
|
10
|
+
```
|
11
|
+
|
8
12
|
```ruby
|
9
13
|
# Gemfile
|
10
|
-
gem 'penguin_queue'
|
14
|
+
gem 'penguin_queue'
|
11
15
|
```
|
12
16
|
|
13
17
|
# Usage
|
@@ -32,25 +36,25 @@ pq.size.times.map { pq.deq } #=> [8, 6, 4, 2, 0]
|
|
32
36
|
|
33
37
|
# Methods
|
34
38
|
```ruby
|
35
|
-
#
|
36
|
-
PenguinQueue.new
|
37
|
-
#
|
38
|
-
|
39
|
+
# initialize
|
40
|
+
PenguinQueue.new
|
41
|
+
PenguinQueue.new(order: :max) # min(default) or max
|
42
|
+
PenguinQueue.new(&calc_priority_from_element_proc)
|
39
43
|
# enqueue
|
40
|
-
<<(e) enq(e)
|
44
|
+
<<(e) enq(e) push(e) unshift(e)
|
41
45
|
# enqueue with custom priority
|
42
|
-
enq(e, priority: p)
|
46
|
+
enq(e, priority: p) push(e, priority: p) unshift(e, priority: e)
|
43
47
|
# dequeue
|
44
|
-
deq shift pop
|
48
|
+
deq shift pop poll deq_with_priority
|
45
49
|
# dequeue multiple
|
46
|
-
deq(n) shift(n) pop(n)
|
50
|
+
deq(n) shift(n) pop(n) poll(n)
|
47
51
|
# fetch
|
48
|
-
first first_with_priority first_node
|
52
|
+
first peek top first_with_priority first_node
|
49
53
|
# remove
|
50
54
|
remove(node) delete(node)
|
51
55
|
# other
|
52
|
-
to_s inspect size empty?
|
56
|
+
to_s inspect clear size empty? min? max?
|
53
57
|
|
54
58
|
# PenguinQueue::Node
|
55
|
-
remove delete value value= priority priority=
|
59
|
+
remove delete value value= priority priority= to_s inspect
|
56
60
|
```
|
@@ -1,25 +1,25 @@
|
|
1
1
|
#include <ruby.h>
|
2
2
|
|
3
|
-
static ID id_priority, id_cmp, id_call;
|
3
|
+
static ID id_priority, id_cmp, id_call, id_order, id_max, id_min;
|
4
4
|
#define RB_STR_BUF_CAT(rstr, cstr) rb_str_buf_cat((rstr), (cstr), sizeof(cstr)-1);
|
5
5
|
struct node {
|
6
6
|
long index, id;
|
7
|
-
VALUE
|
7
|
+
VALUE queue, priority, value;
|
8
8
|
};
|
9
9
|
VALUE node_class;
|
10
10
|
void node_mark(struct node *ptr){
|
11
|
-
rb_gc_mark(ptr->
|
11
|
+
rb_gc_mark(ptr->queue);
|
12
12
|
rb_gc_mark(ptr->priority);
|
13
13
|
rb_gc_mark(ptr->value);
|
14
14
|
}
|
15
|
-
VALUE node_alloc_internal(long index, long id, VALUE
|
15
|
+
VALUE node_alloc_internal(long index, long id, VALUE queue, VALUE priority, VALUE value){
|
16
16
|
struct node *ptr = ALLOC(struct node);
|
17
17
|
ptr->index = index;
|
18
18
|
ptr->id = id;
|
19
|
-
ptr->
|
19
|
+
ptr->queue = queue;
|
20
20
|
ptr->priority = priority;
|
21
21
|
ptr->value = value;
|
22
|
-
return Data_Wrap_Struct(node_class, node_mark,
|
22
|
+
return Data_Wrap_Struct(node_class, node_mark, RUBY_DEFAULT_FREE, ptr);
|
23
23
|
}
|
24
24
|
#define NODE_PREPARE(self, name) struct node *name;Data_Get_Struct(self, struct node, name);
|
25
25
|
VALUE node_pri(VALUE self){
|
@@ -30,10 +30,6 @@ VALUE node_val(VALUE self){
|
|
30
30
|
NODE_PREPARE(self, ptr);
|
31
31
|
return ptr->value;
|
32
32
|
}
|
33
|
-
VALUE node_val_set(VALUE self, VALUE val){
|
34
|
-
NODE_PREPARE(self, ptr);
|
35
|
-
return ptr->value = val;
|
36
|
-
}
|
37
33
|
VALUE node_inspect(VALUE self){
|
38
34
|
NODE_PREPARE(self, nptr);
|
39
35
|
VALUE str = rb_str_buf_new(0);
|
@@ -48,6 +44,7 @@ VALUE node_inspect(VALUE self){
|
|
48
44
|
|
49
45
|
struct queue_data{
|
50
46
|
long counter;
|
47
|
+
int compare_sgn;
|
51
48
|
VALUE heap, compare_by;
|
52
49
|
};
|
53
50
|
|
@@ -60,17 +57,25 @@ long compare(VALUE a, VALUE b){
|
|
60
57
|
}
|
61
58
|
if(RB_TYPE_P(a, T_STRING)&&RB_TYPE_P(b, T_STRING))
|
62
59
|
return rb_str_cmp(a, b);
|
63
|
-
|
60
|
+
VALUE cmp = rb_funcall(a, id_cmp, 1, b);
|
61
|
+
if(NIL_P(cmp))rb_cmperr(a,b);
|
62
|
+
return rb_fix2long(cmp);
|
64
63
|
}
|
64
|
+
|
65
65
|
long compare_id(long a, long b){return a>b?1:a<b?-1:0;}
|
66
|
-
|
67
|
-
|
68
|
-
|
66
|
+
|
67
|
+
#define QUEUE_PREPARE(self, name) struct queue_data *name;Data_Get_Struct(self, struct queue_data, name);
|
68
|
+
#define OPTHASH_GIVEN_P(opts) (argc > 0 && !NIL_P((opts) = rb_check_hash_type(argv[argc-1])) && (--argc, 1))
|
69
|
+
|
70
|
+
void queue_mark(struct queue_data *self){
|
71
|
+
rb_gc_mark(self->heap);
|
72
|
+
rb_gc_mark(self->compare_by);
|
69
73
|
}
|
70
|
-
|
71
|
-
VALUE
|
74
|
+
|
75
|
+
VALUE queue_alloc(VALUE klass){
|
72
76
|
struct queue_data *ptr=ALLOC(struct queue_data);
|
73
77
|
ptr->counter = 0;
|
78
|
+
ptr->compare_sgn = 1;
|
74
79
|
ptr->heap = rb_ary_new_capa(1);
|
75
80
|
rb_ary_push(ptr->heap, Qnil);
|
76
81
|
if(rb_block_given_p()){
|
@@ -78,13 +83,35 @@ VALUE heap_alloc(VALUE klass){
|
|
78
83
|
}else{
|
79
84
|
ptr->compare_by = Qnil;
|
80
85
|
}
|
81
|
-
return Data_Wrap_Struct(klass,
|
86
|
+
return Data_Wrap_Struct(klass, queue_mark, RUBY_DEFAULT_FREE, ptr);
|
82
87
|
}
|
83
88
|
|
84
|
-
|
89
|
+
VALUE queue_initialize(int argc, VALUE *argv, VALUE self){
|
90
|
+
QUEUE_PREPARE(self, ptr);
|
91
|
+
VALUE opts, order;
|
92
|
+
if(!OPTHASH_GIVEN_P(opts))return self;
|
93
|
+
ID keyword_ids[] = {id_order};
|
94
|
+
rb_get_kwargs(opts, keyword_ids, 0, 1, &order);
|
95
|
+
if(order == Qundef)return self;
|
96
|
+
if(order == ID2SYM(id_max)){
|
97
|
+
ptr->compare_sgn = -1;
|
98
|
+
}else if(order != ID2SYM(id_min)){
|
99
|
+
rb_raise(rb_eArgError, "order should be :min or :max");
|
100
|
+
}
|
101
|
+
return self;
|
102
|
+
}
|
85
103
|
|
86
|
-
|
104
|
+
VALUE queue_clear(VALUE self){
|
87
105
|
QUEUE_PREPARE(self, ptr);
|
106
|
+
ptr->counter = 0;
|
107
|
+
rb_ary_clear(ptr->heap);
|
108
|
+
rb_ary_push(ptr->heap, Qnil);
|
109
|
+
return self;
|
110
|
+
}
|
111
|
+
|
112
|
+
void queue_up(VALUE self, VALUE node){
|
113
|
+
QUEUE_PREPARE(self, ptr);
|
114
|
+
int sgn = ptr->compare_sgn;
|
88
115
|
RARRAY_PTR_USE(ptr->heap, heap, {
|
89
116
|
NODE_PREPARE(node, nptr);
|
90
117
|
long index = nptr->index;
|
@@ -92,8 +119,8 @@ void heap_up(VALUE self, VALUE node){
|
|
92
119
|
long pindex = index/2;
|
93
120
|
VALUE pnode = heap[pindex];
|
94
121
|
NODE_PREPARE(pnode, pptr);
|
95
|
-
long cmp = compare(pptr->priority, nptr->priority);
|
96
|
-
if(!cmp)cmp=compare_id(pptr->id, nptr->id);
|
122
|
+
long cmp = compare(pptr->priority, nptr->priority)*sgn;
|
123
|
+
if(!cmp)cmp=compare_id(pptr->id, nptr->id)*sgn;
|
97
124
|
if(cmp<0)break;
|
98
125
|
pptr->index = index;
|
99
126
|
heap[index] = pnode;
|
@@ -104,8 +131,9 @@ void heap_up(VALUE self, VALUE node){
|
|
104
131
|
});
|
105
132
|
}
|
106
133
|
|
107
|
-
void
|
134
|
+
void queue_down(VALUE self, VALUE node){
|
108
135
|
QUEUE_PREPARE(self, ptr);
|
136
|
+
int sgn = ptr->compare_sgn;
|
109
137
|
long length = RARRAY_LEN(ptr->heap);
|
110
138
|
RARRAY_PTR_USE(ptr->heap, heap, {
|
111
139
|
NODE_PREPARE(node, nptr);
|
@@ -117,16 +145,16 @@ void heap_down(VALUE self, VALUE node){
|
|
117
145
|
if(lindex+1 < length){
|
118
146
|
VALUE rnode = heap[lindex+1];
|
119
147
|
NODE_PREPARE(rnode, rptr);
|
120
|
-
long cmp = compare(lptr->priority, rptr->priority);
|
121
|
-
if(!cmp)cmp=compare_id(lptr->id, rptr->id);
|
148
|
+
long cmp = compare(lptr->priority, rptr->priority)*sgn;
|
149
|
+
if(!cmp)cmp=compare_id(lptr->id, rptr->id)*sgn;
|
122
150
|
if(cmp >= 0){
|
123
151
|
lindex += 1;
|
124
152
|
lnode = rnode;
|
125
153
|
lptr = rptr;
|
126
154
|
}
|
127
155
|
}
|
128
|
-
long cmp = compare(nptr->priority, lptr->priority);
|
129
|
-
if(!cmp)cmp=compare_id(nptr->id, lptr->id);
|
156
|
+
long cmp = compare(nptr->priority, lptr->priority)*sgn;
|
157
|
+
if(!cmp)cmp=compare_id(nptr->id, lptr->id)*sgn;
|
130
158
|
if(cmp <= 0)break;
|
131
159
|
lptr->index = index;
|
132
160
|
heap[index] = lnode;
|
@@ -137,10 +165,11 @@ void heap_down(VALUE self, VALUE node){
|
|
137
165
|
});
|
138
166
|
}
|
139
167
|
|
140
|
-
VALUE
|
168
|
+
VALUE queue_remove_node(VALUE self, VALUE node){
|
141
169
|
if(!rb_obj_is_kind_of(node, node_class))return Qnil;
|
142
170
|
QUEUE_PREPARE(self, ptr);
|
143
171
|
NODE_PREPARE(node, nptr);
|
172
|
+
int sgn = ptr->compare_sgn;
|
144
173
|
long length = RARRAY_LEN(ptr->heap);
|
145
174
|
if(nptr->index >= length || RARRAY_AREF(ptr->heap, nptr->index) != node)return Qnil;
|
146
175
|
RARRAY_PTR_USE(ptr->heap, heap, {
|
@@ -149,12 +178,12 @@ VALUE heap_remove_node(VALUE self, VALUE node){
|
|
149
178
|
NODE_PREPARE(replace_node, rptr);
|
150
179
|
heap[nptr->index] = replace_node;
|
151
180
|
rptr->index = nptr->index;
|
152
|
-
long cmp = compare(rptr->priority, nptr->priority);
|
153
|
-
if(!cmp)cmp = compare_id(rptr->id, nptr->id);
|
181
|
+
long cmp = compare(rptr->priority, nptr->priority)*sgn;
|
182
|
+
if(!cmp)cmp = compare_id(rptr->id, nptr->id)*sgn;
|
154
183
|
if(cmp > 0){
|
155
|
-
|
184
|
+
queue_down(nptr->queue, replace_node);
|
156
185
|
}else{
|
157
|
-
|
186
|
+
queue_up(nptr->queue, replace_node);
|
158
187
|
}
|
159
188
|
});
|
160
189
|
return Qnil;
|
@@ -162,29 +191,48 @@ VALUE heap_remove_node(VALUE self, VALUE node){
|
|
162
191
|
|
163
192
|
VALUE node_remove(VALUE self){
|
164
193
|
NODE_PREPARE(self, nptr);
|
165
|
-
|
194
|
+
queue_remove_node(nptr->queue, self);
|
166
195
|
return Qnil;
|
167
196
|
}
|
168
197
|
|
169
|
-
|
198
|
+
void node_update_priority_internal(VALUE node, VALUE priority){
|
170
199
|
NODE_PREPARE(node, nptr);
|
171
|
-
QUEUE_PREPARE(nptr->
|
200
|
+
QUEUE_PREPARE(nptr->queue, ptr);
|
201
|
+
int sgn = ptr->compare_sgn;
|
172
202
|
VALUE priority_was = nptr->priority;
|
173
203
|
nptr->priority = priority;
|
174
|
-
long cmp = compare(priority, priority_was);
|
175
|
-
if(cmp == 0)return
|
204
|
+
long cmp = compare(priority, priority_was)*sgn;
|
205
|
+
if(cmp == 0)return;
|
176
206
|
RARRAY_PTR_USE(ptr->heap, heap, {
|
177
|
-
if(heap[nptr->index] != node)return
|
207
|
+
if(heap[nptr->index] != node)return;
|
178
208
|
});
|
179
209
|
if(cmp < 0){
|
180
|
-
|
210
|
+
queue_up(nptr->queue, node);
|
181
211
|
}else{
|
182
|
-
|
212
|
+
queue_down(nptr->queue, node);
|
183
213
|
}
|
184
|
-
return Qnil;
|
185
214
|
}
|
186
215
|
|
187
|
-
VALUE
|
216
|
+
VALUE node_update_priority(VALUE node, VALUE priority){
|
217
|
+
NODE_PREPARE(node, nptr);
|
218
|
+
QUEUE_PREPARE(nptr->queue, ptr);
|
219
|
+
if(ptr->compare_by != Qnil)rb_raise(rb_eRuntimeError, "priority update not supported on queue initialized with block");
|
220
|
+
node_update_priority_internal(node, priority);
|
221
|
+
return priority;
|
222
|
+
}
|
223
|
+
|
224
|
+
VALUE node_update_value(VALUE node, VALUE value){
|
225
|
+
NODE_PREPARE(node, nptr);
|
226
|
+
QUEUE_PREPARE(nptr->queue, ptr);
|
227
|
+
nptr->value = value;
|
228
|
+
if(ptr->compare_by != Qnil){
|
229
|
+
VALUE priority = rb_funcall(ptr->compare_by, id_call, 1, value);
|
230
|
+
node_update_priority_internal(node, priority);
|
231
|
+
}
|
232
|
+
return value;
|
233
|
+
}
|
234
|
+
|
235
|
+
VALUE queue_enq_vp(VALUE self, VALUE value, VALUE priority){
|
188
236
|
QUEUE_PREPARE(self, ptr);
|
189
237
|
if(ptr->compare_by != Qnil){
|
190
238
|
priority = rb_funcall(ptr->compare_by, id_call, 1, value);
|
@@ -193,14 +241,11 @@ VALUE heap_enq_vp(VALUE self, VALUE value, VALUE priority){
|
|
193
241
|
VALUE node = node_alloc_internal(length, ptr->counter, self, priority, value);
|
194
242
|
ptr->counter++;
|
195
243
|
rb_ary_push(ptr->heap, node);
|
196
|
-
|
244
|
+
queue_up(self, node);
|
197
245
|
return node;
|
198
246
|
}
|
199
247
|
|
200
|
-
|
201
|
-
(argc > 0 && !NIL_P((opts) = rb_check_hash_type(argv[argc-1])) && (--argc, 1))
|
202
|
-
|
203
|
-
VALUE heap_enq(int argc, VALUE *argv, VALUE self){
|
248
|
+
VALUE queue_enq(int argc, VALUE *argv, VALUE self){
|
204
249
|
VALUE value, opts, priority, pri = Qundef;
|
205
250
|
if (OPTHASH_GIVEN_P(opts)) {
|
206
251
|
ID keyword_ids[] = {id_priority};
|
@@ -208,18 +253,13 @@ VALUE heap_enq(int argc, VALUE *argv, VALUE self){
|
|
208
253
|
}
|
209
254
|
rb_scan_args(argc, argv, "1", &value);
|
210
255
|
priority = (pri == Qundef) ? value : pri;
|
211
|
-
return
|
212
|
-
}
|
213
|
-
VALUE heap_push(VALUE self, VALUE value){
|
214
|
-
return heap_enq_vp(self, value, value);
|
256
|
+
return queue_enq_vp(self, value, priority);
|
215
257
|
}
|
216
|
-
VALUE
|
217
|
-
|
218
|
-
for(int i=0;i<argc;i++)rb_ary_push(nodes, heap_push(self, argv[i]));
|
219
|
-
return nodes;
|
258
|
+
VALUE queue_push(VALUE self, VALUE value){
|
259
|
+
return queue_enq_vp(self, value, value);
|
220
260
|
}
|
221
261
|
|
222
|
-
VALUE
|
262
|
+
VALUE queue_first_node(VALUE self){
|
223
263
|
QUEUE_PREPARE(self, ptr);
|
224
264
|
long length = RARRAY_LEN(ptr->heap);
|
225
265
|
if(length == 1)return Qnil;
|
@@ -227,20 +267,20 @@ VALUE heap_first_node(VALUE self){
|
|
227
267
|
return heap[1];
|
228
268
|
});
|
229
269
|
}
|
230
|
-
VALUE
|
231
|
-
VALUE node =
|
270
|
+
VALUE queue_first(VALUE self){
|
271
|
+
VALUE node = queue_first_node(self);
|
232
272
|
if(node == Qnil)return Qnil;
|
233
273
|
NODE_PREPARE(node, nptr);
|
234
274
|
return nptr->value;
|
235
275
|
}
|
236
|
-
VALUE
|
237
|
-
VALUE node =
|
276
|
+
VALUE queue_first_with_priority(VALUE self){
|
277
|
+
VALUE node = queue_first_node(self);
|
238
278
|
if(node == Qnil)return Qnil;
|
239
279
|
NODE_PREPARE(node, nptr);
|
240
280
|
return rb_ary_new_from_args(2, nptr->value, nptr->priority);
|
241
281
|
}
|
242
282
|
|
243
|
-
VALUE
|
283
|
+
VALUE queue_deq_node(VALUE self){
|
244
284
|
QUEUE_PREPARE(self, ptr);
|
245
285
|
long length = RARRAY_LEN(ptr->heap);
|
246
286
|
if(length == 1)return Qnil;
|
@@ -250,14 +290,14 @@ VALUE heap_deq_node(VALUE self){
|
|
250
290
|
NODE_PREPARE(node, nptr);
|
251
291
|
if(length > 1){
|
252
292
|
nptr->index = 1;
|
253
|
-
|
293
|
+
queue_down(self, node);
|
254
294
|
}
|
255
295
|
return first;
|
256
296
|
});
|
257
297
|
}
|
258
|
-
VALUE
|
298
|
+
VALUE queue_deq(int argc, VALUE *argv, VALUE self){
|
259
299
|
if(argc == 0){
|
260
|
-
VALUE node =
|
300
|
+
VALUE node = queue_deq_node(self);
|
261
301
|
if(node == Qnil)return Qnil;
|
262
302
|
NODE_PREPARE(node, nptr);
|
263
303
|
return nptr->value;
|
@@ -271,33 +311,49 @@ VALUE heap_deq(int argc, VALUE *argv, VALUE self){
|
|
271
311
|
if(n>length)n=length;
|
272
312
|
VALUE result = rb_ary_new_capa(n);
|
273
313
|
for(int i=0;i<n;i++){
|
274
|
-
VALUE node =
|
314
|
+
VALUE node = queue_deq_node(self);
|
275
315
|
NODE_PREPARE(node, nptr);
|
276
316
|
rb_ary_push(result, nptr->value);
|
277
317
|
}
|
278
318
|
return result;
|
279
319
|
}
|
280
320
|
}
|
281
|
-
VALUE
|
282
|
-
VALUE node =
|
321
|
+
VALUE queue_deq_with_priority(VALUE self){
|
322
|
+
VALUE node = queue_deq_node(self);
|
283
323
|
if(node == Qnil)return Qnil;
|
284
324
|
NODE_PREPARE(node, nptr);
|
285
325
|
return rb_ary_new_from_args(2, nptr->value, nptr->priority);
|
286
326
|
}
|
287
327
|
|
288
|
-
VALUE
|
328
|
+
VALUE queue_size(VALUE self){
|
289
329
|
QUEUE_PREPARE(self, ptr);
|
290
330
|
return LONG2FIX(RARRAY_LEN(ptr->heap)-1);
|
291
331
|
}
|
292
|
-
VALUE
|
332
|
+
VALUE queue_is_empty(VALUE self){
|
293
333
|
QUEUE_PREPARE(self, ptr);
|
294
334
|
return RARRAY_LEN(ptr->heap) == 1 ? Qtrue : Qfalse;
|
295
335
|
}
|
296
|
-
|
336
|
+
#define QUEUE_PTR_IS_MIN(ptr) ((ptr)->compare_sgn==1)
|
337
|
+
VALUE queue_is_min(VALUE self){
|
338
|
+
QUEUE_PREPARE(self, ptr);
|
339
|
+
return QUEUE_PTR_IS_MIN(ptr) ? Qtrue : Qfalse;
|
340
|
+
}
|
341
|
+
VALUE queue_is_max(VALUE self){
|
342
|
+
QUEUE_PREPARE(self, ptr);
|
343
|
+
return QUEUE_PTR_IS_MIN(ptr) ? Qfalse : Qtrue;
|
344
|
+
}
|
345
|
+
VALUE queue_inspect(VALUE self){
|
346
|
+
QUEUE_PREPARE(self, ptr);
|
297
347
|
VALUE str = rb_str_buf_new(0);
|
298
348
|
rb_str_buf_append(str, rb_class_name(CLASS_OF(self)));
|
299
|
-
RB_STR_BUF_CAT(str, "{
|
300
|
-
|
349
|
+
RB_STR_BUF_CAT(str, "{order: ");
|
350
|
+
if(QUEUE_PTR_IS_MIN(ptr)){
|
351
|
+
RB_STR_BUF_CAT(str, ":min");
|
352
|
+
}else{
|
353
|
+
RB_STR_BUF_CAT(str, ":max");
|
354
|
+
}
|
355
|
+
RB_STR_BUF_CAT(str, ", size: ");
|
356
|
+
rb_str_buf_append(str, rb_inspect(queue_size(self)));
|
301
357
|
RB_STR_BUF_CAT(str, "}");
|
302
358
|
return str;
|
303
359
|
}
|
@@ -306,33 +362,43 @@ void Init_penguin_queue(void){
|
|
306
362
|
id_priority = rb_intern("priority");
|
307
363
|
id_call = rb_intern("call");
|
308
364
|
id_cmp = rb_intern("<=>");
|
365
|
+
id_max = rb_intern("max");
|
366
|
+
id_min = rb_intern("min");
|
367
|
+
id_order = rb_intern("order");
|
309
368
|
|
310
|
-
VALUE
|
311
|
-
rb_define_alloc_func(
|
312
|
-
rb_define_method(
|
313
|
-
rb_define_method(
|
314
|
-
rb_define_method(
|
315
|
-
rb_define_method(
|
316
|
-
rb_define_method(
|
317
|
-
rb_define_method(
|
318
|
-
rb_define_method(
|
319
|
-
rb_define_method(
|
320
|
-
rb_define_method(
|
321
|
-
rb_define_method(
|
322
|
-
rb_define_method(
|
323
|
-
rb_define_method(
|
324
|
-
rb_define_method(
|
325
|
-
rb_define_method(
|
326
|
-
rb_define_method(
|
327
|
-
rb_define_method(
|
328
|
-
rb_define_method(
|
369
|
+
VALUE queue_class = rb_define_class("PenguinQueue", rb_cObject);
|
370
|
+
rb_define_alloc_func(queue_class, queue_alloc);
|
371
|
+
rb_define_method(queue_class, "initialize", queue_initialize, -1);
|
372
|
+
rb_define_method(queue_class, "size", queue_size, 0);
|
373
|
+
rb_define_method(queue_class, "empty?", queue_is_empty, 0);
|
374
|
+
rb_define_method(queue_class, "clear", queue_clear, 0);
|
375
|
+
rb_define_method(queue_class, "inspect", queue_inspect, 0);
|
376
|
+
rb_define_method(queue_class, "top", queue_first, 0);
|
377
|
+
rb_define_method(queue_class, "peek", queue_first, 0);
|
378
|
+
rb_define_method(queue_class, "first", queue_first, 0);
|
379
|
+
rb_define_method(queue_class, "first_node", queue_first_node, 0);
|
380
|
+
rb_define_method(queue_class, "first_with_priority", queue_first_with_priority, 0);
|
381
|
+
rb_define_method(queue_class, "to_s", queue_inspect, 0);
|
382
|
+
rb_define_method(queue_class, "push", queue_enq, -1);
|
383
|
+
rb_define_method(queue_class, "<<", queue_push, 1);
|
384
|
+
rb_define_method(queue_class, "enq", queue_enq, -1);
|
385
|
+
rb_define_method(queue_class, "unshift", queue_enq, -1);
|
386
|
+
rb_define_method(queue_class, "pop", queue_deq, -1);
|
387
|
+
rb_define_method(queue_class, "shift", queue_deq, -1);
|
388
|
+
rb_define_method(queue_class, "deq", queue_deq, -1);
|
389
|
+
rb_define_method(queue_class, "poll", queue_deq, -1);
|
390
|
+
rb_define_method(queue_class, "deq_with_priority", queue_deq_with_priority, 0);
|
391
|
+
rb_define_method(queue_class, "delete", queue_remove_node, 1);
|
392
|
+
rb_define_method(queue_class, "remove", queue_remove_node, 1);
|
393
|
+
rb_define_method(queue_class, "min?", queue_is_min, 0);
|
394
|
+
rb_define_method(queue_class, "max?", queue_is_max, 0);
|
329
395
|
|
330
|
-
node_class = rb_define_class_under(
|
396
|
+
node_class = rb_define_class_under(queue_class, "Node", rb_cObject);
|
331
397
|
rb_undef_alloc_func(node_class);
|
332
398
|
rb_define_method(node_class, "priority", node_pri, 0);
|
333
399
|
rb_define_method(node_class, "priority=", node_update_priority, 1);
|
334
400
|
rb_define_method(node_class, "value", node_val, 0);
|
335
|
-
rb_define_method(node_class, "value=",
|
401
|
+
rb_define_method(node_class, "value=", node_update_value, 1);
|
336
402
|
rb_define_method(node_class, "inspect", node_inspect, 0);
|
337
403
|
rb_define_method(node_class, "to_s", node_inspect, 0);
|
338
404
|
rb_define_method(node_class, "remove", node_remove, 0);
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: penguin_queue
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- tompng
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-02-
|
11
|
+
date: 2017-02-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|