gravatar
deba@inf.elte.hu
deba@inf.elte.hu
Two bug fixes in DynArcLookUp
0 1 0
default
1 file changed with 3 insertions and 0 deletions:
↑ Collapse diff ↑
Ignore white space 512 line context
... ...
@@ -1131,676 +1131,679 @@
1131 1131
  ///\endcode
1132 1132
  ///
1133 1133
  ///\sa findEdge()
1134 1134
  template <typename _Graph>
1135 1135
  class ConEdgeIt : public _Graph::Edge {
1136 1136
  public:
1137 1137

	
1138 1138
    typedef _Graph Graph;
1139 1139
    typedef typename Graph::Edge Parent;
1140 1140

	
1141 1141
    typedef typename Graph::Edge Edge;
1142 1142
    typedef typename Graph::Node Node;
1143 1143

	
1144 1144
    /// \brief Constructor.
1145 1145
    ///
1146 1146
    /// Construct a new ConEdgeIt iterating on the edges which
1147 1147
    /// connects the \c u and \c v node.
1148 1148
    ConEdgeIt(const Graph& g, Node u, Node v) : _graph(g) {
1149 1149
      Parent::operator=(findEdge(_graph, u, v));
1150 1150
    }
1151 1151

	
1152 1152
    /// \brief Constructor.
1153 1153
    ///
1154 1154
    /// Construct a new ConEdgeIt which continues the iterating from
1155 1155
    /// the \c e edge.
1156 1156
    ConEdgeIt(const Graph& g, Edge e) : Parent(e), _graph(g) {}
1157 1157

	
1158 1158
    /// \brief Increment operator.
1159 1159
    ///
1160 1160
    /// It increments the iterator and gives back the next edge.
1161 1161
    ConEdgeIt& operator++() {
1162 1162
      Parent::operator=(findEdge(_graph, _graph.u(*this),
1163 1163
                                 _graph.v(*this), *this));
1164 1164
      return *this;
1165 1165
    }
1166 1166
  private:
1167 1167
    const Graph& _graph;
1168 1168
  };
1169 1169

	
1170 1170

	
1171 1171
  ///Dynamic arc look up between given endpoints.
1172 1172

	
1173 1173
  ///Using this class, you can find an arc in a digraph from a given
1174 1174
  ///source to a given target in amortized time <em>O(log d)</em>,
1175 1175
  ///where <em>d</em> is the out-degree of the source node.
1176 1176
  ///
1177 1177
  ///It is possible to find \e all parallel arcs between two nodes with
1178 1178
  ///the \c findFirst() and \c findNext() members.
1179 1179
  ///
1180 1180
  ///See the \ref ArcLookUp and \ref AllArcLookUp classes if your
1181 1181
  ///digraph is not changed so frequently.
1182 1182
  ///
1183 1183
  ///This class uses a self-adjusting binary search tree, Sleator's
1184 1184
  ///and Tarjan's Splay tree for guarantee the logarithmic amortized
1185 1185
  ///time bound for arc lookups. This class also guarantees the
1186 1186
  ///optimal time bound in a constant factor for any distribution of
1187 1187
  ///queries.
1188 1188
  ///
1189 1189
  ///\tparam G The type of the underlying digraph.
1190 1190
  ///
1191 1191
  ///\sa ArcLookUp
1192 1192
  ///\sa AllArcLookUp
1193 1193
  template<class G>
1194 1194
  class DynArcLookUp
1195 1195
    : protected ItemSetTraits<G, typename G::Arc>::ItemNotifier::ObserverBase
