BALL  1.4.79
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
composite.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=2:
3 //
4 
5 #ifndef BALL_CONCEPT_COMPOSITE_H
6 #define BALL_CONCEPT_COMPOSITE_H
7 
8 #ifndef BALL_COMMON_H
9 # include <BALL/common.h>
10 #endif
11 
12 #ifndef BALL_CONCEPT_PERSISTENTOBJECT_H
14 #endif
15 
16 #ifndef BALL_CONCEPT_COMPARATOR_H
17 # include <BALL/CONCEPT/comparator.h>
18 #endif
19 
20 #ifndef BALL_CONCEPT_BIDIRECTIONALITERATOR_H
22 #endif
23 
24 #ifndef BALL_CONCEPT_OBJECT_H
25 # include <BALL/CONCEPT/object.h>
26 #endif
27 
28 #ifndef BALL_CONCEPT_SELECTABLE_H
29 # include <BALL/CONCEPT/selectable.h>
30 #endif
31 
32 #ifndef BALL_CONCEPT_VISITOR_H
33 # include <BALL/CONCEPT/visitor.h>
34 #endif
35 
36 #ifndef BALL_CONCEPT_PROCESSOR_H
37 # include <BALL/CONCEPT/processor.h>
38 #endif
39 
40 #ifndef BALL_CONCEPT_TIMESTAMP_H
41 # include <BALL/CONCEPT/timeStamp.h>
42 #endif
43 
45 namespace BALL
46 {
47  class Atom;
48 
72  : public PersistentObject,
73  public Selectable
74  {
75  public:
76 
80 
81 #ifndef BALL_KERNEL_PREDICATE_TYPE
82 #define BALL_KERNEL_PREDICATE_TYPE
83 
89 #endif
90 
93  enum StampType
94  {
97  MODIFICATION = 1,
100  SELECTION = 2,
103  BOTH = 3
104  };
106 
108 
109  static UnaryProcessor<Composite> DEFAULT_PROCESSOR;
110  static KernelPredicateType DEFAULT_UNARY_PREDICATE;
111 
115 
119  Composite()
120  ;
121 
130  Composite(const Composite& composite, bool deep = true)
131  ;
132 
138  virtual ~Composite()
139  ;
140 
152  virtual void clear()
153  ;
154 
164  virtual void destroy()
165  ;
166 
179  void destroy(bool virtual_destroy)
180  ;
181 
189  void* clone(Composite& root) const
190  ;
191 
193 
197 
203  virtual void persistentWrite(PersistenceManager& pm, const char* name = 0) const;
204 
209  virtual void persistentRead(PersistenceManager& pm);
210 
212 
216 
222  void set(const Composite& composite, bool deep = true) ;
223 
228  Composite& operator = (const Composite& composite) ;
229 
236  void get(Composite& composite, bool deep = true) const ;
237 
242  Size getDegree() const ;
243 
248  Size count(const KernelPredicateType& predicate) const ;
249 
253  Size countDescendants() const ;
254 
260  Size getPathLength(const Composite& composite) const ;
261 
266  Size getDepth() const ;
267 
272  Size getHeight() const
273  ;
274 
278  Composite& getRoot() ;
279 
283  const Composite& getRoot() const ;
284 
289  Composite* getLowestCommonAncestor(const Composite& composite)
290  ;
291 
296  const Composite* getLowestCommonAncestor(const Composite& composite) const
297  ;
298 
307  template <typename T>
308  T* getAncestor(const T& /* dummy */)
309  ;
310 
317  template <typename T>
318  const T* getAncestor(const T& /* dummy */) const ;
319 
327  template <typename T>
328  T* getPrevious(const T& /* dummy */) ;
329 
337  template <typename T>
338  const T* getPrevious(const T& dummy) const ;
339 
347  template <typename T>
348  T* getNext(const T& /* dummy */) ;
349 
357  template <typename T>
358  const T* getNext(const T& dummy) const ;
359 
363  Composite* getParent() ;
364 
368  const Composite* getParent() const ;
369 
376  Composite* getChild(Index index) ;
377 
384  const Composite* getChild(Index index) const ;
385 
394  Composite* getSibling(Index index) ;
395 
404  const Composite* getSibling(Index index) const ;
405 
409  Composite* getFirstChild() ;
410 
414  const Composite* getFirstChild() const ;
415 
419  Composite* getLastChild() ;
420 
424  const Composite* getLastChild() const ;
425 
429  const PreciseTime& getModificationTime() const ;
430 
434  const PreciseTime& getSelectionTime() const ;
435 
445  void stamp(StampType stamp = BOTH) ;
446 
452  void prependChild(Composite& composite) ;
453 
461  void appendChild(Composite& composite) ;
462 
482  static bool insertParent(Composite& parent, Composite& first,
483  Composite& last, bool destroy_parent = true)
484  ;
485 
495  void insertBefore(Composite& composite) ;
496 
506  void insertAfter(Composite& composite) ;
507 
516  void spliceBefore(Composite& composite) ;
517 
526  void spliceAfter(Composite& composite) ;
527 
537  void splice(Composite& composite) ;
538 
547  bool removeChild(Composite& child) ;
548 
549 
562  Size removeSelected() ;
563 
576  Size removeUnselected();
577 
586  void replace(Composite& composite) ;
587 
596  void swap(Composite& composite) ;
597 
606  virtual void select() ;
607 
616  virtual void deselect() ;
618 
621 
628  bool operator == (const Composite& composite) const ;
629 
633  bool operator != (const Composite& composite) const
634  ;
635 
639  bool isEmpty() const ;
640 
644  bool isRoot() const ;
645 
648  bool isRootOf(const Composite& composite) const ;
649 
652  bool isInterior() const ;
653 
656  bool hasChild() const ;
657 
660  bool isChildOf(const Composite& composite) const ;
661 
664  bool isFirstChild() const ;
665 
668  bool isFirstChildOf(const Composite& composite) const ;
669 
672  bool isLastChild() const ;
673 
676  bool isLastChildOf(const Composite& composite) const ;
677 
680  bool hasParent() const ;
681 
684  bool isParentOf(const Composite& composite) const ;
685 
689  bool hasSibling() const ;
690 
693  bool isSiblingOf(const Composite& composite) const ;
694 
698  bool hasPreviousSibling() const ;
699 
702  bool isPreviousSiblingOf(const Composite& composite) const ;
703 
707  bool hasNextSibling() const ;
708 
711  bool isNextSiblingOf(const Composite& composite) const ;
712 
715  bool isDescendantOf(const Composite& composite) const ;
716 
719  template <typename T>
720  bool hasAncestor(const T& dummy) const ;
721 
724  bool isAncestorOf(const Composite& composite) const ;
725 
729  bool isRelatedWith(const Composite& composite) const ;
730 
734  bool isHomomorph(const Composite& composite) const ;
735 
745  bool containsSelection() const ;
747 
757  virtual bool isAtom() const { return false; }
758 
762  virtual bool isMolecule() const { return false; }
763 
767  virtual bool isProtein() const { return false; }
768 
772  virtual bool isFragment() const { return false; }
773 
777  virtual bool isResidue() const { return false; }
778 
782  virtual bool isChain() const { return false; }
784 
790  virtual bool isValid() const ;
791 
796  virtual void dump(std::ostream& s = std::cout, Size depth = 0) const
797  ;
798 
800 
802 
809  void host(Visitor<Composite>& visitor);
810 
815  template <typename T>
816  bool applyAncestor(UnaryProcessor<T>& processor);
817 
822  template <typename T>
823  bool applyAncestor(ConstUnaryProcessor<T>& processor) const;
824 
825 
830  template <typename T>
831  bool applyChild(UnaryProcessor<T>& processor);
832 
837  template <typename T>
838  bool applyChild(ConstUnaryProcessor<T>& processor) const;
839 
847  template <typename T>
848  bool applyDescendantPreorder(UnaryProcessor<T>& processor);
849 
857  template <typename T>
858  bool applyDescendantPreorder(ConstUnaryProcessor<T>& processor) const;
859 
867  template <typename T>
868  bool applyDescendantPostorder(UnaryProcessor<T>& processor);
869 
877  template <typename T>
878  bool applyDescendantPostorder(ConstUnaryProcessor<T>& processor) const;
879 
887  template <typename T>
888  bool applyDescendant(UnaryProcessor<T>& processor);
889 
897  template <typename T>
898  bool applyDescendant(ConstUnaryProcessor<T>& processor) const;
899 
906  template <typename T>
907  bool applyPreorder(UnaryProcessor<T>& processor);
908 
915  template <typename T>
916  bool applyPreorder(ConstUnaryProcessor<T>& processor) const;
917 
924  template <typename T>
925  bool applyPostorder(UnaryProcessor<T>& processor);
926 
933  template <typename T>
934  bool applyPostorder(ConstUnaryProcessor<T>& processor) const;
935 
942  template <typename T>
943  bool apply(UnaryProcessor<T>& processor);
944 
951  template <typename T>
952  bool apply(ConstUnaryProcessor<T>& processor) const;
953 
958  template <typename T>
959  bool applyLevel(UnaryProcessor<T>& processor, long level);
961 
966  template <typename T>
967  bool applyLevel(ConstUnaryProcessor<T>& processor, long level) const;
969 
970 
971 
972 
974  {
975  public:
976 
979 
980  : bound_(0),
981  ancestor_(0)
982  {
983  }
984 
987 
988  : bound_(const_cast<Composite*>(&composite)),
989  ancestor_(0)
990  {
991  }
992 
995 
996  : bound_(traits.bound_),
997  ancestor_(traits.ancestor_)
998  {
999  }
1000 
1001  BALL_INLINE
1002  const AncestorIteratorTraits& operator = (const AncestorIteratorTraits& traits)
1003 
1004  {
1005  bound_ = traits.bound_;
1006  ancestor_ = traits.ancestor_;
1007  return *this;
1008  }
1009 
1010  BALL_INLINE Composite* getContainer() { return bound_; }
1011 
1012  BALL_INLINE const Composite* getContainer() const { return bound_; }
1013 
1014  BALL_INLINE bool isSingular() const { return (bound_ == 0); }
1015 
1016  BALL_INLINE Composite* getPosition() { return ancestor_; }
1017 
1018  BALL_INLINE Composite* const& getPosition() const { return ancestor_; }
1019 
1020  BALL_INLINE bool operator == (const AncestorIteratorTraits& traits) const { return (ancestor_ == traits.ancestor_); }
1021 
1022  BALL_INLINE bool operator != (const AncestorIteratorTraits& traits) const { return !(ancestor_ == traits.ancestor_); }
1023 
1024  BALL_INLINE bool isValid() const { return (bound_ != 0 && ancestor_ != 0); }
1025 
1026  BALL_INLINE void invalidate() { bound_ = ancestor_ = 0; }
1027 
1028  BALL_INLINE void toBegin() { ancestor_ = bound_->parent_; }
1029 
1030  BALL_INLINE bool isBegin() const { return (ancestor_ == bound_->parent_); }
1031 
1032  BALL_INLINE void toEnd() { ancestor_ = 0; }
1033 
1034  BALL_INLINE bool isEnd() const { return (ancestor_ == 0); }
1035 
1036  BALL_INLINE Composite& getData() { return *ancestor_; }
1037 
1038  BALL_INLINE const Composite& getData() const { return *ancestor_; }
1039 
1040  BALL_INLINE void forward() { ancestor_ = ancestor_->parent_; }
1041 
1042  private:
1043 
1044  Composite* bound_;
1045  Composite* ancestor_;
1046  };
1047 
1049 
1052 
1054  {
1055  return AncestorIterator::begin(*this);
1056  }
1057 
1059  {
1060  return AncestorIterator::end(*this);
1061  }
1062 
1065 
1067  {
1068  return AncestorConstIterator::begin(*this);
1069  }
1070 
1072  {
1073  return AncestorConstIterator::end(*this);
1074  }
1075 
1077  {
1078  public:
1079 
1081 
1082  : bound_(0),
1083  child_(0)
1084  {
1085  }
1086 
1088 
1089  : bound_((Composite *)&composite),
1090  child_(0)
1091  {
1092  }
1093 
1095 
1096  : bound_(traits.bound_),
1097  child_(traits.child_)
1098  {
1099  }
1100 
1102 
1103  {
1104  bound_ = traits.bound_;
1105  child_ = traits.child_;
1106  return *this;
1107  }
1108 
1109  BALL_INLINE Composite* getContainer() { return bound_; }
1110 
1111  BALL_INLINE const Composite* getContainer() const { return bound_; }
1112 
1113  BALL_INLINE bool isSingular() const { return (bound_ == 0); }
1114 
1115  BALL_INLINE Composite* getPosition() { return child_; }
1116 
1117  BALL_INLINE Composite* const& getPosition() const { return child_; }
1118 
1119  BALL_INLINE bool operator == (const ChildCompositeIteratorTraits& traits) const { return (child_ == traits.child_); }
1120 
1121  BALL_INLINE bool operator != (const ChildCompositeIteratorTraits& traits) const { return !(child_ == traits.child_); }
1122 
1123  BALL_INLINE bool isValid() const { return (bound_ != 0 && child_ != 0); }
1124 
1125  BALL_INLINE void invalidate() { bound_ = child_ = 0; }
1126 
1127  BALL_INLINE void toBegin() { child_ = bound_->first_child_; }
1128 
1129  BALL_INLINE bool isBegin() const { return (child_ == bound_->first_child_); }
1130 
1131  BALL_INLINE void toEnd() { child_ = 0; }
1132 
1133  BALL_INLINE bool isEnd() const { return (child_ == 0); }
1134 
1135  BALL_INLINE void toRBegin() { child_ = bound_->last_child_; }
1136 
1137  BALL_INLINE bool isRBegin() const { return (child_ == bound_->last_child_); }
1138 
1139  BALL_INLINE void toREnd() { child_ = 0; }
1140 
1141  BALL_INLINE bool isREnd() const { return (child_ == 0); }
1142 
1143  BALL_INLINE Composite& getData() { return *child_; }
1144 
1145  BALL_INLINE const Composite& getData() const { return *child_; }
1146 
1147  BALL_INLINE void forward() { child_ = child_->next_; }
1148 
1150  {
1151  if (child_ == 0)
1152  {
1153  // Allow decrementation for past-the-end iterators
1154  child_ = bound_->last_child_;
1155  }
1156  else
1157  {
1158  child_ = child_->previous_;
1159  }
1160  }
1161 
1162  private:
1163 
1164  Composite* bound_;
1165  Composite* child_;
1166  };
1167 
1169 
1172 
1174 
1175  {
1176  return ChildCompositeIterator::begin(*this);
1177  }
1178 
1180 
1181  {
1182  return ChildCompositeIterator::end(*this);
1183  }
1184 
1185 
1186 
1189 
1191 
1192  {
1193  return ChildCompositeConstIterator::begin(*this);
1194  }
1195 
1197 
1198  {
1199  return ChildCompositeConstIterator::end(*this);
1200  }
1201 
1202 
1203 
1204  typedef std::reverse_iterator<ChildCompositeIterator> ChildCompositeReverseIterator;
1205 
1207  {
1208  return ChildCompositeReverseIterator(endChildComposite());
1209  }
1210 
1212  {
1213  return ChildCompositeReverseIterator(beginChildComposite());
1214  }
1215 
1216 
1217 
1218  typedef std::reverse_iterator<ChildCompositeConstIterator> ChildCompositeConstReverseIterator;
1219 
1221  {
1222  return ChildCompositeConstReverseIterator(endChildComposite());
1223  }
1224 
1226  {
1227  return ChildCompositeConstReverseIterator(beginChildComposite());
1228  }
1229 
1231  {
1232  public:
1233 
1235 
1236  : bound_(0),
1237  position_(0)
1238  {
1239  }
1240 
1242 
1243  : bound_(const_cast<Composite*>(&composite)),
1244  position_(0)
1245  {
1246  }
1247 
1249 
1250  : bound_(traits.bound_),
1251  position_(traits.position_)
1252  {
1253  }
1254 
1256 
1257  BALL_INLINE bool isValid() const
1258  {
1259  return ((bound_ != 0) && (position_ != 0));
1260  }
1261 
1263  {
1264  bound_ = traits.bound_;
1265  position_ = traits.position_;
1266  return *this;
1267  }
1268 
1269  BALL_INLINE Composite* getContainer() { return bound_; }
1270 
1271  BALL_INLINE const Composite* getContainer() const { return bound_; }
1272 
1273  BALL_INLINE bool isSingular() const { return (bound_ == 0); }
1274 
1275  BALL_INLINE Composite* getPosition() { return position_; }
1276 
1277  BALL_INLINE const Composite* getPosition() const { return position_; }
1278  BALL_INLINE void setPosition(Composite* position) { position_ = position; }
1279 
1280 
1281  BALL_INLINE Composite& getData() { return *position_; }
1282 
1283  BALL_INLINE const Composite& getData() const { return *position_; }
1284 
1286  {
1287  return (position_ == traits.position_);
1288  }
1289 
1291  {
1292  return !(position_ == traits.position_);
1293  }
1294 
1296  {
1297  bound_ = 0;
1298  position_ = 0;
1299  }
1300 
1302  {
1303  position_ = bound_;
1304  }
1305 
1306  BALL_INLINE bool isBegin() const
1307  {
1308  return (position_ == bound_);
1309  }
1310 
1312  {
1313  position_ = 0;
1314  }
1315 
1316  BALL_INLINE bool isEnd() const
1317  {
1318  return (position_ == 0);
1319  }
1320 
1322  {
1323  if (bound_ != 0)
1324  {
1325  position_ = findPreviousPosition(0);
1326  }
1327  }
1328 
1329  BALL_INLINE bool isRBegin() const
1330  {
1331  return (position_ == findPreviousPosition(0));
1332  }
1333 
1335  {
1336  position_ = bound_;
1337  }
1338 
1339  BALL_INLINE bool isREnd() const
1340  {
1341  return (position_ == bound_);
1342  }
1343 
1345  {
1346  position_ = findNextPosition(position_);
1347  }
1348 
1350  {
1351  position_ = findPreviousPosition(position_);
1352  }
1353 
1354  protected:
1355 
1358 
1361 
1363  {
1364  // If we are at the root of the iterator, the
1365  // decrementing it results in an invalid iterator
1366  // (past-the-end).
1367  if (p == bound_)
1368  {
1369  return 0;
1370  }
1371  // If we decrement a past-the-end-iterator, we
1372  // start at the root and "fall down" on the right
1373  // hand side following the last_child_ pointers
1374  // until we hit bottom.
1375  else if (p == 0)
1376  {
1377  if (bound_->last_child_ == 0)
1378  {
1379  return bound_;
1380  }
1381  else
1382  {
1383  p = bound_->last_child_;
1384  }
1385  while (p->last_child_ != 0)
1386  {
1387  p = p->last_child_;
1388  }
1389  }
1390  // Normally, we just grab the guy to the
1391  // left in the doubly-linked child list.
1392  else if (p->previous_ != 0)
1393  {
1394  p = p->previous_;
1395 
1396  // If the guy to the left hast children,
1397  // we do the drop on the rigth again.
1398  while (p->last_child_ != 0)
1399  {
1400  p = p->last_child_;
1401  }
1402  }
1403  // Finally, if we can't go down and we can't go
1404  // left, we have to go upwards.
1405  else if (p != bound_)
1406  {
1407  p = p->parent_;
1408  }
1409 
1410  return p;
1411  }
1412 
1414  {
1415  // If we are in a past-the-end position, we stay put.
1416  if (p == 0)
1417  {
1418  return 0;
1419  }
1420  // Otherwise, we try the first child. If there's one,
1421  // that's our next position.
1422  else
1423  {
1424  if (p->first_child_ != 0)
1425  {
1426  p = p->first_child_;
1427  }
1428  else
1429  {
1430  // If we are already in the root node, we are done.
1431  if (p == bound_)
1432  {
1433  return 0;
1434  }
1435  // Otherwise, we try to walk to the right at the current level.
1436  if (p->next_ != 0)
1437  {
1438  p = p->next_;
1439  }
1440  // If that doesn't work out, we'll have to climb up again.
1441  // Now, we either revisit a node we have already been to, or we
1442  // are trying to climb up *beyond* our iteration root (bound_).
1443  // In the latter case, we return a past-the-end-iterator (0).
1444  else
1445  {
1446  // If we could not walk left or right and we are at the root
1447  // again, then we are done with the iteration (this is the
1448  // case if bound_ is a leaf node).
1449  while (p->next_ == 0)
1450  {
1451  p = p->parent_;
1452  if ((p == bound_) || (p == 0))
1453  {
1454  return 0;
1455  }
1456  }
1457  p = p->next_;
1458  }
1459  }
1460  }
1461  return p;
1462  }
1463  };
1464 
1466 
1469 
1470  CompositeIterator beginComposite() { return CompositeIterator::begin(*this); }
1471 
1472  CompositeIterator endComposite() { return CompositeIterator::end(*this); }
1473 
1476 
1478  {
1479  return CompositeConstIterator::begin(*this);
1480  }
1481 
1483  {
1484  return CompositeConstIterator::end(*this);
1485  }
1486 
1487 
1488  typedef std::reverse_iterator<CompositeIterator> CompositeReverseIterator;
1489 
1491  {
1492  return CompositeReverseIterator(endComposite());
1493  }
1494 
1496  {
1497  return CompositeReverseIterator(beginComposite());
1498  }
1499 
1500 
1501  typedef std::reverse_iterator<CompositeConstIterator> CompositeConstReverseIterator;
1502 
1504  {
1505  return CompositeConstReverseIterator(endComposite());
1506  }
1507 
1509  {
1510  return CompositeConstReverseIterator(beginComposite());
1511  }
1512 
1513  /*
1514  * This function removes and deletes all composites that are
1515  * supplied in the list of children.
1516  */
1517  void deleteChildrenList_(std::list<Composite*>& composites);
1518 
1519  private:
1520 
1522  Size getHeight_(Size size, Size& max_height) const ;
1523 
1525  Size countDescendants_() const ;
1526 
1528  void clone_(Composite& parent, Composite& stack) const ;
1529 
1530  // \throws Exception::GeneralException
1531  template <typename T>
1532  bool applyLevelNostart_(UnaryProcessor<T>& processor, long level);
1533 
1534  // \throws Exception::GeneralException
1535  template <typename T>
1536  bool applyLevelNostart_(ConstUnaryProcessor<T>& processor, long level) const;
1537 
1538  // \throws Exception::GeneralException
1539  template <typename T>
1540  bool applyChildNostart_(UnaryProcessor<T>& processor);
1541 
1542  // \throws Exception::GeneralException
1543  template <typename T>
1544  bool applyChildNostart_(ConstUnaryProcessor<T>& processor) const;
1545 
1546  // \throws Exception::GeneralException
1547  template <typename T>
1548  bool applyPreorderNostart_(UnaryProcessor<T>& processor);
1549 
1550  // \throws Exception::GeneralException
1551  bool applyPreorderNostart_(UnaryProcessor<Atom>& processor);
1552 
1553  // \throws Exception::GeneralException
1554  template <typename T>
1555  bool applyPreorderNostart_(ConstUnaryProcessor<T>& processor) const;
1556 
1557  // \throws Exception::GeneralException
1558  template <typename T>
1559  bool applyDescendantPreorderNostart_(UnaryProcessor<T>& processor);
1560 
1561  // \throws Exception::GeneralException
1562  bool applyDescendantPreorderNostart_(UnaryProcessor<Atom>& processor);
1563 
1564  // \throws Exception::GeneralException
1565  template <typename T>
1566  bool applyDescendantPreorderNostart_(ConstUnaryProcessor<T>& processor) const;
1567 
1568  // \throws Exception::GeneralException
1569  template <typename T>
1570  bool applyDescendantPostorderNostart_(UnaryProcessor<T>& processor);
1571 
1572  // \throws Exception::GeneralException
1573  template <typename T>
1574  bool applyDescendantPostorderNostart_(ConstUnaryProcessor<T>& processor) const;
1575 
1576  void updateSelection_();
1577  void determineSelection_();
1578  void select_(bool update_parent = true);
1579  void deselect_(bool update_parent = true);
1580 
1581  void destroyChildren_();
1582 
1583  // private attributes
1584 
1585  Size number_of_children_;
1586  Composite* parent_;
1587  Composite* previous_;
1588  Composite* next_;
1589  Composite* first_child_;
1590  Composite* last_child_;
1591  unsigned char properties_;
1592  bool contains_selection_;
1593  Size number_of_selected_children_;
1594  Size number_of_children_containing_selection_;
1595  TimeStamp selection_stamp_;
1596  TimeStamp modification_stamp_;
1597  };
1598 
1599  template <typename T>
1601  {
1602  if (processor.start() == false)
1603  {
1604  return false;
1605  }
1606 
1607  Processor::Result result;
1608 
1609  for (Composite* composite = parent_; composite != 0; composite = composite->parent_)
1610  {
1611  T* t_ptr;
1612  if ((t_ptr = dynamic_cast<T*>(composite)) != 0)
1613  {
1614  result = processor(*t_ptr);
1615  if (result <= Processor::BREAK)
1616  {
1617  return (result == Processor::BREAK);
1618  }
1619  }
1620  }
1621 
1622  return processor.finish();
1623  }
1624 
1625  template <typename T>
1627  {
1628  if (processor.start() == false)
1629  {
1630  return false;
1631  }
1632 
1633  Processor::Result result;
1634 
1635  for (const Composite* composite = parent_; composite != 0; composite = composite->parent_)
1636  {
1637  const T* t_ptr;
1638  if ((t_ptr = dynamic_cast<const T*>(composite)) != 0)
1639  {
1640  result = processor(*t_ptr);
1641  if (result <= Processor::BREAK)
1642  {
1643  return (result == Processor::BREAK);
1644  }
1645  }
1646  }
1647 
1648  return processor.finish();
1649  }
1650 
1651  template <typename T>
1653  {
1654  return processor.start() && applyChildNostart_(processor) && processor.finish();
1655  }
1656 
1657  template <typename T>
1659  {
1660  return processor.start() && applyChildNostart_(processor) && processor.finish();
1661  }
1662 
1663  template <typename T>
1664  bool Composite::applyChildNostart_(UnaryProcessor<T>& processor)
1665  {
1667 
1668  for (Composite* composite = first_child_;
1669  composite != 0; composite = composite->next_)
1670  {
1671  T* t_ptr;
1672  if ((t_ptr = dynamic_cast<T*>(composite)) != 0)
1673  {
1674  result = processor(*t_ptr);
1675  if (result <= Processor::BREAK)
1676  {
1677  break;
1678  }
1679  }
1680  }
1681 
1682  return (result >= Processor::BREAK);
1683  }
1684 
1685  template <typename T>
1686  bool Composite::applyChildNostart_(ConstUnaryProcessor<T>& processor) const
1687  {
1689 
1690  for (const Composite* composite = first_child_;
1691  composite != 0; composite = composite->next_)
1692  {
1693  const T* t_ptr;
1694  if ((t_ptr = dynamic_cast<const T*>(composite)) != 0)
1695  {
1696  result = processor(*t_ptr);
1697  if (result <= Processor::BREAK)
1698  {
1699  break;
1700  }
1701  }
1702  }
1703 
1704  return (result >= Processor::BREAK);
1705  }
1706 
1707  template <typename T>
1709  {
1710  return processor.start() && applyDescendantPreorderNostart_(processor) && processor.finish();
1711  }
1712 
1713  template <typename T>
1715  {
1716  return processor.start() && applyDescendantPreorderNostart_(processor) && processor.finish();
1717  }
1718 
1719  template <typename T>
1720  bool Composite::applyDescendantPreorderNostart_(UnaryProcessor<T>& processor)
1721  {
1722  Processor::Result result;
1723 
1724  for (Composite* composite = first_child_;
1725  composite != 0; composite = composite->next_)
1726  {
1727  T* t_ptr;
1728  if ((t_ptr = dynamic_cast<T*>(composite)) != 0)
1729  {
1730  result = processor(*t_ptr);
1731 
1732  if (result <= Processor::BREAK)
1733  {
1734  return (result == Processor::BREAK);
1735  }
1736  }
1737 
1738  if (composite->first_child_ != 0 && composite->applyDescendantPreorderNostart_(processor) == false)
1739  {
1740  return false;
1741  }
1742  }
1743 
1744  return true;
1745  }
1746 
1747  template <typename T>
1748  bool Composite::applyDescendantPreorderNostart_(ConstUnaryProcessor<T>& processor) const
1749  {
1750  Processor::Result result;
1751 
1752  for (const Composite* composite = first_child_;
1753  composite != 0; composite = composite->next_)
1754  {
1755  const T* t_ptr;
1756  if ((t_ptr = dynamic_cast<const T*>(composite)) != 0)
1757  {
1758  result = processor(*t_ptr);
1759 
1760  if (result <= Processor::BREAK)
1761  {
1762  return (result == Processor::BREAK);
1763  }
1764  }
1765 
1766  if (composite->first_child_ != 0 && composite->applyDescendantPreorderNostart_(processor) == false)
1767  {
1768  return false;
1769  }
1770  }
1771 
1772  return true;
1773  }
1774 
1775  template <typename T>
1777  {
1778  return processor.start() && applyDescendantPostorderNostart_(processor) && processor.finish();
1779  }
1780 
1781  template <typename T>
1783  {
1784  return processor.start() && applyDescendantPostorderNostart_(processor) && processor.finish();
1785  }
1786 
1787  template <typename T>
1788  bool Composite::applyDescendantPostorderNostart_(UnaryProcessor<T>& processor)
1789  {
1790  Processor::Result result;
1791 
1792  for (Composite* composite = first_child_;
1793  composite != 0; composite = composite->next_)
1794  {
1795  if (composite->first_child_ != 0 &&
1796  composite->applyDescendantPostorderNostart_(processor) == false)
1797  {
1798  return false;
1799  }
1800 
1801  T* t_ptr = dynamic_cast<T*>(composite);
1802  if (t_ptr != 0)
1803  {
1804  result = processor(*t_ptr);
1805 
1806  if (result <= Processor::BREAK)
1807  {
1808  return (result == Processor::BREAK);
1809  }
1810  }
1811  }
1812 
1813  return true;
1814  }
1815 
1816  template <typename T>
1817  bool Composite::applyDescendantPostorderNostart_(ConstUnaryProcessor<T>& processor) const
1818  {
1819  Processor::Result result;
1820 
1821  for (const Composite* composite = first_child_;
1822  composite != 0; composite = composite->next_)
1823  {
1824  if (composite->first_child_ != 0 &&
1825  composite->applyDescendantPostorderNostart_(processor) == false)
1826  {
1827  return false;
1828  }
1829 
1830  const T* t_ptr = dynamic_cast<const T*>(composite);
1831  if (t_ptr != 0)
1832  {
1833  result = processor(*t_ptr);
1834 
1835  if (result <= Processor::BREAK)
1836  {
1837  return (result == Processor::BREAK);
1838  }
1839  }
1840  }
1841 
1842  return true;
1843  }
1844 
1845  template <typename T>
1847  {
1848  if (!processor.start() || !applyDescendantPostorderNostart_(processor))
1849  {
1850  return false;
1851  }
1852 
1853  T* t_ptr = dynamic_cast<T*>(this);
1854 
1855  return (t_ptr != 0 &&
1856  processor(*t_ptr) >= Processor::BREAK &&
1857  processor.finish() );
1858  }
1859 
1860  template <typename T>
1862  {
1863  if (!processor.start() || !applyDescendantPostorderNostart_(processor))
1864  {
1865  return false;
1866  }
1867 
1868  const T* t_ptr = dynamic_cast<const T*>(this);
1869 
1870  return (t_ptr != 0 &&
1871  processor(*t_ptr) >= Processor::BREAK &&
1872  processor.finish() );
1873  }
1874 
1875 
1876  template <typename T>
1877  bool Composite::applyLevel(UnaryProcessor<T>& processor, long level)
1878  {
1879  return processor.start() && applyLevelNostart_(processor, level) && processor.finish();
1880  }
1881 
1882  template <typename T>
1883  bool Composite::applyLevel(ConstUnaryProcessor<T>& processor, long level) const
1884  {
1885  return processor.start() && applyLevelNostart_(processor, level) && processor.finish();
1886  }
1887 
1888  template <typename T>
1889  bool Composite::applyLevelNostart_(UnaryProcessor<T>& processor, long level)
1890  {
1891  if (level == 0)
1892  {
1893  T* t_ptr = dynamic_cast<T*>(this);
1894  if (t_ptr != 0)
1895  {
1896  Processor::Result result = processor(*t_ptr);
1897 
1898  if (result <= Processor::BREAK)
1899  {
1900  return (result == Processor::BREAK);
1901  }
1902  }
1903  }
1904  else
1905  {
1906  if (--level == 0)
1907  {
1908  return applyChildNostart_(processor);
1909  }
1910  else
1911  {
1912  if (level > 0)
1913  {
1914  for (Composite* composite = first_child_;
1915  composite != 0; composite = composite->next_)
1916  {
1917  if (composite->first_child_ != 0 && composite->applyLevelNostart_(processor, level) == false)
1918  {
1919  return false;
1920  }
1921  }
1922  }
1923  }
1924  }
1925  return true;
1926  }
1927 
1928  template <typename T>
1929  bool Composite::applyLevelNostart_(ConstUnaryProcessor<T>& processor, long level) const
1930  {
1931  if (level == 0)
1932  {
1933  const T* t_ptr = dynamic_cast<const T*>(this);
1934  if (t_ptr != 0)
1935  {
1936  Processor::Result result = processor(*t_ptr);
1937 
1938  if (result <= Processor::BREAK)
1939  {
1940  return (result == Processor::BREAK);
1941  }
1942  }
1943  }
1944  else
1945  {
1946  if (--level == 0)
1947  {
1948  return applyChildNostart_(processor);
1949  }
1950  else
1951  {
1952  if (level > 0)
1953  {
1954  for (const Composite* composite = first_child_;
1955  composite != 0; composite = composite->next_)
1956  {
1957  if (composite->first_child_ != 0 && composite->applyLevelNostart_(processor, level) == false)
1958  {
1959  return false;
1960  }
1961  }
1962  }
1963  }
1964  }
1965  return true;
1966  }
1967 
1968 
1969  template <typename T>
1970  bool Composite::applyPreorderNostart_(UnaryProcessor<T>& processor)
1971  {
1972  Processor::Result result;
1973  bool return_value;
1974  T* t_ptr = dynamic_cast<T*>(this);
1975  if (t_ptr != 0)
1976  {
1977  result = processor(*t_ptr);
1978 
1979  if (result <= Processor::BREAK)
1980  {
1981  return_value = (result == Processor::BREAK);
1982  }
1983  else
1984  {
1985  return_value = applyDescendantPreorderNostart_(processor);
1986  }
1987  }
1988  else
1989  {
1990  return_value = applyDescendantPreorderNostart_(processor);
1991  }
1992 
1993  return return_value;
1994  }
1995 
1996  template <typename T>
1997  bool Composite::applyPreorderNostart_(ConstUnaryProcessor<T>& processor) const
1998  {
1999  Processor::Result result;
2000  bool return_value;
2001  const T* t_ptr = dynamic_cast<const T*>(this);
2002  if (t_ptr != 0)
2003  {
2004  result = processor(*t_ptr);
2005 
2006  if (result <= Processor::BREAK)
2007  {
2008  return_value = (result == Processor::BREAK);
2009  }
2010  else
2011  {
2012  return_value = applyDescendantPreorderNostart_(processor);
2013  }
2014  }
2015  else
2016  {
2017  return_value = applyDescendantPreorderNostart_(processor);
2018  }
2019 
2020  return return_value;
2021  }
2022 
2023  template <typename T>
2025  {
2026  return applyDescendantPreorder(processor);
2027  }
2028 
2029  template <typename T>
2031  {
2032  return applyDescendantPreorder(processor);
2033  }
2034 
2035  template <typename T>
2037  {
2038  return processor.start() && applyPreorderNostart_(processor) && processor.finish();
2039  }
2040 
2041  template <typename T>
2043  {
2044  return processor.start() && applyPreorderNostart_(processor) && processor.finish();
2045  }
2046 
2047  template <typename T>
2048  BALL_INLINE
2050  {
2051  return applyPreorder(processor);
2052  }
2053 
2054  template <typename T>
2055  BALL_INLINE
2057  {
2058  return applyPreorder(processor);
2059  }
2060 
2061  template <typename T>
2062  BALL_INLINE
2063  T* Composite::getAncestor(const T& /* dummy */)
2064 
2065  {
2066  T* T_ptr = 0;
2067 
2068  for (Composite* composite_ptr = parent_;
2069  composite_ptr != 0; composite_ptr = composite_ptr->parent_)
2070  {
2071  T_ptr = dynamic_cast<T*>(composite_ptr);
2072  if (T_ptr != 0)
2073  {
2074  break;
2075  }
2076  }
2077 
2078  return T_ptr;
2079  }
2080 
2081  template <typename T>
2082  BALL_INLINE
2083  const T* Composite::getAncestor(const T& /* dummy */) const
2084 
2085  {
2086  T* t_ptr = 0;
2087  for (Composite* composite_ptr = parent_;
2088  composite_ptr != 0; composite_ptr = composite_ptr->parent_)
2089  {
2090  if ((t_ptr = dynamic_cast<T*>(composite_ptr)) != 0)
2091  {
2092  break;
2093  }
2094  }
2095 
2096  return const_cast<const T*>(t_ptr);
2097  }
2098 
2099  template <typename T>
2100  BALL_INLINE
2101  T* Composite::getPrevious(const T& /* dummy */)
2102 
2103  {
2104  // create an iterator bound to the root of the subtree
2106 
2107  // set its position to the current composite
2108  it.getTraits().setPosition(this);
2109 
2110  // walk back until we find something
2111  // or we cannot walk any further
2112  if (+it)
2113  {
2114  do
2115  {
2116  --it;
2117  }
2118  while (+it && !RTTI::isKindOf<T>(*it));
2119  }
2120 
2121  // return a NULL pointer if nothing was found
2122  Composite* ptr = 0;
2123  if (+it)
2124  {
2125  ptr = &*it;
2126  }
2127 
2128  return dynamic_cast<T*>(ptr);
2129  }
2130 
2131  template <typename T>
2132  BALL_INLINE
2133  const T* Composite::getPrevious(const T& dummy) const
2134 
2135  {
2136  // cast away the constness of this and call the non-const method
2137  Composite* nonconst_this = const_cast<Composite*>(this);
2138 
2139  return const_cast<const T*>(nonconst_this->getPrevious(dummy));
2140  }
2141 
2142  template <typename T>
2143  BALL_INLINE
2144  T* Composite::getNext(const T& /* dummy */)
2145 
2146  {
2147  // create an iterator bound to the root of the subtree
2149 
2150  // set its position to the current composite
2151  it.getTraits().setPosition(this);
2152 
2153  // walk forward until we find something
2154  // or we cannot walk any further
2155  do
2156  {
2157  it++;
2158  }
2159  while (it.isValid() && !RTTI::isKindOf<T>(*it));
2160 
2161 
2162  // return a NULL pointer if nothing was found
2163  Composite* ptr = 0;
2164  if (+it)
2165  {
2166  ptr = &*it;
2167  }
2168 
2169  return dynamic_cast<T*>(ptr);
2170  }
2171 
2172  template <typename T>
2173  BALL_INLINE
2174  const T* Composite::getNext(const T& dummy) const
2175 
2176  {
2177  // cast away the constness of this and call the non-const method
2178  Composite* nonconst_this = const_cast<Composite*>(this);
2179 
2180  return const_cast<const T*>(nonconst_this->getNext(dummy));
2181  }
2182 
2183  template <typename T>
2184  BALL_INLINE
2185  bool Composite::hasAncestor(const T& dummy ) const
2186 
2187  {
2188  return (getAncestor(dummy) != 0);
2189  }
2190 
2191 # ifndef BALL_NO_INLINE_FUNCTIONS
2192 # include <BALL/CONCEPT/composite.iC>
2193 # endif
2194 
2195 
2196 } // namespace BALL
2197 
2198 #endif // BALL_CONCEPT_COMPOSITE_H
BALL_INLINE bool isSingular() const
Definition: composite.h:1014
BALL_INLINE Composite * getPosition()
Definition: composite.h:1275
BALL_INLINE Composite *const & getPosition() const
Definition: composite.h:1117
ChildCompositeIteratorTraits(const ChildCompositeIteratorTraits &traits)
Definition: composite.h:1094
CompositeReverseIterator rendComposite()
Definition: composite.h:1495
Composite * bound_
A pointer to the "container" the iterator is bound to.
Definition: composite.h:1357
bool applyDescendantPreorder(UnaryProcessor< T > &processor)
Definition: composite.h:1708
BALL_INLINE Composite & getData()
Definition: composite.h:1036
ChildCompositeIterator endChildComposite()
Definition: composite.h:1179
CompositeIteratorTraits(const CompositeIteratorTraits &traits)
Definition: composite.h:1248
BALL_INLINE const Composite & getData() const
Definition: composite.h:1038
bool hasAncestor(const T &dummy) const
ChildCompositeReverseIterator rbeginChildComposite()
Definition: composite.h:1206
BALL_INLINE bool isBegin() const
Definition: composite.h:1030
T * getPrevious(const T &)
AncestorConstIterator beginAncestor() const
Definition: composite.h:1066
ConstBidirectionalIterator< Composite, Composite, Composite *, CompositeIteratorTraits > CompositeConstIterator
Definition: composite.h:1475
virtual bool start()
Definition: processor.h:92
std::reverse_iterator< ChildCompositeConstIterator > ChildCompositeConstReverseIterator
Definition: composite.h:1218
bool applyDescendantPostorder(UnaryProcessor< T > &processor)
Definition: composite.h:1776
BALL_INLINE bool isREnd() const
Definition: composite.h:1339
bool applyPostorder(UnaryProcessor< T > &processor)
Definition: composite.h:1846
bool applyPreorder(UnaryProcessor< T > &processor)
Definition: composite.h:2036
ChildCompositeReverseIterator rendChildComposite()
Definition: composite.h:1211
T * getNext(const T &)
AncestorIterator endAncestor()
Definition: composite.h:1058
CompositeConstIterator beginComposite() const
Definition: composite.h:1477
bool applyChild(UnaryProcessor< T > &processor)
Definition: composite.h:1652
BALL_INLINE Composite * getContainer()
Definition: composite.h:1109
virtual bool isProtein() const
Definition: composite.h:767
BALL_INLINE Composite * getContainer()
Definition: composite.h:1010
bool applyDescendant(UnaryProcessor< T > &processor)
Definition: composite.h:2024
CompositeReverseIterator rbeginComposite()
Definition: composite.h:1490
BALL_INLINE AncestorIteratorTraits(const Composite &composite)
Definition: composite.h:986
BALL_INLINE AncestorIteratorTraits(const AncestorIteratorTraits &traits)
Definition: composite.h:994
BALL_INLINE bool isValid() const
Definition: composite.h:1024
BALL_INLINE Composite * getContainer()
Definition: composite.h:1269
BALL_INLINE bool isBegin() const
Definition: composite.h:1306
CompositeConstIterator endComposite() const
Definition: composite.h:1482
CompositeConstReverseIterator rbeginComposite() const
Definition: composite.h:1503
#define BALL_INLINE
Definition: debug.h:15
UnaryPredicate< Composite > KernelPredicateType
Definition: composite.h:88
BALL_INLINE Composite & getData()
Definition: composite.h:1281
CompositeIteratorTraits(const Composite &composite)
Definition: composite.h:1241
BALL_INLINE void setPosition(Composite *position)
Definition: composite.h:1278
BALL_INLINE bool isValid() const
Definition: baseIterator.h:175
ChildCompositeIterator beginChildComposite()
Definition: composite.h:1173
CompositeIterator beginComposite()
Definition: composite.h:1470
ChildCompositeConstReverseIterator rendChildComposite() const
Definition: composite.h:1225
BALL_EXPORT bool operator!=(const String &s1, const String &s2)
bool applyAncestor(UnaryProcessor< T > &processor)
Definition: composite.h:1600
BALL_INLINE const Composite * getContainer() const
Definition: composite.h:1012
#define BALL_CREATE_DEEP(name)
Definition: create.h:26
std::reverse_iterator< ChildCompositeIterator > ChildCompositeReverseIterator
Definition: composite.h:1204
virtual bool isFragment() const
Definition: composite.h:772
std::reverse_iterator< CompositeConstIterator > CompositeConstReverseIterator
Definition: composite.h:1501
virtual bool isChain() const
Definition: composite.h:782
CompositeConstReverseIterator rendComposite() const
Definition: composite.h:1508
std::reverse_iterator< CompositeIterator > CompositeReverseIterator
Definition: composite.h:1488
Composite * findPreviousPosition(Composite *p) const
Definition: composite.h:1362
BALL_INLINE const Traits & getTraits() const
Get a constant reference to the traits of this iterator.
Definition: baseIterator.h:130
BALL_INLINE const Composite * getContainer() const
Definition: composite.h:1271
BidirectionalIterator< Composite, Composite, Composite *, CompositeIteratorTraits > CompositeIterator
Definition: composite.h:1468
T * getAncestor(const T &)
bool apply(UnaryProcessor< T > &processor)
BALL_EXPORT bool operator==(const String &s1, const String &s2)
ChildCompositeConstIterator endChildComposite() const
Definition: composite.h:1196
Composite & getRoot()
Composite * position_
The current iterator position.
Definition: composite.h:1360
ChildCompositeConstReverseIterator rbeginChildComposite() const
Definition: composite.h:1220
virtual bool start()
Definition: processor.h:128
virtual bool finish()
Definition: processor.h:133
ChildCompositeIteratorTraits(const Composite &composite)
Definition: composite.h:1087
virtual bool finish()
Definition: processor.h:99
BALL_INLINE bool isRBegin() const
Definition: composite.h:1329
virtual bool isResidue() const
Definition: composite.h:777
BALL_INLINE Composite * getPosition()
Definition: composite.h:1016
Mutable bidirectional iterator.
BidirectionalIterator< Composite, Composite, Composite *, ChildCompositeIteratorTraits > ChildCompositeIterator
Definition: composite.h:1171
BALL_INLINE bool isSingular() const
Definition: composite.h:1273
ConstBidirectionalIterator< Composite, Composite, Composite *, ChildCompositeIteratorTraits > ChildCompositeConstIterator
Definition: composite.h:1188
CompositeIterator endComposite()
Definition: composite.h:1472
char Atom[5]
Definition: PDBdefs.h:257
AncestorIterator beginAncestor()
Definition: composite.h:1053
virtual bool isMolecule() const
Definition: composite.h:762
ConstForwardIterator< Composite, Composite, Composite *, AncestorIteratorTraits > AncestorConstIterator
Definition: composite.h:1064
BALL_INLINE bool isValid() const
Definition: composite.h:1257
BALL_INLINE const Composite * getPosition() const
Definition: composite.h:1277
BALL_INLINE bool isEnd() const
Definition: composite.h:1316
bool applyLevel(UnaryProcessor< T > &processor, long level)
Definition: composite.h:1877
Composite * findNextPosition(Composite *p) const
Definition: composite.h:1413
BALL_INLINE const Composite * getContainer() const
Definition: composite.h:1111
BALL_INLINE const Composite & getData() const
Definition: composite.h:1145
AncestorConstIterator endAncestor() const
Definition: composite.h:1071
#define BALL_EXPORT
Definition: COMMON/global.h:50
ChildCompositeConstIterator beginChildComposite() const
Definition: composite.h:1190
BALL_INLINE bool isEnd() const
Definition: composite.h:1034
ForwardIterator< Composite, Composite, Composite *, AncestorIteratorTraits > AncestorIterator
Definition: composite.h:1051
BALL_INLINE Composite *const & getPosition() const
Definition: composite.h:1018
BALL_INLINE const Composite & getData() const
Definition: composite.h:1283