FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
mutable_priority_queue.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
9
10namespace FEAT
11{
12
13/*
14Copyright 2010 James Gregson. All rights reserved.
15
16Redistribution and use in source and binary forms, with or without modification, are
17permitted provided that the following conditions are met:
18
19 1. Redistributions of source code must retain the above copyright notice, this list of
20 conditions and the following disclaimer.
21
22 2. Redistributions in binary form must reproduce the above copyright notice, this list
23 of conditions and the following disclaimer in the documentation and/or other materials
24 provided with the distribution.
25
26THIS SOFTWARE IS PROVIDED BY James Gregson ``AS IS'' AND ANY EXPRESS OR IMPLIED
27WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
28FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL James Gregson OR
29CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
32ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35*/
36/*
37 A priority queue in which the priorities of entries may be
38 updated in lg(n) time.
39
40 Simply two maps glued together, one mapping keys (priorities)
41 to values and the other mapping values to keys. Both keys
42 and values must meet the requirements for storage in a
43 std::map, i.e. be less than comparable. Typically not a
44 problem for priorities, but can be a pain if Values are stored
45 by value in the structures. Not a problem if pointers to
46 values are stored.
47
48 insert, erase and update operations are all logarithmic, but
49 approximately 2x slower than std::map since two std::maps
50 are updated in each case.
51*/
52 template< typename _Key, typename _Value, typename _KeyComp=std::greater<_Key>, typename _ValueComp=std::less<_Value> >
54 public:
55 // iterator type for the key2val map, allows in-order traversal of the queue
56 typedef typename std::multimap<_Key,_Value,_KeyComp>::const_iterator iterator;
57 typedef typename std::multimap<_Key,_Value,_KeyComp>::const_iterator const_iterator;
58
59 // default constructor, uses std::greater and std::less for key and value comparisons
60 mutable_priority_queue() : key2Val(_KeyComp()), val2Key(_ValueComp()) { }
61
62 // constructor with key comparison predicate
63 mutable_priority_queue( const _KeyComp &keyComp ) : key2Val(keyComp), val2Key(_ValueComp()){ }
64
65 // constructor with value comparison predicate
66 mutable_priority_queue( const _ValueComp &valComp ) : key2Val(_KeyComp()), val2Key(valComp) { }
67
68 // constructor with both comparison predicates
69 mutable_priority_queue( const _KeyComp &keyComp, const _ValueComp &valComp ) : key2Val(keyComp), val2Key(valComp) { }
70
71 // copy constructor
72 mutable_priority_queue( const mutable_priority_queue<_Key,_Value,_KeyComp,_ValueComp> &x ) : key2Val(x.key2Val), val2Key(x.val2Key) {}
73
74 // destructor, clears both maps
76 key2Val.clear();
77 val2Key.clear();
78 }
79
80 // assignment operator
82 key2Val = x.key2Val;
83 val2Key = x.val2Key;
84 }
85
86 // returns an iterator for the front of the queue
87 inline iterator begin(){
88 return key2Val.begin();
89 }
90
91 // returns an iterator for the end of the queue
92 inline iterator end(){
93 return key2Val.end();
94 }
95
96 // empties the queue
97 inline void clear(){
98 key2Val.clear();
99 val2Key.clear();
100 }
101
102 // removes from both maps the value val
103 inline void erase( _Value val ){
104 typename std::map<_Value,_Key,_ValueComp>::iterator iter=val2Key.find( val );
105 if( iter != val2Key.end() ){
106 key2Val.erase( _find(iter->second,iter->first) );
107 val2Key.erase( iter );
108 }
109 }
110
111 // returns true if the queue is empty
112 inline bool empty(){
113 return key2Val.empty();
114 }
115
116 // returns the value at the front of the queue
117 inline _Value front_value(){
118 return (*key2Val.begin()).second;
119 }
120
121 // returns the key at the front of the queue
122 inline _Value front_key(){
123 return (*key2Val.begin()).first;
124 }
125
126 // removes the front entry in the queue
127 inline void pop(){
128 auto iter = key2Val.begin();
129 _erase( iter );
130 }
131
132 // push an entry onto the queue
133 inline void push( _Value val, _Key key ){
134 insert( val, key );
135 }
136
137 // adds a value key pair to the queue.
138 // an attempt first has to be made to erase
139 // the item, since it could exist with another
140 // key and the queue does not allow multiple
141 // keys per value
142 inline void insert( _Value val, _Key key ){
143 erase( val );
144 key2Val.insert( std::pair<_Key,_Value>( key, val ) );
145 val2Key.insert( std::pair<_Value,_Key>( val, key ) );
146 }
147
148 // update the key associated with a value,
149 // simply a convenience function that calls
150 // insert()
151 inline void update( _Value val, _Key key ){
152 insert( val, key );
153 }
154
155 // returns the number of elements in the queue
156 inline size_t size(){
157 return key2Val.size();
158 }
159
160 inline size_t count(_Value val)
161 {
162 return val2Key.count(val);
163 }
164
165 private:
166 std::multimap< _Key, _Value, _KeyComp > key2Val;
167 std::map< _Value, _Key, _ValueComp > val2Key;
168
169 inline void _erase( typename std::multimap<_Key,_Value,_KeyComp>::iterator it ){
170 val2Key.erase( it->second );
171 key2Val.erase( it );
172 }
173
174 inline typename std::multimap<_Key,_Value,_KeyComp>::iterator _find( _Key k, _Value v ){
175 typename std::multimap<_Key,_Value,_KeyComp>::iterator it = key2Val.lower_bound(k);
176 typename std::multimap<_Key,_Value,_KeyComp>::iterator hi = key2Val.upper_bound(k);
177 while( it != hi ){
178 if( it->second == v )
179 return it;
180 it++;
181 }
182 return key2Val.end();
183 }
184 };
185}
FEAT Kernel base header.
FEAT namespace.
Definition: adjactor.hpp:12