priority_queue_cxx 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -5
- data/ext/fast_containers/fc_pq.cpp +31 -31
- data/ext/fast_containers/fc_pq.h +19 -20
- data/lib/fc.rb +3 -3
- 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: e986c8e1ee2ffadf9ee65bbc906fe3fe980aff79
|
4
|
+
data.tar.gz: 21d4f3c327dbd7c1fe059dc119d6847871259ae4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0b2a75924e27c81bd72609b98766dcf5da2d41a39c08f8d0ec2e391630bd9e0bd7cf78e5382b38fd00694b2c7162fd8d1da2bb946e0f7d7bdff290fdec23bdd0
|
7
|
+
data.tar.gz: 355340dda6b0ebdd4667a9e65db95be6d7b25dd0698e5c57c9f3e5767eff3098e34c08d92bf2439e05e69d051b596a7573302e7aec61fabe8bfaa20862e97404
|
data/README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# PriorityQueueCxx
|
2
2
|
|
3
|
-
[![Gem Version](https://badge.fury.io/rb/
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/priority_queue_cxx.png)](http://badge.fury.io/rb/priority_queue_cxx)
|
4
4
|
|
5
|
-
FastContainers provides a fast implementatin of priority queues for ruby. Speed is achieved by exposing the c++ standard implementation through a light ruby wrapper. As a bigger project, the library may grow
|
5
|
+
*FastContainers* provides a fast implementatin of priority queues for ruby. Speed is achieved by exposing the c++ standard implementation through a light ruby wrapper. As a bigger project, the library may grow to provide a number of containers that are not in the standard ruby library and are presently only available as pure ruby libraries. Presently, however, the library includes a single class named PriorityQueue. More containers will be added as necessity arises. Contributors and feature requests are most welcome.
|
6
6
|
|
7
|
-
The library exposes a module named
|
7
|
+
The library exposes a module named ```FastContainers``` (to be required using ```require 'fc'```) which provides the PriorityQueue class.
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
@@ -217,7 +217,7 @@ Create a new priority queue and returns it. queue_kind specifies whether to buil
|
|
217
217
|
|
218
218
|
Example:
|
219
219
|
|
220
|
-
```ruby
|
220
|
+
```ruby
|
221
221
|
pq = FastContainers::PriorityQueue.new(:min)
|
222
222
|
```
|
223
223
|
|
@@ -291,4 +291,3 @@ The class Includes Enumerable, so that standard enumeration based methods (e.g.,
|
|
291
291
|
## Notes
|
292
292
|
|
293
293
|
<sup id="ref1">1</sup> It is worth mentioning that, due to how priority queue are implemented by the C++ standard library, this implementation can't efficiently support priority changes. In any case, to support this feature would require important changes in the current API.<a href="#which-is-the-best-priority-queue-implementation-for-ruby" title="back reference">↩</a>
|
294
|
-
|
@@ -22,9 +22,9 @@
|
|
22
22
|
#include <iostream>
|
23
23
|
|
24
24
|
namespace fc_pq {
|
25
|
-
|
25
|
+
|
26
26
|
typedef std::pair<void*,double> PQElem;
|
27
|
-
|
27
|
+
|
28
28
|
class PairsComparator
|
29
29
|
{
|
30
30
|
bool reverse;
|
@@ -36,54 +36,54 @@ namespace fc_pq {
|
|
36
36
|
else return (lhs.second<rhs.second);
|
37
37
|
}
|
38
38
|
};
|
39
|
-
|
39
|
+
|
40
40
|
// --------------------------------------
|
41
41
|
// PQueue
|
42
42
|
// --------------------------------------
|
43
|
-
|
43
|
+
|
44
44
|
typedef std::vector<PQElem> PQueueStorage;
|
45
45
|
typedef unsigned int PQueueStorageVersion;
|
46
|
-
|
47
|
-
typedef struct _PQueue {
|
46
|
+
|
47
|
+
typedef struct _PQueue {
|
48
48
|
PQueueStorage storage;
|
49
49
|
PairsComparator comparator;
|
50
50
|
PQueueStorageVersion version;
|
51
|
-
|
51
|
+
|
52
52
|
_PQueue(PQueueKind kind) : comparator(kind), version(0) { }
|
53
|
-
}* PQueue;
|
53
|
+
}* PQueue;
|
54
54
|
|
55
55
|
void destroy(PQueue q){
|
56
56
|
delete q;
|
57
57
|
}
|
58
|
-
|
58
|
+
|
59
59
|
PQueue create(PQueueKind kind) {
|
60
60
|
return new _PQueue(kind);
|
61
61
|
}
|
62
|
-
|
62
|
+
|
63
63
|
/* Getting the size of the container */
|
64
64
|
unsigned int size(PQueue q) {
|
65
65
|
return q->storage.size();
|
66
66
|
}
|
67
|
-
|
68
|
-
|
67
|
+
|
68
|
+
|
69
69
|
void push(PQueue q, void* value, double priority) {
|
70
70
|
q->version++;
|
71
71
|
q->storage.push_back(PQElem(value, priority));
|
72
72
|
push_heap(q->storage.begin(), q->storage.end(), q->comparator);
|
73
73
|
}
|
74
|
-
|
74
|
+
|
75
75
|
void* top(PQueue q) {
|
76
76
|
return q->storage.at(0).first;
|
77
77
|
}
|
78
|
-
|
78
|
+
|
79
79
|
double top_key(PQueue q) {
|
80
80
|
return q->storage.at(0).second;
|
81
81
|
}
|
82
|
-
|
82
|
+
|
83
83
|
double second_best_key(PQueue q) {
|
84
84
|
if(q->storage.size()==2)
|
85
85
|
return q->storage.at(1).second;
|
86
|
-
|
86
|
+
|
87
87
|
double key1 = q->storage.at(1).second;
|
88
88
|
double key2 = q->storage.at(2).second;
|
89
89
|
if( key1 > key2 ) {
|
@@ -92,31 +92,31 @@ namespace fc_pq {
|
|
92
92
|
return key2;
|
93
93
|
}
|
94
94
|
}
|
95
|
-
|
95
|
+
|
96
96
|
void pop(PQueue q) {
|
97
97
|
q->version++;
|
98
98
|
pop_heap(q->storage.begin(), q->storage.end(), q->comparator);
|
99
99
|
q->storage.pop_back();
|
100
100
|
}
|
101
|
-
|
101
|
+
|
102
102
|
bool empty(PQueue q) {
|
103
103
|
return q->storage.empty();
|
104
104
|
}
|
105
|
-
|
105
|
+
|
106
106
|
// --------------------------------------
|
107
107
|
// Iterator
|
108
108
|
// --------------------------------------
|
109
|
-
|
110
|
-
|
109
|
+
|
110
|
+
|
111
111
|
typedef struct _PQueueIterator {
|
112
112
|
PQueueStorage::const_iterator iterator;
|
113
113
|
PQueue pqueue;
|
114
114
|
PQueueStorage* storage;
|
115
115
|
PQueueStorageVersion version;
|
116
|
-
|
117
|
-
_PQueueIterator(PQueue q) : iterator(q->storage.begin()), pqueue(q), storage(&q->storage), version(q->version)
|
116
|
+
|
117
|
+
_PQueueIterator(PQueue q) : iterator(q->storage.begin()), pqueue(q), storage(&q->storage), version(q->version)
|
118
118
|
{ }
|
119
|
-
|
119
|
+
|
120
120
|
void checkVersion() throw(PQueueException) {
|
121
121
|
if(version != pqueue->version) {
|
122
122
|
throw PQueueException("FastContainers::PriorityQueue - a change in the priority queue invalidated the current iterator.");
|
@@ -124,35 +124,35 @@ namespace fc_pq {
|
|
124
124
|
}
|
125
125
|
} PQueueImplIterator;
|
126
126
|
#define QIT(it) ((PQueueImplIterator*)(it))
|
127
|
-
|
127
|
+
|
128
128
|
/* Returns a new iterator object */
|
129
129
|
PQueueIterator iterator(PQueue q) {
|
130
130
|
PQueueImplIterator* it = new PQueueImplIterator(q);
|
131
131
|
return it;
|
132
132
|
}
|
133
|
-
|
133
|
+
|
134
134
|
void iterator_dispose(PQueueIterator it) {
|
135
135
|
delete it;
|
136
136
|
}
|
137
|
-
|
137
|
+
|
138
138
|
/* Returns the value of the current element */
|
139
139
|
void* iterator_get_value(PQueueIterator it) {
|
140
140
|
return it->iterator->first;
|
141
141
|
}
|
142
|
-
|
142
|
+
|
143
143
|
/* Returns the priority of the current element */
|
144
144
|
double iterator_get_key(PQueueIterator it) {
|
145
145
|
return it->iterator->second;
|
146
146
|
}
|
147
|
-
|
147
|
+
|
148
148
|
/* Moves on to the next element */
|
149
149
|
PQueueIterator iterator_next(PQueueIterator it) throw(PQueueException) {
|
150
150
|
it->checkVersion();
|
151
151
|
it->iterator++;
|
152
152
|
return it;
|
153
153
|
}
|
154
|
-
|
154
|
+
|
155
155
|
bool iterator_end(PQueueIterator it) {
|
156
156
|
return it->iterator == it->storage->end();
|
157
157
|
}
|
158
|
-
}
|
158
|
+
}
|
data/ext/fast_containers/fc_pq.h
CHANGED
@@ -22,64 +22,63 @@
|
|
22
22
|
#define FC_QUEUE_H_KTY6FH1S
|
23
23
|
|
24
24
|
#include <queue>
|
25
|
+
#include <stdexcept>
|
25
26
|
|
26
27
|
namespace fc_pq {
|
27
28
|
class PQueueException : public std::runtime_error {
|
28
29
|
public:
|
29
30
|
PQueueException(const char* msg) : std::runtime_error(msg) {}
|
30
31
|
};
|
31
|
-
|
32
|
+
|
32
33
|
typedef struct _PQueue* PQueue;
|
33
34
|
typedef struct _PQueueIterator* PQueueIterator;
|
34
35
|
typedef enum { MIN_QUEUE, MAX_QUEUE } PQueueKind;
|
35
|
-
|
36
|
+
|
36
37
|
/* Constructor. It defaults to construct a max queue. If true is passed
|
37
|
-
it construct a min queue.*/
|
38
|
+
it construct a min queue.*/
|
38
39
|
PQueue create(PQueueKind kind);
|
39
|
-
|
40
|
+
|
40
41
|
/* Destructor */
|
41
|
-
void destroy(PQueue q);
|
42
|
-
|
42
|
+
void destroy(PQueue q);
|
43
|
+
|
43
44
|
/* Getting the size of the container */
|
44
45
|
unsigned int size(PQueue q);
|
45
|
-
|
46
|
+
|
46
47
|
/* Adding elements */
|
47
48
|
void push(PQueue q, void* value, double priority);
|
48
|
-
|
49
|
+
|
49
50
|
/* Inspecting the queue top (for values) */
|
50
51
|
void* top(PQueue q);
|
51
|
-
|
52
|
+
|
52
53
|
/* Inspecting the queue top (for priorities) */
|
53
54
|
double top_key(PQueue q);
|
54
|
-
|
55
|
+
|
55
56
|
/* Returns the priority of the next best element */
|
56
57
|
double second_best_key(PQueue q);
|
57
|
-
|
58
|
+
|
58
59
|
/* Removing the queue top */
|
59
60
|
void pop(PQueue q);
|
60
|
-
|
61
|
+
|
61
62
|
/* Returns true if the queue is empty */
|
62
63
|
bool empty(PQueue q);
|
63
|
-
|
64
|
+
|
64
65
|
/* Returns a new iterator object */
|
65
66
|
PQueueIterator iterator(PQueue q);
|
66
|
-
|
67
|
+
|
67
68
|
/* Dispose the iterator */
|
68
69
|
void iterator_dispose(PQueueIterator it);
|
69
|
-
|
70
|
+
|
70
71
|
/* Returns the value of the current element */
|
71
72
|
void* iterator_get_value(PQueueIterator it);
|
72
|
-
|
73
|
+
|
73
74
|
/* Returns the priority of the current element */
|
74
75
|
double iterator_get_key(PQueueIterator it);
|
75
|
-
|
76
|
+
|
76
77
|
/* Moves on to the next element */
|
77
78
|
PQueueIterator iterator_next(PQueueIterator it) throw(PQueueException);
|
78
|
-
|
79
|
+
|
79
80
|
/* Return true if the iterator is already out of the container */
|
80
81
|
bool iterator_end(PQueueIterator it);
|
81
82
|
}
|
82
83
|
|
83
84
|
#endif /* end of include guard: FC_QUEUE_H_KTY6FH1S */
|
84
|
-
|
85
|
-
|
data/lib/fc.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: priority_queue_cxx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roberto Esposito
|
@@ -45,7 +45,7 @@ files:
|
|
45
45
|
- test/performance/test_fc_vs_pqueue.rb
|
46
46
|
- test/performance/test_fc_vs_priority_queue.rb
|
47
47
|
- test/test_fast_containers.rb
|
48
|
-
homepage: https://github.com/boborbt/
|
48
|
+
homepage: https://github.com/boborbt/priority_queue_cxx
|
49
49
|
licenses:
|
50
50
|
- MIT
|
51
51
|
metadata: {}
|