1196 1196
  {
1197 1197
  public:
1198 1198
    typedef typename ItemSetTraits<G, typename G::Arc>
1199 1199
    ::ItemNotifier::ObserverBase Parent;
1200 1200

	
1201 1201
    TEMPLATE_DIGRAPH_TYPEDEFS(G);
1202 1202
    typedef G Digraph;
1203 1203

	
1204 1204
  protected:
1205 1205

	
1206 1206
    class AutoNodeMap : public ItemSetTraits<G, Node>::template Map<Arc>::Type {
1207 1207
    public:
1208 1208

	
1209 1209
      typedef typename ItemSetTraits<G, Node>::template Map<Arc>::Type Parent;
1210 1210

	
1211 1211
      AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {}
1212 1212

	
1213 1213
      virtual void add(const Node& node) {
1214 1214
        Parent::add(node);
1215 1215
        Parent::set(node, INVALID);
1216 1216
      }
1217 1217

	
1218 1218
      virtual void add(const std::vector<Node>& nodes) {
1219 1219
        Parent::add(nodes);
1220 1220
        for (int i = 0; i < int(nodes.size()); ++i) {
1221 1221
          Parent::set(nodes[i], INVALID);
1222 1222
        }
1223 1223
      }
1224 1224

	
1225 1225
      virtual void build() {
1226 1226
        Parent::build();
1227 1227
        Node it;
1228 1228
        typename Parent::Notifier* nf = Parent::notifier();
1229 1229
        for (nf->first(it); it != INVALID; nf->next(it)) {
1230 1230
          Parent::set(it, INVALID);
1231 1231
        }
1232 1232
      }
1233 1233
    };
1234 1234

	
1235 1235
    const Digraph &_g;
1236 1236
    AutoNodeMap _head;
1237 1237
    typename Digraph::template ArcMap<Arc> _parent;
1238 1238
    typename Digraph::template ArcMap<Arc> _left;
1239 1239
    typename Digraph::template ArcMap<Arc> _right;
1240 1240

	
1241 1241
    class ArcLess {
1242 1242
      const Digraph &g;
1243 1243
    public:
1244 1244
      ArcLess(const Digraph &_g) : g(_g) {}
1245 1245
      bool operator()(Arc a,Arc b) const
1246 1246
      {
1247 1247
        return g.target(a)<g.target(b);
1248 1248
      }
1249 1249
    };
1250 1250

	
1251 1251
  public:
1252 1252

	
1253 1253
    ///Constructor
1254 1254

	
1255 1255
    ///Constructor.
1256 1256
    ///
1257 1257
    ///It builds up the search database.
1258 1258
    DynArcLookUp(const Digraph &g)
1259 1259
      : _g(g),_head(g),_parent(g),_left(g),_right(g)
1260 1260
    {
1261 1261
      Parent::attach(_g.notifier(typename Digraph::Arc()));
1262 1262
      refresh();
1263 1263
    }
1264 1264

	
1265 1265
  protected:
1266 1266

	
1267 1267
    virtual void add(const Arc& arc) {
1268 1268
      insert(arc);
1269 1269
    }
1270 1270

	
1271 1271
    virtual void add(const std::vector<Arc>& arcs) {
1272 1272
      for (int i = 0; i < int(arcs.size()); ++i) {
1273 1273
        insert(arcs[i]);
1274 1274
      }
1275 1275
    }
1276 1276

	
1277 1277
    virtual void erase(const Arc& arc) {
1278 1278
      remove(arc);
1279 1279
    }
1280 1280

	
1281 1281
    virtual void erase(const std::vector<Arc>& arcs) {
1282 1282
      for (int i = 0; i < int(arcs.size()); ++i) {
1283 1283
        remove(arcs[i]);
1284 1284
      }
1285 1285
    }
1286 1286

	
1287 1287
    virtual void build() {
1288 1288
      refresh();
1289 1289
    }
1290 1290

	
1291 1291
    virtual void clear() {
1292 1292
      for(NodeIt n(_g);n!=INVALID;++n) {
1293 1293
        _head.set(n, INVALID);
1294 1294
      }
1295 1295
    }
1296 1296

	
1297 1297
    void insert(Arc arc) {
1298 1298
      Node s = _g.source(arc);
1299 1299
      Node t = _g.target(arc);
1300 1300
      _left.set(arc, INVALID);
1301 1301
      _right.set(arc, INVALID);
1302 1302

	
1303 1303
      Arc e = _head[s];
1304 1304
      if (e == INVALID) {
1305 1305
        _head.set(s, arc);
1306 1306
        _parent.set(arc, INVALID);
1307 1307
        return;
1308 1308
      }
1309 1309
      while (true) {
1310 1310
        if (t < _g.target(e)) {
1311 1311
          if (_left[e] == INVALID) {
1312 1312
            _left.set(e, arc);
1313 1313
            _parent.set(arc, e);
1314 1314
            splay(arc);
1315 1315
            return;
1316 1316
          } else {
1317 1317
            e = _left[e];
1318 1318
          }
1319 1319
        } else {
1320 1320
          if (_right[e] == INVALID) {
1321 1321
            _right.set(e, arc);
1322 1322
            _parent.set(arc, e);
1323 1323
            splay(arc);
1324 1324
            return;
1325 1325
          } else {
1326 1326
            e = _right[e];
1327 1327
          }
1328 1328
        }
1329 1329
      }
1330 1330
    }
1331 1331

	
1332 1332
    void remove(Arc arc) {
1333 1333
      if (_left[arc] == INVALID) {
1334 1334
        if (_right[arc] != INVALID) {
1335 1335
          _parent.set(_right[arc], _parent[arc]);
1336 1336
        }
1337 1337
        if (_parent[arc] != INVALID) {
1338 1338
          if (_left[_parent[arc]] == arc) {
1339 1339
            _left.set(_parent[arc], _right[arc]);
1340 1340
          } else {
1341 1341
            _right.set(_parent[arc], _right[arc]);
1342 1342
          }
1343 1343
        } else {
1344 1344
          _head.set(_g.source(arc), _right[arc]);
1345 1345
        }
1346 1346
      } else if (_right[arc] == INVALID) {
1347 1347
        _parent.set(_left[arc], _parent[arc]);
1348 1348
        if (_parent[arc] != INVALID) {
1349 1349
          if (_left[_parent[arc]] == arc) {
1350 1350
            _left.set(_parent[arc], _left[arc]);
1351 1351
          } else {
1352 1352
            _right.set(_parent[arc], _left[arc]);
1353 1353
          }
1354 1354
        } else {
1355 1355
          _head.set(_g.source(arc), _left[arc]);
1356 1356
        }
1357 1357
      } else {
1358 1358
        Arc e = _left[arc];
1359 1359
        if (_right[e] != INVALID) {
1360 1360
          e = _right[e];
1361 1361
          while (_right[e] != INVALID) {
1362 1362
            e = _right[e];
1363 1363
          }
1364 1364
          Arc s = _parent[e];
1365 1365
          _right.set(_parent[e], _left[e]);
1366 1366
          if (_left[e] != INVALID) {
1367 1367
            _parent.set(_left[e], _parent[e]);
1368 1368
          }
1369 1369

	
1370 1370
          _left.set(e, _left[arc]);
1371 1371
          _parent.set(_left[arc], e);
1372 1372
          _right.set(e, _right[arc]);
1373 1373
          _parent.set(_right[arc], e);
1374 1374

	
1375 1375
          _parent.set(e, _parent[arc]);
1376 1376
          if (_parent[arc] != INVALID) {
1377 1377
            if (_left[_parent[arc]] == arc) {
1378 1378
              _left.set(_parent[arc], e);
1379 1379
            } else {
1380 1380
              _right.set(_parent[arc], e);
1381 1381
            }
1382 1382
          }
1383 1383
          splay(s);
1384 1384
        } else {
1385 1385
          _right.set(e, _right[arc]);
1386 1386
          _parent.set(_right[arc], e);
1387
          _parent.set(e, _parent[arc]);
1387 1388

	
1388 1389
          if (_parent[arc] != INVALID) {
1389 1390
            if (_left[_parent[arc]] == arc) {
1390 1391
              _left.set(_parent[arc], e);
1391 1392
            } else {
1392 1393
              _right.set(_parent[arc], e);
1393 1394
            }
1394 1395
          } else {
1395 1396
            _head.set(_g.source(arc), e);
1396 1397
          }
1397 1398
        }
1398 1399
      }
1399 1400
    }
1400 1401

	
1401 1402
    Arc refreshRec(std::vector<Arc> &v,int a,int b)
1402 1403
    {
1403 1404
      int m=(a+b)/2;
1404 1405
      Arc me=v[m];
1405 1406
      if (a < m) {
1406 1407
        Arc left = refreshRec(v,a,m-1);
1407 1408
        _left.set(me, left);
1408 1409
        _parent.set(left, me);
1409 1410
      } else {
1410 1411
        _left.set(me, INVALID);
1411 1412
      }
1412 1413
      if (m < b) {
1413 1414
        Arc right = refreshRec(v,m+1,b);
1414 1415
        _right.set(me, right);
1415 1416
        _parent.set(right, me);
1416 1417
      } else {
1417 1418
        _right.set(me, INVALID);
1418 1419
      }
1419 1420
      return me;
1420 1421
    }
1421 1422

	
1422 1423
    void refresh() {
1423 1424
      for(NodeIt n(_g);n!=INVALID;++n) {
1424 1425
        std::vector<Arc> v;
1425 1426
        for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
1426 1427
        if(v.size()) {
1427 1428
          std::sort(v.begin(),v.end(),ArcLess(_g));
1428 1429
          Arc head = refreshRec(v,0,v.size()-1);
1429 1430
          _head.set(n, head);
1430 1431
          _parent.set(head, INVALID);
1431 1432
        }
1432 1433
        else _head.set(n, INVALID);
1433 1434
      }
1434 1435
    }
1435 1436

	
1436 1437
    void zig(Arc v) {
1437 1438
      Arc w = _parent[v];
1438 1439
      _parent.set(v, _parent[w]);
1439 1440
      _parent.set(w, v);
1440 1441
      _left.set(w, _right[v]);
1441 1442
      _right.set(v, w);
1442 1443
      if (_parent[v] != INVALID) {
1443 1444
        if (_right[_parent[v]] == w) {
1444 1445
          _right.set(_parent[v], v);
1445 1446
        } else {
1446 1447
          _left.set(_parent[v], v);
1447 1448
        }
1448 1449
      }
1449 1450
      if (_left[w] != INVALID){
1450 1451
        _parent.set(_left[w], w);
1451 1452
      }
1452 1453
    }
1453 1454

	
1454 1455
    void zag(Arc v) {
1455 1456
      Arc w = _parent[v];
1456 1457
      _parent.set(v, _parent[w]);
1457 1458
      _parent.set(w, v);
1458 1459
      _right.set(w, _left[v]);
1459 1460
      _left.set(v, w);
1460 1461
      if (_parent[v] != INVALID){
1461 1462
        if (_left[_parent[v]] == w) {
1462 1463
          _left.set(_parent[v], v);
1463 1464
        } else {
1464 1465
          _right.set(_parent[v], v);
1465 1466
        }
1466 1467
      }
1467 1468
      if (_right[w] != INVALID){
1468 1469
        _parent.set(_right[w], w);
1469 1470
      }
1470 1471
    }
1471 1472

	
1472 1473
    void splay(Arc v) {
1473 1474
      while (_parent[v] != INVALID) {
1474 1475
        if (v == _left[_parent[v]]) {
1475 1476
          if (_parent[_parent[v]] == INVALID) {
1476 1477
            zig(v);
1477 1478
          } else {
1478 1479
            if (_parent[v] == _left[_parent[_parent[v]]]) {
1479 1480
              zig(_parent[v]);
1480 1481
              zig(v);
1481 1482
            } else {
1482 1483
              zig(v);
1483 1484
              zag(v);
1484 1485
            }
1485 1486
          }
1486 1487
        } else {
1487 1488
          if (_parent[_parent[v]] == INVALID) {
1488 1489
            zag(v);
1489 1490
          } else {
1490 1491
            if (_parent[v] == _left[_parent[_parent[v]]]) {
1491 1492
              zag(v);
1492 1493
              zig(v);
1493 1494
            } else {
1494 1495
              zag(_parent[v]);
1495 1496
              zag(v);
1496 1497
            }
1497 1498
          }
1498 1499
        }
1499 1500
      }
1500 1501
      _head[_g.source(v)] = v;
1501 1502
    }
1502 1503

	
1503 1504

	
1504 1505
  public:
1505 1506

	
1506 1507
    ///Find an arc between two nodes.
1507 1508

	
1508 1509
    ///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where
1509 1510
    /// <em>d</em> is the number of outgoing arcs of \c s.
1510 1511
    ///\param s The source node
1511 1512
    ///\param t The target node
1512 1513
    ///\return An arc from \c s to \c t if there exists,
1513 1514
    ///\ref INVALID otherwise.
1514 1515
    Arc operator()(Node s, Node t) const
1515 1516
    {
1516 1517
      Arc a = _head[s];
1518
      if (a == INVALID) return INVALID;
1517 1519
      while (true) {
1518 1520
        if (_g.target(a) == t) {
1519 1521
          const_cast<DynArcLookUp&>(*this).splay(a);
1520 1522
          return a;
1521 1523
        } else if (t < _g.target(a)) {
1522 1524
          if (_left[a] == INVALID) {
1523 1525
            const_cast<DynArcLookUp&>(*this).splay(a);
1524 1526
            return INVALID;
1525 1527
          } else {
1526 1528
            a = _left[a];
1527 1529
          }
1528 1530
        } else  {
1529 1531
          if (_right[a] == INVALID) {
1530 1532
            const_cast<DynArcLookUp&>(*this).splay(a);
1531 1533
            return INVALID;
1532 1534
          } else {
1533 1535
            a = _right[a];
1534 1536
          }
1535 1537
        }
1536 1538
      }
1537 1539
    }
1538 1540

	
1539 1541
    ///Find the first arc between two nodes.
1540 1542

	
1541 1543
    ///Find the first arc between two nodes in time
1542 1544
    /// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of
1543 1545
    /// outgoing arcs of \c s.
1544 1546
    ///\param s The source node
1545 1547
    ///\param t The target node
1546 1548
    ///\return An arc from \c s to \c t if there exists, \ref INVALID
1547 1549
    /// otherwise.
1548 1550
    Arc findFirst(Node s, Node t) const
1549 1551
    {
1550 1552
      Arc a = _head[s];
1553
      if (a == INVALID) return INVALID;
1551 1554
      Arc r = INVALID;
1552 1555
      while (true) {
1553 1556
        if (_g.target(a) < t) {
1554 1557
          if (_right[a] == INVALID) {
1555 1558
            const_cast<DynArcLookUp&>(*this).splay(a);
1556 1559
            return r;
1557 1560
          } else {
1558 1561
            a = _right[a];
1559 1562
          }
1560 1563
        } else {
1561 1564
          if (_g.target(a) == t) {
1562 1565
            r = a;
1563 1566
          }
1564 1567
          if (_left[a] == INVALID) {
1565 1568
            const_cast<DynArcLookUp&>(*this).splay(a);
1566 1569
            return r;
1567 1570
          } else {
1568 1571
            a = _left[a];
1569 1572
          }
1570 1573
        }
1571 1574
      }
1572 1575
    }
1573 1576

	
1574 1577
    ///Find the next arc between two nodes.
1575 1578

	
1576 1579
    ///Find the next arc between two nodes in time
1577 1580
    /// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of
1578 1581
    /// outgoing arcs of \c s.
1579 1582
    ///\param s The source node
1580 1583
    ///\param t The target node
1581 1584
    ///\return An arc from \c s to \c t if there exists, \ref INVALID
1582 1585
    /// otherwise.
1583 1586

	
1584 1587
    ///\note If \c e is not the result of the previous \c findFirst()
1585 1588
    ///operation then the amorized time bound can not be guaranteed.
1586 1589
#ifdef DOXYGEN
1587 1590
    Arc findNext(Node s, Node t, Arc a) const
1588 1591
#else
1589 1592
    Arc findNext(Node, Node t, Arc a) const
1590 1593
#endif
1591 1594
    {
1592 1595
      if (_right[a] != INVALID) {
1593 1596
        a = _right[a];
1594 1597
        while (_left[a] != INVALID) {
1595 1598
          a = _left[a];
1596 1599
        }
1597 1600
        const_cast<DynArcLookUp&>(*this).splay(a);
1598 1601
      } else {
1599 1602
        while (_parent[a] != INVALID && _right[_parent[a]] ==  a) {
1600 1603
          a = _parent[a];
1601 1604
        }
1602 1605
        if (_parent[a] == INVALID) {
1603 1606
          return INVALID;
1604 1607
        } else {
1605 1608
          a = _parent[a];
1606 1609
          const_cast<DynArcLookUp&>(*this).splay(a);
1607 1610
        }
1608 1611
      }
1609 1612
      if (_g.target(a) == t) return a;
1610 1613
      else return INVALID;
1611 1614
    }
1612 1615

	
1613 1616
  };
