| ... | ... |
@@ -212,193 +212,195 @@ |
| 212 | 212 |
///Use DigraphCopy() instead. |
| 213 | 213 |
|
| 214 | 214 |
///Assignment of SmartDigraph to another one is \e not allowed. |
| 215 | 215 |
///Use DigraphCopy() instead. |
| 216 | 216 |
void operator=(const SmartDigraph &) {}
|
| 217 | 217 |
|
| 218 | 218 |
public: |
| 219 | 219 |
|
| 220 | 220 |
/// Constructor |
| 221 | 221 |
|
| 222 | 222 |
/// Constructor. |
| 223 | 223 |
/// |
| 224 | 224 |
SmartDigraph() {};
|
| 225 | 225 |
|
| 226 | 226 |
///Add a new node to the digraph. |
| 227 | 227 |
|
| 228 | 228 |
/// \return the new node. |
| 229 | 229 |
/// |
| 230 | 230 |
Node addNode() { return Parent::addNode(); }
|
| 231 | 231 |
|
| 232 | 232 |
///Add a new arc to the digraph. |
| 233 | 233 |
|
| 234 | 234 |
///Add a new arc to the digraph with source node \c s |
| 235 | 235 |
///and target node \c t. |
| 236 | 236 |
///\return the new arc. |
| 237 | 237 |
Arc addArc(const Node& s, const Node& t) {
|
| 238 | 238 |
return Parent::addArc(s, t); |
| 239 | 239 |
} |
| 240 | 240 |
|
| 241 | 241 |
/// \brief Using this it is possible to avoid the superfluous memory |
| 242 | 242 |
/// allocation. |
| 243 | 243 |
|
| 244 | 244 |
/// Using this it is possible to avoid the superfluous memory |
| 245 | 245 |
/// allocation: if you know that the digraph you want to build will |
| 246 | 246 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
| 247 | 247 |
/// then it is worth reserving space for this amount before starting |
| 248 | 248 |
/// to build the digraph. |
| 249 | 249 |
/// \sa reserveArc |
| 250 | 250 |
void reserveNode(int n) { nodes.reserve(n); };
|
| 251 | 251 |
|
| 252 | 252 |
/// \brief Using this it is possible to avoid the superfluous memory |
| 253 | 253 |
/// allocation. |
| 254 | 254 |
|
| 255 | 255 |
/// Using this it is possible to avoid the superfluous memory |
| 256 | 256 |
/// allocation: if you know that the digraph you want to build will |
| 257 | 257 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
| 258 | 258 |
/// then it is worth reserving space for this amount before starting |
| 259 | 259 |
/// to build the digraph. |
| 260 | 260 |
/// \sa reserveNode |
| 261 | 261 |
void reserveArc(int m) { arcs.reserve(m); };
|
| 262 | 262 |
|
| 263 | 263 |
/// \brief Node validity check |
| 264 | 264 |
/// |
| 265 | 265 |
/// This function gives back true if the given node is valid, |
| 266 | 266 |
/// ie. it is a real node of the graph. |
| 267 | 267 |
/// |
| 268 | 268 |
/// \warning A removed node (using Snapshot) could become valid again |
| 269 | 269 |
/// when new nodes are added to the graph. |
| 270 | 270 |
bool valid(Node n) const { return Parent::valid(n); }
|
| 271 | 271 |
|
| 272 | 272 |
/// \brief Arc validity check |
| 273 | 273 |
/// |
| 274 | 274 |
/// This function gives back true if the given arc is valid, |
| 275 | 275 |
/// ie. it is a real arc of the graph. |
| 276 | 276 |
/// |
| 277 | 277 |
/// \warning A removed arc (using Snapshot) could become valid again |
| 278 | 278 |
/// when new arcs are added to the graph. |
| 279 | 279 |
bool valid(Arc a) const { return Parent::valid(a); }
|
| 280 | 280 |
|
| 281 | 281 |
///Clear the digraph. |
| 282 | 282 |
|
| 283 | 283 |
///Erase all the nodes and arcs from the digraph. |
| 284 | 284 |
/// |
| 285 | 285 |
void clear() {
|
| 286 | 286 |
Parent::clear(); |
| 287 | 287 |
} |
| 288 | 288 |
|
| 289 | 289 |
///Split a node. |
| 290 | 290 |
|
| 291 | 291 |
///This function splits a node. First a new node is added to the digraph, |
| 292 | 292 |
///then the source of each outgoing arc of \c n is moved to this new node. |
| 293 | 293 |
///If \c connect is \c true (this is the default value), then a new arc |
| 294 | 294 |
///from \c n to the newly created node is also added. |
| 295 | 295 |
///\return The newly created node. |
| 296 | 296 |
/// |
| 297 | 297 |
///\note The <tt>Arc</tt>s |
| 298 | 298 |
///referencing a moved arc remain |
| 299 | 299 |
///valid. However <tt>InArc</tt>'s and <tt>OutArc</tt>'s |
| 300 | 300 |
///may be invalidated. |
| 301 | 301 |
///\warning This functionality cannot be used together with the Snapshot |
| 302 | 302 |
///feature. |
| 303 | 303 |
Node split(Node n, bool connect = true) |
| 304 | 304 |
{
|
| 305 | 305 |
Node b = addNode(); |
| 306 | 306 |
nodes[b._id].first_out=nodes[n._id].first_out; |
| 307 | 307 |
nodes[n._id].first_out=-1; |
| 308 |
for(int i=nodes[b._id].first_out;i!=-1; |
|
| 308 |
for(int i=nodes[b._id].first_out; i!=-1; i=arcs[i].next_out) {
|
|
| 309 |
arcs[i].source=b._id; |
|
| 310 |
} |
|
| 309 | 311 |
if(connect) addArc(n,b); |
| 310 | 312 |
return b; |
| 311 | 313 |
} |
| 312 | 314 |
|
| 313 | 315 |
public: |
| 314 | 316 |
|
| 315 | 317 |
class Snapshot; |
| 316 | 318 |
|
| 317 | 319 |
protected: |
| 318 | 320 |
|
| 319 | 321 |
void restoreSnapshot(const Snapshot &s) |
| 320 | 322 |
{
|
| 321 | 323 |
while(s.arc_num<arcs.size()) {
|
| 322 | 324 |
Arc arc = arcFromId(arcs.size()-1); |
| 323 | 325 |
Parent::notifier(Arc()).erase(arc); |
| 324 | 326 |
nodes[arcs.back().source].first_out=arcs.back().next_out; |
| 325 | 327 |
nodes[arcs.back().target].first_in=arcs.back().next_in; |
| 326 | 328 |
arcs.pop_back(); |
| 327 | 329 |
} |
| 328 | 330 |
while(s.node_num<nodes.size()) {
|
| 329 | 331 |
Node node = nodeFromId(nodes.size()-1); |
| 330 | 332 |
Parent::notifier(Node()).erase(node); |
| 331 | 333 |
nodes.pop_back(); |
| 332 | 334 |
} |
| 333 | 335 |
} |
| 334 | 336 |
|
| 335 | 337 |
public: |
| 336 | 338 |
|
| 337 | 339 |
///Class to make a snapshot of the digraph and to restrore to it later. |
| 338 | 340 |
|
| 339 | 341 |
///Class to make a snapshot of the digraph and to restrore to it later. |
| 340 | 342 |
/// |
| 341 | 343 |
///The newly added nodes and arcs can be removed using the |
| 342 | 344 |
///restore() function. |
| 343 | 345 |
///\note After you restore a state, you cannot restore |
| 344 | 346 |
///a later state, in other word you cannot add again the arcs deleted |
| 345 | 347 |
///by restore() using another one Snapshot instance. |
| 346 | 348 |
/// |
| 347 | 349 |
///\warning If you do not use correctly the snapshot that can cause |
| 348 | 350 |
///either broken program, invalid state of the digraph, valid but |
| 349 | 351 |
///not the restored digraph or no change. Because the runtime performance |
| 350 | 352 |
///the validity of the snapshot is not stored. |
| 351 | 353 |
class Snapshot |
| 352 | 354 |
{
|
| 353 | 355 |
SmartDigraph *_graph; |
| 354 | 356 |
protected: |
| 355 | 357 |
friend class SmartDigraph; |
| 356 | 358 |
unsigned int node_num; |
| 357 | 359 |
unsigned int arc_num; |
| 358 | 360 |
public: |
| 359 | 361 |
///Default constructor. |
| 360 | 362 |
|
| 361 | 363 |
///Default constructor. |
| 362 | 364 |
///To actually make a snapshot you must call save(). |
| 363 | 365 |
/// |
| 364 | 366 |
Snapshot() : _graph(0) {}
|
| 365 | 367 |
///Constructor that immediately makes a snapshot |
| 366 | 368 |
|
| 367 | 369 |
///This constructor immediately makes a snapshot of the digraph. |
| 368 | 370 |
///\param graph The digraph we make a snapshot of. |
| 369 | 371 |
Snapshot(SmartDigraph &graph) : _graph(&graph) {
|
| 370 | 372 |
node_num=_graph->nodes.size(); |
| 371 | 373 |
arc_num=_graph->arcs.size(); |
| 372 | 374 |
} |
| 373 | 375 |
|
| 374 | 376 |
///Make a snapshot. |
| 375 | 377 |
|
| 376 | 378 |
///Make a snapshot of the digraph. |
| 377 | 379 |
/// |
| 378 | 380 |
///This function can be called more than once. In case of a repeated |
| 379 | 381 |
///call, the previous snapshot gets lost. |
| 380 | 382 |
///\param graph The digraph we make the snapshot of. |
| 381 | 383 |
void save(SmartDigraph &graph) |
| 382 | 384 |
{
|
| 383 | 385 |
_graph=&graph; |
| 384 | 386 |
node_num=_graph->nodes.size(); |
| 385 | 387 |
arc_num=_graph->arcs.size(); |
| 386 | 388 |
} |
| 387 | 389 |
|
| 388 | 390 |
///Undo the changes until a snapshot. |
| 389 | 391 |
|
| 390 | 392 |
///Undo the changes until a snapshot created by save(). |
| 391 | 393 |
/// |
| 392 | 394 |
///\note After you restored a state, you cannot restore |
| 393 | 395 |
///a later state, in other word you cannot add again the arcs deleted |
| 394 | 396 |
///by restore(). |
| 395 | 397 |
void restore() |
| 396 | 398 |
{
|
| 397 | 399 |
_graph->restoreSnapshot(*this); |
| 398 | 400 |
} |
| 399 | 401 |
}; |
| 400 | 402 |
}; |
| 401 | 403 |
|
| 402 | 404 |
|
| 403 | 405 |
class SmartGraphBase {
|
| 404 | 406 |
|
0 comments (0 inline)