0
2
0
| ... | ... |
@@ -194,145 +194,191 @@ |
| 194 | 194 |
} else {
|
| 195 | 195 |
node_first_out[source] = arc_index; |
| 196 | 196 |
} |
| 197 | 197 |
} |
| 198 | 198 |
node_first_out[node_num] = arc_num; |
| 199 | 199 |
} |
| 200 | 200 |
|
| 201 | 201 |
protected: |
| 202 | 202 |
|
| 203 | 203 |
void fastFirstOut(Arc& e, const Node& n) const {
|
| 204 | 204 |
e.id = node_first_out[n.id]; |
| 205 | 205 |
} |
| 206 | 206 |
|
| 207 | 207 |
static void fastNextOut(Arc& e) {
|
| 208 | 208 |
++e.id; |
| 209 | 209 |
} |
| 210 | 210 |
void fastLastOut(Arc& e, const Node& n) const {
|
| 211 | 211 |
e.id = node_first_out[n.id + 1]; |
| 212 | 212 |
} |
| 213 | 213 |
|
| 214 | 214 |
protected: |
| 215 | 215 |
bool built; |
| 216 | 216 |
int node_num; |
| 217 | 217 |
int arc_num; |
| 218 | 218 |
int *node_first_out; |
| 219 | 219 |
int *node_first_in; |
| 220 | 220 |
int *arc_source; |
| 221 | 221 |
int *arc_target; |
| 222 | 222 |
int *arc_next_in; |
| 223 | 223 |
int *arc_next_out; |
| 224 | 224 |
}; |
| 225 | 225 |
|
| 226 | 226 |
typedef DigraphExtender<StaticDigraphBase> ExtendedStaticDigraphBase; |
| 227 | 227 |
|
| 228 | 228 |
|
| 229 | 229 |
/// \ingroup graphs |
| 230 | 230 |
/// |
| 231 | 231 |
/// \brief A static directed graph class. |
| 232 | 232 |
/// |
| 233 | 233 |
/// \ref StaticDigraph is a highly efficient digraph implementation, |
| 234 | 234 |
/// but it is fully static. |
| 235 | 235 |
/// It stores only two \c int values for each node and only four \c int |
| 236 | 236 |
/// values for each arc. Moreover it provides faster item iteration than |
| 237 | 237 |
/// \ref ListDigraph and \ref SmartDigraph, especially using \c OutArcIt |
| 238 | 238 |
/// iterators, since its arcs are stored in an appropriate order. |
| 239 | 239 |
/// However it only provides build() and clear() functions and does not |
| 240 | 240 |
/// support any other modification of the digraph. |
| 241 | 241 |
/// |
| 242 |
/// Since this digraph structure is completely static, its nodes and arcs |
|
| 243 |
/// can be indexed with integers from the ranges <tt>[0..nodeNum()-1]</tt> |
|
| 244 |
/// and <tt>[0..arcNum()-1]</tt>, respectively. |
|
| 245 |
/// The index of an item is the same as its ID, it can be obtained |
|
| 246 |
/// using the corresponding \ref index() or \ref concepts::Digraph::id() |
|
| 247 |
/// "id()" function. A node or arc with a certain index can be obtained |
|
| 248 |
/// using node() or arc(). |
|
| 249 |
/// |
|
| 242 | 250 |
/// This type fully conforms to the \ref concepts::Digraph "Digraph concept". |
| 243 | 251 |
/// Most of its member functions and nested classes are documented |
| 244 | 252 |
/// only in the concept class. |
| 245 | 253 |
/// |
| 246 | 254 |
/// \sa concepts::Digraph |
| 247 | 255 |
class StaticDigraph : public ExtendedStaticDigraphBase {
|
| 248 | 256 |
public: |
| 249 | 257 |
|
| 250 | 258 |
typedef ExtendedStaticDigraphBase Parent; |
| 251 | 259 |
|
| 252 | 260 |
public: |
| 253 | 261 |
|
| 254 |
/// \brief |
|
| 262 |
/// \brief Constructor |
|
| 255 | 263 |
/// |
| 256 |
/// This function erases all nodes and arcs from the digraph. |
|
| 257 |
void clear() {
|
|
| 258 |
Parent::clear(); |
|
| 259 |
} |
|
| 264 |
/// Default constructor. |
|
| 265 |
StaticDigraph() : Parent() {}
|
|
| 266 |
|
|
| 267 |
/// \brief The node with the given index. |
|
| 268 |
/// |
|
| 269 |
/// This function returns the node with the given index. |
|
| 270 |
/// \sa index() |
|
| 271 |
Node node(int ix) const { return Parent::nodeFromId(ix); }
|
|
| 272 |
|
|
| 273 |
/// \brief The arc with the given index. |
|
| 274 |
/// |
|
| 275 |
/// This function returns the arc with the given index. |
|
| 276 |
/// \sa index() |
|
| 277 |
Arc arc(int ix) const { return Parent::arcFromId(ix); }
|
|
| 278 |
|
|
| 279 |
/// \brief The index of the given node. |
|
| 280 |
/// |
|
| 281 |
/// This function returns the index of the the given node. |
|
| 282 |
/// \sa node() |
|
| 283 |
int index(Node node) const { return Parent::id(node); }
|
|
| 284 |
|
|
| 285 |
/// \brief The index of the given arc. |
|
| 286 |
/// |
|
| 287 |
/// This function returns the index of the the given arc. |
|
| 288 |
/// \sa arc() |
|
| 289 |
int index(Arc arc) const { return Parent::id(arc); }
|
|
| 290 |
|
|
| 291 |
/// \brief Number of nodes. |
|
| 292 |
/// |
|
| 293 |
/// This function returns the number of nodes. |
|
| 294 |
int nodeNum() const { return node_num; }
|
|
| 295 |
|
|
| 296 |
/// \brief Number of arcs. |
|
| 297 |
/// |
|
| 298 |
/// This function returns the number of arcs. |
|
| 299 |
int arcNum() const { return arc_num; }
|
|
| 260 | 300 |
|
| 261 | 301 |
/// \brief Build the digraph copying another digraph. |
| 262 | 302 |
/// |
| 263 | 303 |
/// This function builds the digraph copying another digraph of any |
| 264 | 304 |
/// kind. It can be called more than once, but in such case, the whole |
| 265 | 305 |
/// structure and all maps will be cleared and rebuilt. |
| 266 | 306 |
/// |
| 267 | 307 |
/// This method also makes possible to copy a digraph to a StaticDigraph |
| 268 | 308 |
/// structure using \ref DigraphCopy. |
| 269 | 309 |
/// |
| 270 | 310 |
/// \param digraph An existing digraph to be copied. |
| 271 | 311 |
/// \param nodeRef The node references will be copied into this map. |
| 272 | 312 |
/// Its key type must be \c Digraph::Node and its value type must be |
| 273 | 313 |
/// \c StaticDigraph::Node. |
| 274 | 314 |
/// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" |
| 275 | 315 |
/// concept. |
| 276 | 316 |
/// \param arcRef The arc references will be copied into this map. |
| 277 | 317 |
/// Its key type must be \c Digraph::Arc and its value type must be |
| 278 | 318 |
/// \c StaticDigraph::Arc. |
| 279 | 319 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 280 | 320 |
/// |
| 281 | 321 |
/// \note If you do not need the arc references, then you could use |
| 282 | 322 |
/// \ref NullMap for the last parameter. However the node references |
| 283 | 323 |
/// are required by the function itself, thus they must be readable |
| 284 | 324 |
/// from the map. |
| 285 | 325 |
template <typename Digraph, typename NodeRefMap, typename ArcRefMap> |
| 286 | 326 |
void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
|
| 287 | 327 |
if (built) Parent::clear(); |
| 288 | 328 |
Parent::build(digraph, nodeRef, arcRef); |
| 289 | 329 |
} |
| 290 | 330 |
|
| 331 |
/// \brief Clear the digraph. |
|
| 332 |
/// |
|
| 333 |
/// This function erases all nodes and arcs from the digraph. |
|
| 334 |
void clear() {
|
|
| 335 |
Parent::clear(); |
|
| 336 |
} |
|
| 291 | 337 |
|
| 292 | 338 |
protected: |
| 293 | 339 |
|
| 294 | 340 |
using Parent::fastFirstOut; |
| 295 | 341 |
using Parent::fastNextOut; |
| 296 | 342 |
using Parent::fastLastOut; |
| 297 | 343 |
|
| 298 | 344 |
public: |
| 299 | 345 |
|
| 300 | 346 |
class OutArcIt : public Arc {
|
| 301 | 347 |
public: |
| 302 | 348 |
|
| 303 | 349 |
OutArcIt() { }
|
| 304 | 350 |
|
| 305 | 351 |
OutArcIt(Invalid i) : Arc(i) { }
|
| 306 | 352 |
|
| 307 | 353 |
OutArcIt(const StaticDigraph& digraph, const Node& node) {
|
| 308 | 354 |
digraph.fastFirstOut(*this, node); |
| 309 | 355 |
digraph.fastLastOut(last, node); |
| 310 | 356 |
if (last == *this) *this = INVALID; |
| 311 | 357 |
} |
| 312 | 358 |
|
| 313 | 359 |
OutArcIt(const StaticDigraph& digraph, const Arc& arc) : Arc(arc) {
|
| 314 | 360 |
if (arc != INVALID) {
|
| 315 | 361 |
digraph.fastLastOut(last, digraph.source(arc)); |
| 316 | 362 |
} |
| 317 | 363 |
} |
| 318 | 364 |
|
| 319 | 365 |
OutArcIt& operator++() {
|
| 320 | 366 |
StaticDigraph::fastNextOut(*this); |
| 321 | 367 |
if (last == *this) *this = INVALID; |
| 322 | 368 |
return *this; |
| 323 | 369 |
} |
| 324 | 370 |
|
| 325 | 371 |
private: |
| 326 | 372 |
Arc last; |
| 327 | 373 |
}; |
| 328 | 374 |
|
| 329 | 375 |
Node baseNode(const OutArcIt &arc) const {
|
| 330 | 376 |
return Parent::source(static_cast<const Arc&>(arc)); |
| 331 | 377 |
} |
| 332 | 378 |
|
| 333 | 379 |
Node runningNode(const OutArcIt &arc) const {
|
| 334 | 380 |
return Parent::target(static_cast<const Arc&>(arc)); |
| 335 | 381 |
} |
| 336 | 382 |
|
| 337 | 383 |
Node baseNode(const InArcIt &arc) const {
|
| 338 | 384 |
return Parent::target(static_cast<const Arc&>(arc)); |
| ... | ... |
@@ -400,96 +400,101 @@ |
| 400 | 400 |
G.build(g, nref, aref); |
| 401 | 401 |
|
| 402 | 402 |
checkGraphNodeList(G, 3); |
| 403 | 403 |
checkGraphArcList(G, 0); |
| 404 | 404 |
|
| 405 | 405 |
SmartDigraph::Arc a1 = g.addArc(n1, n2); |
| 406 | 406 |
|
| 407 | 407 |
G.build(g, nref, aref); |
| 408 | 408 |
|
| 409 | 409 |
check(G.source(aref[a1]) == nref[n1] && G.target(aref[a1]) == nref[n2], |
| 410 | 410 |
"Wrong arc or wrong references"); |
| 411 | 411 |
checkGraphNodeList(G, 3); |
| 412 | 412 |
checkGraphArcList(G, 1); |
| 413 | 413 |
|
| 414 | 414 |
checkGraphOutArcList(G, nref[n1], 1); |
| 415 | 415 |
checkGraphOutArcList(G, nref[n2], 0); |
| 416 | 416 |
checkGraphOutArcList(G, nref[n3], 0); |
| 417 | 417 |
|
| 418 | 418 |
checkGraphInArcList(G, nref[n1], 0); |
| 419 | 419 |
checkGraphInArcList(G, nref[n2], 1); |
| 420 | 420 |
checkGraphInArcList(G, nref[n3], 0); |
| 421 | 421 |
|
| 422 | 422 |
checkGraphConArcList(G, 1); |
| 423 | 423 |
|
| 424 | 424 |
SmartDigraph::Arc |
| 425 | 425 |
a2 = g.addArc(n2, n1), |
| 426 | 426 |
a3 = g.addArc(n2, n3), |
| 427 | 427 |
a4 = g.addArc(n2, n3); |
| 428 | 428 |
|
| 429 | 429 |
digraphCopy(g, G).nodeRef(nref).run(); |
| 430 | 430 |
|
| 431 | 431 |
checkGraphNodeList(G, 3); |
| 432 | 432 |
checkGraphArcList(G, 4); |
| 433 | 433 |
|
| 434 | 434 |
checkGraphOutArcList(G, nref[n1], 1); |
| 435 | 435 |
checkGraphOutArcList(G, nref[n2], 3); |
| 436 | 436 |
checkGraphOutArcList(G, nref[n3], 0); |
| 437 | 437 |
|
| 438 | 438 |
checkGraphInArcList(G, nref[n1], 1); |
| 439 | 439 |
checkGraphInArcList(G, nref[n2], 1); |
| 440 | 440 |
checkGraphInArcList(G, nref[n3], 2); |
| 441 | 441 |
|
| 442 | 442 |
checkGraphConArcList(G, 4); |
| 443 | 443 |
|
| 444 | 444 |
checkNodeIds(G); |
| 445 | 445 |
checkArcIds(G); |
| 446 | 446 |
checkGraphNodeMap(G); |
| 447 | 447 |
checkGraphArcMap(G); |
| 448 |
|
|
| 449 |
int n = G.nodeNum(); |
|
| 450 |
int m = G.arcNum(); |
|
| 451 |
check(G.index(G.node(n-1)) == n-1, "Wrong index."); |
|
| 452 |
check(G.index(G.arc(m-1)) == m-1, "Wrong index."); |
|
| 448 | 453 |
} |
| 449 | 454 |
|
| 450 | 455 |
void checkFullDigraph(int num) {
|
| 451 | 456 |
typedef FullDigraph Digraph; |
| 452 | 457 |
DIGRAPH_TYPEDEFS(Digraph); |
| 453 | 458 |
Digraph G(num); |
| 454 | 459 |
|
| 455 | 460 |
checkGraphNodeList(G, num); |
| 456 | 461 |
checkGraphArcList(G, num * num); |
| 457 | 462 |
|
| 458 | 463 |
for (NodeIt n(G); n != INVALID; ++n) {
|
| 459 | 464 |
checkGraphOutArcList(G, n, num); |
| 460 | 465 |
checkGraphInArcList(G, n, num); |
| 461 | 466 |
} |
| 462 | 467 |
|
| 463 | 468 |
checkGraphConArcList(G, num * num); |
| 464 | 469 |
|
| 465 | 470 |
checkNodeIds(G); |
| 466 | 471 |
checkArcIds(G); |
| 467 | 472 |
checkGraphNodeMap(G); |
| 468 | 473 |
checkGraphArcMap(G); |
| 469 | 474 |
|
| 470 | 475 |
for (int i = 0; i < G.nodeNum(); ++i) {
|
| 471 | 476 |
check(G.index(G(i)) == i, "Wrong index"); |
| 472 | 477 |
} |
| 473 | 478 |
|
| 474 | 479 |
for (NodeIt s(G); s != INVALID; ++s) {
|
| 475 | 480 |
for (NodeIt t(G); t != INVALID; ++t) {
|
| 476 | 481 |
Arc a = G.arc(s, t); |
| 477 | 482 |
check(G.source(a) == s && G.target(a) == t, "Wrong arc lookup"); |
| 478 | 483 |
} |
| 479 | 484 |
} |
| 480 | 485 |
} |
| 481 | 486 |
|
| 482 | 487 |
void checkDigraphs() {
|
| 483 | 488 |
{ // Checking ListDigraph
|
| 484 | 489 |
checkDigraphBuild<ListDigraph>(); |
| 485 | 490 |
checkDigraphSplit<ListDigraph>(); |
| 486 | 491 |
checkDigraphAlter<ListDigraph>(); |
| 487 | 492 |
checkDigraphErase<ListDigraph>(); |
| 488 | 493 |
checkDigraphSnapshot<ListDigraph>(); |
| 489 | 494 |
checkDigraphValidityErase<ListDigraph>(); |
| 490 | 495 |
} |
| 491 | 496 |
{ // Checking SmartDigraph
|
| 492 | 497 |
checkDigraphBuild<SmartDigraph>(); |
| 493 | 498 |
checkDigraphSplit<SmartDigraph>(); |
| 494 | 499 |
checkDigraphSnapshot<SmartDigraph>(); |
| 495 | 500 |
checkDigraphValidity<SmartDigraph>(); |
0 comments (0 inline)