1614 1617

	
1615 1618
  ///Fast arc look up between given endpoints.
1616 1619

	
1617 1620
  ///Using this class, you can find an arc in a digraph from a given
1618 1621
  ///source to a given target in time <em>O(log d)</em>,
1619 1622
  ///where <em>d</em> is the out-degree of the source node.
1620 1623
  ///
1621 1624
  ///It is not possible to find \e all parallel arcs between two nodes.
1622 1625
  ///Use \ref AllArcLookUp for this purpose.
1623 1626
  ///
1624 1627
  ///\warning This class is static, so you should refresh() (or at least
1625 1628
  ///refresh(Node)) this data structure
1626 1629
  ///whenever the digraph changes. This is a time consuming (superlinearly
1627 1630
  ///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs).
1628 1631
  ///
1629 1632
  ///\tparam G The type of the underlying digraph.
1630 1633
  ///
1631 1634
  ///\sa DynArcLookUp
1632 1635
  ///\sa AllArcLookUp
1633 1636
  template<class G>
1634 1637
  class ArcLookUp
1635 1638
  {
1636 1639
  public:
1637 1640
    TEMPLATE_DIGRAPH_TYPEDEFS(G);
1638 1641
    typedef G Digraph;
1639 1642

	
1640 1643
  protected:
1641 1644
    const Digraph &_g;
1642 1645
    typename Digraph::template NodeMap<Arc> _head;
1643 1646
    typename Digraph::template ArcMap<Arc> _left;
1644 1647
    typename Digraph::template ArcMap<Arc> _right;
1645 1648

	
1646 1649
    class ArcLess {
1647 1650
      const Digraph &g;
1648 1651
    public:
1649 1652
      ArcLess(const Digraph &_g) : g(_g) {}
1650 1653
      bool operator()(Arc a,Arc b) const
1651 1654
      {
1652 1655
        return g.target(a)<g.target(b);
1653 1656
      }
1654 1657
    };
1655 1658

	
1656 1659
  public:
1657 1660

	
1658 1661
    ///Constructor
1659 1662

	
1660 1663
    ///Constructor.
1661 1664
    ///
1662 1665
    ///It builds up the search database, which remains valid until the digraph
1663 1666
    ///changes.
1664 1667
    ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
1665 1668

	
1666 1669
  private:
1667 1670
    Arc refreshRec(std::vector<Arc> &v,int a,int b)
1668 1671
    {
1669 1672
      int m=(a+b)/2;
1670 1673
      Arc me=v[m];
1671 1674
      _left[me] = a<m?refreshRec(v,a,m-1):INVALID;
1672 1675
      _right[me] = m<b?refreshRec(v,m+1,b):INVALID;
1673 1676
      return me;
1674 1677
    }
1675 1678
  public:
1676 1679
    ///Refresh the data structure at a node.
1677 1680

	
1678 1681
    ///Build up the search database of node \c n.
1679 1682
    ///
1680 1683
    ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
1681 1684
    ///the number of the outgoing arcs of \c n.
1682 1685
    void refresh(Node n)
1683 1686
    {
1684 1687
      std::vector<Arc> v;
1685 1688
      for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
1686 1689
      if(v.size()) {
1687 1690
        std::sort(v.begin(),v.end(),ArcLess(_g));
1688 1691
        _head[n]=refreshRec(v,0,v.size()-1);
1689 1692
      }
1690 1693
      else _head[n]=INVALID;
1691 1694
    }
1692 1695
    ///Refresh the full data structure.
1693 1696

	
1694 1697
    ///Build up the full search database. In fact, it simply calls
1695 1698
    ///\ref refresh(Node) "refresh(n)" for each node \c n.
1696 1699
    ///
1697 1700
    ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
1698 1701
    ///the number of the arcs of \c n and <em>D</em> is the maximum
1699 1702
    ///out-degree of the digraph.
1700 1703

	
1701 1704
    void refresh()
1702 1705
    {
1703 1706
      for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
1704 1707
    }
1705 1708

	
1706 1709
    ///Find an arc between two nodes.
1707 1710

	
1708 1711
    ///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where
1709 1712
    /// <em>d</em> is the number of outgoing arcs of \c s.
1710 1713
    ///\param s The source node
1711 1714
    ///\param t The target node
1712 1715
    ///\return An arc from \c s to \c t if there exists,
1713 1716
    ///\ref INVALID otherwise.
1714 1717
    ///
1715 1718
    ///\warning If you change the digraph, refresh() must be called before using
1716 1719
    ///this operator. If you change the outgoing arcs of
1717 1720
    ///a single node \c n, then
1718 1721
    ///\ref refresh(Node) "refresh(n)" is enough.
1719 1722
    ///
1720 1723
    Arc operator()(Node s, Node t) const
1721 1724
    {
1722 1725
      Arc e;
1723 1726
      for(e=_head[s];
1724 1727
          e!=INVALID&&_g.target(e)!=t;
1725 1728
          e = t < _g.target(e)?_left[e]:_right[e]) ;
1726 1729
      return e;
1727 1730
    }
1728 1731

	
1729 1732
  };
