Port preflow push max flow alg. from svn -r3516 (#176)
Namely,
- port the files
- apply the migrate script
- apply the unify script
- break the long lines in lemon/preflow.h
- convert the .dim test file to .lgf
- fix compilation problems
1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2008
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_BITS_ARRAY_MAP_H
20 #define LEMON_BITS_ARRAY_MAP_H
24 #include <lemon/bits/traits.h>
25 #include <lemon/bits/alteration_notifier.h>
26 #include <lemon/concept_check.h>
27 #include <lemon/concepts/maps.h>
31 // \brief Graph map based on the array storage.
37 // \brief Graph map based on the array storage.
39 // The ArrayMap template class is graph map structure that automatically
40 // updates the map when a key is added to or erased from the graph.
41 // This map uses the allocators to implement the container functionality.
43 // The template parameters are the Graph, the current Item type and
44 // the Value type of the map.
45 template <typename _Graph, typename _Item, typename _Value>
47 : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
53 // The reference map tag.
54 typedef True ReferenceMapTag;
56 // The key type of the map.
58 // The value type of the map.
61 // The const reference type of the map.
62 typedef const _Value& ConstReference;
63 // The reference type of the map.
64 typedef _Value& Reference;
67 typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier;
69 // The MapBase of the Map which imlements the core regisitry function.
70 typedef typename Notifier::ObserverBase Parent;
73 typedef std::allocator<Value> Allocator;
77 // \brief Graph initialized map constructor.
79 // Graph initialized map constructor.
80 explicit ArrayMap(const Graph& graph) {
81 Parent::attach(graph.notifier(Item()));
83 Notifier* nf = Parent::notifier();
85 for (nf->first(it); it != INVALID; nf->next(it)) {
87 allocator.construct(&(values[id]), Value());
91 // \brief Constructor to use default value to initialize the map.
93 // It constructs a map and initialize all of the the map.
94 ArrayMap(const Graph& graph, const Value& value) {
95 Parent::attach(graph.notifier(Item()));
97 Notifier* nf = Parent::notifier();
99 for (nf->first(it); it != INVALID; nf->next(it)) {
100 int id = nf->id(it);;
101 allocator.construct(&(values[id]), value);
106 // \brief Constructor to copy a map of the same map type.
108 // Constructor to copy a map of the same map type.
109 ArrayMap(const ArrayMap& copy) : Parent() {
110 if (copy.attached()) {
111 attach(*copy.notifier());
113 capacity = copy.capacity;
114 if (capacity == 0) return;
115 values = allocator.allocate(capacity);
116 Notifier* nf = Parent::notifier();
118 for (nf->first(it); it != INVALID; nf->next(it)) {
119 int id = nf->id(it);;
120 allocator.construct(&(values[id]), copy.values[id]);
124 // \brief Assign operator.
126 // This operator assigns for each item in the map the
127 // value mapped to the same item in the copied map.
128 // The parameter map should be indiced with the same
129 // itemset because this assign operator does not change
130 // the container of the map.
131 ArrayMap& operator=(const ArrayMap& cmap) {
132 return operator=<ArrayMap>(cmap);
136 // \brief Template assign operator.
138 // The given parameter should be conform to the ReadMap
139 // concecpt and could be indiced by the current item set of
140 // the NodeMap. In this case the value for each item
141 // is assigned by the value of the given ReadMap.
142 template <typename CMap>
143 ArrayMap& operator=(const CMap& cmap) {
144 checkConcept<concepts::ReadMap<Key, _Value>, CMap>();
145 const typename Parent::Notifier* nf = Parent::notifier();
147 for (nf->first(it); it != INVALID; nf->next(it)) {
154 // \brief The destructor of the map.
156 // The destructor of the map.
157 virtual ~ArrayMap() {
166 using Parent::attach;
167 using Parent::detach;
168 using Parent::attached;
172 // \brief The subscript operator.
174 // The subscript operator. The map can be subscripted by the
175 // actual keys of the graph.
176 Value& operator[](const Key& key) {
177 int id = Parent::notifier()->id(key);
181 // \brief The const subscript operator.
183 // The const subscript operator. The map can be subscripted by the
184 // actual keys of the graph.
185 const Value& operator[](const Key& key) const {
186 int id = Parent::notifier()->id(key);
190 // \brief Setter function of the map.
192 // Setter function of the map. Equivalent with map[key] = val.
193 // This is a compatibility feature with the not dereferable maps.
194 void set(const Key& key, const Value& val) {
200 // \brief Adds a new key to the map.
202 // It adds a new key to the map. It is called by the observer notifier
203 // and it overrides the add() member function of the observer base.
204 virtual void add(const Key& key) {
205 Notifier* nf = Parent::notifier();
206 int id = nf->id(key);
207 if (id >= capacity) {
208 int new_capacity = (capacity == 0 ? 1 : capacity);
209 while (new_capacity <= id) {
212 Value* new_values = allocator.allocate(new_capacity);
214 for (nf->first(it); it != INVALID; nf->next(it)) {
215 int jd = nf->id(it);;
217 allocator.construct(&(new_values[jd]), values[jd]);
218 allocator.destroy(&(values[jd]));
221 if (capacity != 0) allocator.deallocate(values, capacity);
223 capacity = new_capacity;
225 allocator.construct(&(values[id]), Value());
228 // \brief Adds more new keys to the map.
230 // It adds more new keys to the map. It is called by the observer notifier
231 // and it overrides the add() member function of the observer base.
232 virtual void add(const std::vector<Key>& keys) {
233 Notifier* nf = Parent::notifier();
235 for (int i = 0; i < int(keys.size()); ++i) {
236 int id = nf->id(keys[i]);
241 if (max_id >= capacity) {
242 int new_capacity = (capacity == 0 ? 1 : capacity);
243 while (new_capacity <= max_id) {
246 Value* new_values = allocator.allocate(new_capacity);
248 for (nf->first(it); it != INVALID; nf->next(it)) {
251 for (int i = 0; i < int(keys.size()); ++i) {
252 int jd = nf->id(keys[i]);
259 allocator.construct(&(new_values[id]), values[id]);
260 allocator.destroy(&(values[id]));
262 if (capacity != 0) allocator.deallocate(values, capacity);
264 capacity = new_capacity;
266 for (int i = 0; i < int(keys.size()); ++i) {
267 int id = nf->id(keys[i]);
268 allocator.construct(&(values[id]), Value());
272 // \brief Erase a key from the map.
274 // Erase a key from the map. It is called by the observer notifier
275 // and it overrides the erase() member function of the observer base.
276 virtual void erase(const Key& key) {
277 int id = Parent::notifier()->id(key);
278 allocator.destroy(&(values[id]));
281 // \brief Erase more keys from the map.
283 // Erase more keys from the map. It is called by the observer notifier
284 // and it overrides the erase() member function of the observer base.
285 virtual void erase(const std::vector<Key>& keys) {
286 for (int i = 0; i < int(keys.size()); ++i) {
287 int id = Parent::notifier()->id(keys[i]);
288 allocator.destroy(&(values[id]));
292 // \brief Builds the map.
294 // It builds the map. It is called by the observer notifier
295 // and it overrides the build() member function of the observer base.
296 virtual void build() {
297 Notifier* nf = Parent::notifier();
300 for (nf->first(it); it != INVALID; nf->next(it)) {
301 int id = nf->id(it);;
302 allocator.construct(&(values[id]), Value());
306 // \brief Clear the map.
308 // It erase all items from the map. It is called by the observer notifier
309 // and it overrides the clear() member function of the observer base.
310 virtual void clear() {
311 Notifier* nf = Parent::notifier();
314 for (nf->first(it); it != INVALID; nf->next(it)) {
316 allocator.destroy(&(values[id]));
318 allocator.deallocate(values, capacity);
325 void allocate_memory() {
326 int max_id = Parent::notifier()->maxId();
333 while (capacity <= max_id) {
336 values = allocator.allocate(capacity);