priority_queue_cxx17 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ require 'mkmf'
2
+
3
+ extension_name = 'fast_containers'
4
+
5
+ create_makefile(extension_name)
@@ -0,0 +1,158 @@
1
+ // Copyright (c) 2014 Roberto Esposito
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining
4
+ // a copy of this software and associated documentation files (the
5
+ // "Software"), to deal in the Software without restriction, including
6
+ // without limitation the rights to use, copy, modify, merge, publish,
7
+ // distribute, sublicense, and/or sell copies of the Software, and to
8
+ // permit persons to whom the Software is furnished to do so, subject to
9
+ // the following conditions:
10
+ //
11
+ // The above copyright notice and this permission notice shall be
12
+ // included in all copies or substantial portions of the Software.
13
+ //
14
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+ #include "fc_pq.h"
22
+ #include <iostream>
23
+
24
+ namespace fc_pq {
25
+
26
+ typedef std::pair<void*,double> PQElem;
27
+
28
+ class PairsComparator
29
+ {
30
+ bool reverse;
31
+ public:
32
+ PairsComparator(PQueueKind kind) {reverse=(kind==MIN_QUEUE);}
33
+ bool operator() (const PQElem& lhs, const PQElem& rhs) const
34
+ {
35
+ if (reverse) return (lhs.second>rhs.second);
36
+ else return (lhs.second<rhs.second);
37
+ }
38
+ };
39
+
40
+ // --------------------------------------
41
+ // PQueue
42
+ // --------------------------------------
43
+
44
+ typedef std::vector<PQElem> PQueueStorage;
45
+ typedef unsigned int PQueueStorageVersion;
46
+
47
+ typedef struct _PQueue {
48
+ PQueueStorage storage;
49
+ PairsComparator comparator;
50
+ PQueueStorageVersion version;
51
+
52
+ _PQueue(PQueueKind kind) : comparator(kind), version(0) { }
53
+ }* PQueue;
54
+
55
+ void destroy(PQueue q){
56
+ delete q;
57
+ }
58
+
59
+ PQueue create(PQueueKind kind) {
60
+ return new _PQueue(kind);
61
+ }
62
+
63
+ /* Getting the size of the container */
64
+ unsigned int size(PQueue q) {
65
+ return q->storage.size();
66
+ }
67
+
68
+
69
+ void push(PQueue q, void* value, double priority) {
70
+ q->version++;
71
+ q->storage.push_back(PQElem(value, priority));
72
+ push_heap(q->storage.begin(), q->storage.end(), q->comparator);
73
+ }
74
+
75
+ void* top(PQueue q) {
76
+ return q->storage.at(0).first;
77
+ }
78
+
79
+ double top_key(PQueue q) {
80
+ return q->storage.at(0).second;
81
+ }
82
+
83
+ double second_best_key(PQueue q) {
84
+ if(q->storage.size()==2)
85
+ return q->storage.at(1).second;
86
+
87
+ double key1 = q->storage.at(1).second;
88
+ double key2 = q->storage.at(2).second;
89
+ if( key1 > key2 ) {
90
+ return key1;
91
+ } else {
92
+ return key2;
93
+ }
94
+ }
95
+
96
+ void pop(PQueue q) {
97
+ q->version++;
98
+ pop_heap(q->storage.begin(), q->storage.end(), q->comparator);
99
+ q->storage.pop_back();
100
+ }
101
+
102
+ bool empty(PQueue q) {
103
+ return q->storage.empty();
104
+ }
105
+
106
+ // --------------------------------------
107
+ // Iterator
108
+ // --------------------------------------
109
+
110
+
111
+ typedef struct _PQueueIterator {
112
+ PQueueStorage::const_iterator iterator;
113
+ PQueue pqueue;
114
+ PQueueStorage* storage;
115
+ PQueueStorageVersion version;
116
+
117
+ _PQueueIterator(PQueue q) : iterator(q->storage.begin()), pqueue(q), storage(&q->storage), version(q->version)
118
+ { }
119
+
120
+ void checkVersion() throw() {
121
+ if(version != pqueue->version) {
122
+ std::terminate();
123
+ }
124
+ }
125
+ } PQueueImplIterator;
126
+ #define QIT(it) ((PQueueImplIterator*)(it))
127
+
128
+ /* Returns a new iterator object */
129
+ PQueueIterator iterator(PQueue q) {
130
+ PQueueImplIterator* it = new PQueueImplIterator(q);
131
+ return it;
132
+ }
133
+
134
+ void iterator_dispose(PQueueIterator it) {
135
+ delete it;
136
+ }
137
+
138
+ /* Returns the value of the current element */
139
+ void* iterator_get_value(PQueueIterator it) {
140
+ return it->iterator->first;
141
+ }
142
+
143
+ /* Returns the priority of the current element */
144
+ double iterator_get_key(PQueueIterator it) {
145
+ return it->iterator->second;
146
+ }
147
+
148
+ /* Moves on to the next element */
149
+ PQueueIterator iterator_next(PQueueIterator it) throw() {
150
+ it->checkVersion();
151
+ it->iterator++;
152
+ return it;
153
+ }
154
+
155
+ bool iterator_end(PQueueIterator it) {
156
+ return it->iterator == it->storage->end();
157
+ }
158
+ }
@@ -0,0 +1,84 @@
1
+ // Copyright (c) 2014 Roberto Esposito
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining
4
+ // a copy of this software and associated documentation files (the
5
+ // "Software"), to deal in the Software without restriction, including
6
+ // without limitation the rights to use, copy, modify, merge, publish,
7
+ // distribute, sublicense, and/or sell copies of the Software, and to
8
+ // permit persons to whom the Software is furnished to do so, subject to
9
+ // the following conditions:
10
+ //
11
+ // The above copyright notice and this permission notice shall be
12
+ // included in all copies or substantial portions of the Software.
13
+ //
14
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+ #ifndef FC_QUEUE_H_KTY6FH1S
22
+ #define FC_QUEUE_H_KTY6FH1S
23
+
24
+ #include <queue>
25
+ #include <stdexcept>
26
+
27
+ namespace fc_pq {
28
+ class PQueueException : public std::runtime_error {
29
+ public:
30
+ PQueueException(const char* msg) : std::runtime_error(msg) {}
31
+ };
32
+
33
+ typedef struct _PQueue* PQueue;
34
+ typedef struct _PQueueIterator* PQueueIterator;
35
+ typedef enum { MIN_QUEUE, MAX_QUEUE } PQueueKind;
36
+
37
+ /* Constructor. It defaults to construct a max queue. If true is passed
38
+ it construct a min queue.*/
39
+ PQueue create(PQueueKind kind);
40
+
41
+ /* Destructor */
42
+ void destroy(PQueue q);
43
+
44
+ /* Getting the size of the container */
45
+ unsigned int size(PQueue q);
46
+
47
+ /* Adding elements */
48
+ void push(PQueue q, void* value, double priority);
49
+
50
+ /* Inspecting the queue top (for values) */
51
+ void* top(PQueue q);
52
+
53
+ /* Inspecting the queue top (for priorities) */
54
+ double top_key(PQueue q);
55
+
56
+ /* Returns the priority of the next best element */
57
+ double second_best_key(PQueue q);
58
+
59
+ /* Removing the queue top */
60
+ void pop(PQueue q);
61
+
62
+ /* Returns true if the queue is empty */
63
+ bool empty(PQueue q);
64
+
65
+ /* Returns a new iterator object */
66
+ PQueueIterator iterator(PQueue q);
67
+
68
+ /* Dispose the iterator */
69
+ void iterator_dispose(PQueueIterator it);
70
+
71
+ /* Returns the value of the current element */
72
+ void* iterator_get_value(PQueueIterator it);
73
+
74
+ /* Returns the priority of the current element */
75
+ double iterator_get_key(PQueueIterator it);
76
+
77
+ /* Moves on to the next element */
78
+ PQueueIterator iterator_next(PQueueIterator it) throw();
79
+
80
+ /* Return true if the iterator is already out of the container */
81
+ bool iterator_end(PQueueIterator it);
82
+ }
83
+
84
+ #endif /* end of include guard: FC_QUEUE_H_KTY6FH1S */
@@ -0,0 +1,32 @@
1
+ # Copyright (c) 2014 Roberto Esposito
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+ require 'fast_containers'
22
+
23
+ module FastContainers
24
+ VERSION = "0.3.4"
25
+
26
+ class PriorityQueue
27
+ include Enumerable
28
+
29
+ alias_method :next, :top
30
+ alias_method :next_key, :top_key
31
+ end
32
+ end
@@ -0,0 +1,37 @@
1
+ # Copyright (c) 2014 Roberto Esposito
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+ require 'fc'
22
+ require 'algorithms'
23
+ require 'benchmark'
24
+
25
+ # Performs 50.000 pushes and pops in priority queues using the fc and
26
+ # algorithms implementations and reports the time spent.
27
+
28
+ N = 50_000
29
+ algo_pq = Containers::PriorityQueue.new
30
+ fc_pq = FastContainers::PriorityQueue.new(:min)
31
+
32
+ Benchmark.bm do |bm|
33
+ bm.report('algo:push') { N.times { |n| algo_pq.push(n.to_s, rand) } }
34
+ bm.report('fc:push') { N.times { |n| fc_pq.push(n.to_s, rand) } }
35
+ bm.report('algo:pop') { N.times { algo_pq.pop } }
36
+ bm.report('fc:pop') { N.times { fc_pq.pop } }
37
+ end
@@ -0,0 +1,37 @@
1
+ # Copyright (c) 2014 Roberto Esposito
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+ require 'fc'
22
+ require 'priority_queue'
23
+ require 'benchmark'
24
+
25
+ # Performs 5,000,000 pushes and pops in priority queues using the fc and
26
+ # algorithms implementations and reports the time spent.
27
+
28
+ N = 5_000_000
29
+ pq_pq = CPriorityQueue.new
30
+ fc_pq = FastContainers::PriorityQueue.new(:min)
31
+
32
+ Benchmark.bm do |bm|
33
+ bm.report('pq:push') { N.times { |n| pq_pq.push(n.to_s,rand) } }
34
+ bm.report('fc:push') { N.times { |n| fc_pq.push(n.to_s, rand) } }
35
+ bm.report('pq:pop') { N.times { pq_pq.delete_min } }
36
+ bm.report('fc:pop') { N.times { fc_pq.pop } }
37
+ end
@@ -0,0 +1,37 @@
1
+ # Copyright (c) 2014 Roberto Esposito
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+ require 'fc'
22
+ require 'em-priority-queue'
23
+ require 'benchmark'
24
+
25
+ # Performs 50.000 pushes and pops in priority queues using the fc and
26
+ # algorithms implementations and reports the time spent.
27
+
28
+ N = 500_000
29
+ em_pq = EM::PriorityQueue.new
30
+ fc_pq = FastContainers::PriorityQueue.new(:min)
31
+
32
+ Benchmark.bm do |bm|
33
+ bm.report('em:push') { N.times { |n| em_pq.push(n.to_s, rand) } }
34
+ bm.report('fc:push') { N.times { |n| fc_pq.push(n.to_s, rand) } }
35
+ bm.report('em:pop') { N.times { em_pq.pop {} } }
36
+ bm.report('fc:pop') { N.times { fc_pq.pop } }
37
+ end
@@ -0,0 +1,37 @@
1
+ # Copyright (c) 2014 Roberto Esposito
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+ require 'fc'
22
+ require 'pqueue'
23
+ require 'benchmark'
24
+
25
+ # Performs 50.000 pushes and pops in priority queues using the fc and
26
+ # algorithms implementations and reports the time spent.
27
+
28
+ N = 100_000
29
+ pq_pq = PQueue.new { |x,y| x[1] <=> y[1] }
30
+ fc_pq = FastContainers::PriorityQueue.new(:min)
31
+
32
+ Benchmark.bm do |bm|
33
+ bm.report('pq:push') { N.times { |n| pq_pq.push([n,rand]) } }
34
+ bm.report('fc:push') { N.times { |n| fc_pq.push(n.to_s, rand) } }
35
+ bm.report('pq:pop') { N.times { pq_pq.pop } }
36
+ bm.report('fc:pop') { N.times { fc_pq.pop } }
37
+ end
@@ -0,0 +1,37 @@
1
+ # Copyright (c) 2014 Roberto Esposito
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+ require 'fc'
22
+ require 'priority_queue'
23
+ require 'benchmark'
24
+
25
+ # Performs 50.000 pushes and pops in priority queues using the fc and
26
+ # algorithms implementations and reports the time spent.
27
+
28
+ N = 50_000
29
+ pq_pq = PriorityQueue.new
30
+ fc_pq = FastContainers::PriorityQueue.new(:min)
31
+
32
+ Benchmark.bm do |bm|
33
+ bm.report('pq:push') { N.times { |n| pq_pq[rand] << n.to_s } }
34
+ bm.report('fc:push') { N.times { |n| fc_pq.push(n.to_s, rand) } }
35
+ bm.report('pq:pop') { N.times { pq_pq.shift } }
36
+ bm.report('fc:pop') { N.times { fc_pq.pop } }
37
+ end