1730 1733

	
1731 1734
  ///Fast look up of all arcs between given endpoints.
1732 1735

	
1733 1736
  ///This class is the same as \ref ArcLookUp, with the addition
1734 1737
  ///that it makes it possible to find all arcs between given endpoints.
1735 1738
  ///
1736 1739
  ///\warning This class is static, so you should refresh() (or at least
1737 1740
  ///refresh(Node)) this data structure
1738 1741
  ///whenever the digraph changes. This is a time consuming (superlinearly
1739 1742
  ///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs).
1740 1743
  ///
1741 1744
  ///\tparam G The type of the underlying digraph.
1742 1745
  ///
1743 1746
  ///\sa DynArcLookUp
1744 1747
  ///\sa ArcLookUp
1745 1748
  template<class G>
1746 1749
  class AllArcLookUp : public ArcLookUp<G>
1747 1750
  {
1748 1751
    using ArcLookUp<G>::_g;
1749 1752
    using ArcLookUp<G>::_right;
1750 1753
    using ArcLookUp<G>::_left;
1751 1754
    using ArcLookUp<G>::_head;
1752 1755

	
1753 1756
    TEMPLATE_DIGRAPH_TYPEDEFS(G);
1754 1757
    typedef G Digraph;
1755 1758

	
1756 1759
    typename Digraph::template ArcMap<Arc> _next;
1757 1760

	
1758 1761
    Arc refreshNext(Arc head,Arc next=INVALID)
1759 1762
    {
1760 1763
      if(head==INVALID) return next;
1761 1764
      else {
1762 1765
        next=refreshNext(_right[head],next);
1763 1766
//         _next[head]=next;
1764 1767
        _next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
1765 1768
          ? next : INVALID;
1766 1769
        return refreshNext(_left[head],head);
1767 1770
      }
1768 1771
    }
1769 1772

	
1770 1773
    void refreshNext()
1771 1774
    {
1772 1775
      for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
1773 1776
    }
1774 1777

	
1775 1778
  public:
1776 1779
    ///Constructor
1777 1780

	
1778 1781
    ///Constructor.
1779 1782
    ///
1780 1783
    ///It builds up the search database, which remains valid until the digraph
1781 1784
    ///changes.
1782 1785
    AllArcLookUp(const Digraph &g) : ArcLookUp<G>(g), _next(g) {refreshNext();}
1783 1786

	
1784 1787
    ///Refresh the data structure at a node.
1785 1788

	
1786 1789
    ///Build up the search database of node \c n.
1787 1790
    ///
1788 1791
    ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
1789 1792
    ///the number of the outgoing arcs of \c n.
1790 1793

	
1791 1794
    void refresh(Node n)
1792 1795
    {
1793 1796
      ArcLookUp<G>::refresh(n);
1794 1797
      refreshNext(_head[n]);
1795 1798
    }
1796 1799

	
1797 1800
    ///Refresh the full data structure.
1798 1801

	
1799 1802
    ///Build up the full search database. In fact, it simply calls
1800 1803
    ///\ref refresh(Node) "refresh(n)" for each node \c n.
1801 1804
    ///
1802 1805
    ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
1803 1806
    ///the number of the arcs of \c n and <em>D</em> is the maximum
1804 1807
    ///out-degree of the digraph.
1805 1808

	
1806 1809
    void refresh()
0 comments (0 inline)