Loading...
Searching...
No Matches
simpleMolecularGraph.h
Go to the documentation of this file.
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#ifndef BALL_STRUCTURE_SIMPLEMOLECULARGRAPH_H
#define BALL_STRUCTURE_SIMPLEMOLECULARGRAPH_H
#ifndef BALL_KERNEL_ATOM_H
# include <BALL/KERNEL/atom.h>
#endif
#ifndef BALL_KERNEL_BOND_H
# include <BALL/KERNEL/bond.h>
#endif
#ifndef BALL_STRUCTURE_MOLECULE_H
# include <BALL/KERNEL/molecule.h>
#endif
#ifndef BALL_STRUCTURE_FRAGMENT_H
# include <BALL/KERNEL/fragment.h>
#endif
#include <list>
#include <iostream>
#include <algorithm>
namespace BALL
{
// forward declarations to resolve nasty dependencies
template <typename Node, typename Edge>
class EdgeItem;
template <typename Node, typename Edge>
class TSimpleMolecularGraph;
template <typename Node, typename Edge>
class NodeItem
{
public:
typedef NodeItem<Node, Edge> NodeItemType;
typedef EdgeItem<Node, Edge> EdgeItemType;
typedef typename std::list<EdgeItem<Node, Edge>*>::iterator Iterator;
typedef typename std::list<EdgeItem<Node, Edge>*>::const_iterator ConstIterator;
friend class TSimpleMolecularGraph<Node, Edge>;
NodeItem() ;
NodeItem(const Atom& atom) ;
Node& getData() ;
const Node& getData() const ;
void setData(const Node& data) ;
const Atom* getAtom() const ;
Atom* getAtom() ;
Iterator begin() ;
ConstIterator begin() const ;
Iterator end() ;
ConstIterator end() const ;
Size getDegree() const ;
bool operator == (const NodeItem& item) const ;
bool operator != (const NodeItem& item) const ;
protected:
void deleteEdge_(EdgeItemType* item)
;
Node data_;
Atom* atom_;
std::list<EdgeItemType*> adjacent_edges_;
};
template <typename Node, typename Edge>
class EdgeItem
{
public:
typedef NodeItem<Node, Edge> NodeItemType;
typedef EdgeItem<Node, Edge> EdgeItemType;
typedef typename std::list<NodeItem<Node, Edge>*>::iterator Iterator;
typedef typename std::list<NodeItem<Node, Edge>*>::const_iterator ConstIterator;
EdgeItem()
: bond_(0), source_(0), target_(0)
{}
EdgeItem(const Bond& bond);
EdgeItem(const Bond& bond, NodeItemType* source, NodeItemType* target);
NodeItemType& getSource() {return *source_;}
NodeItemType& getTarget() {return *target_;}
const NodeItemType& getSource() const {return *source_;}
const NodeItemType& getTarget() const {return *target_;}
Node& getData() {return data_;}
const Node& getData() const {return data_;}
void setData(const Edge& data) { data_ = data; };
Bond* getBond() { return bond_; }
const Bond* getBond() const { return bond_; }
bool operator == (const EdgeItem& item) const { return (bond_ == item.bond_); }
bool operator != (const EdgeItem& item) const { return (bond_ != item.bond_); }
protected:
Edge data_;
Bond* bond_;
NodeItemType* source_;
NodeItemType* target_;
};
template <typename Node, typename Edge>
EdgeItem<Node, Edge>::EdgeItem(const Bond& bond)
: bond_(const_cast<Bond*>(&bond))
{
}
template <typename Node, typename Edge>
EdgeItem<Node, Edge>::EdgeItem(const Bond& bond, NodeItem<Node, Edge>* first, NodeItem<Node, Edge>* second)
: bond_(const_cast<Bond*>(&bond)),
source_(first),
target_(second)
{
}
template <typename Node, typename Edge>
class TSimpleMolecularGraph
{
public:
typedef NodeItem<Node, Edge> NodeItemType;
typedef EdgeItem<Node, Edge> EdgeItemType;
typedef typename std::list<NodeItemType>::iterator NodeIterator;
typedef typename std::list<NodeItemType>::const_iterator NodeConstIterator;
typedef typename std::list<EdgeItemType>::iterator EdgeIterator;
typedef typename std::list<EdgeItemType>::const_iterator EdgeConstIterator;
TSimpleMolecularGraph() ;
TSimpleMolecularGraph(const Molecule& molecule) ;
bool newNode(const Atom& atom) ;
bool newEdge(const Bond& bond) ;
bool deleteNode(NodeItemType& node);
bool deleteEdge(EdgeItemType& edge);
bool deleteNode(const Atom& atom);
bool deleteEdge(const Bond& bond);
NodeIterator beginNode() { return nodes_.begin(); }
NodeConstIterator beginNode() const { return nodes_.begin(); }
EdgeIterator beginEdge() { return edges_.begin(); }
EdgeConstIterator beginEdge() const { return edges_.begin(); }
NodeIterator endNode() { return nodes_.end(); }
NodeConstIterator endNode() const { return nodes_.end(); }
EdgeIterator endEdge() { return edges_.end(); }
EdgeConstIterator endEdge() const { return edges_.end(); }
bool has(const Atom& atom) const { return atom_to_node_.has(const_cast<Atom*>(&atom)); }
bool has(const Bond& bond) const { return bond_to_edge_.has(const_cast<Bond*>(&bond)); }
NodeItemType& getNode(Position index) { return nodes_[index]; };
const NodeItemType& getNode(Position index) const { return nodes_[index]; };
EdgeItemType& getEdge(Position index) { return edges_[index]; };
const EdgeItemType& getEdge(Position index) const { return edges_[index]; };
Size getNumberOfNodes() const ;
Size getNumberOfEdges() const ;
protected:
std::list<NodeItemType> nodes_;
std::list<EdgeItemType> edges_;
HashMap<Atom*, NodeItemType*> atom_to_node_;
HashMap<Bond*, EdgeItemType*> bond_to_edge_;
};
typedef TSimpleMolecularGraph<Index, Index> SimpleMolecularGraph;
template <typename Node, typename Edge>
TSimpleMolecularGraph<Node, Edge>::TSimpleMolecularGraph()
: nodes_(),
edges_(),
atom_to_node_(),
bond_to_edge_()
{
}
template <typename Node, typename Edge>
TSimpleMolecularGraph<Node, Edge>::TSimpleMolecularGraph(const Molecule& molecule)
: nodes_(),
edges_(),
atom_to_node_(),
bond_to_edge_()
{
AtomConstIterator ai = molecule.beginAtom();
Atom::BondConstIterator bi;
for (; +ai; ++ai)
{
newNode(*ai);
}
for (ai = molecule.beginAtom(); +ai; ++ai)
{
for (bi = ai->beginBond(); +bi; ++bi)
{
if (bi->getFirstAtom() == &*ai)
{
newEdge(*bi);
}
}
}
}
template <typename Node, typename Edge>
bool TSimpleMolecularGraph<Node, Edge>::newNode(const Atom& atom)
{
Atom* atom_ptr = const_cast<Atom*>(&atom);
if (atom_to_node_.has(atom_ptr))
{
return false;
}
nodes_.push_back(NodeItemType(atom));
atom_to_node_.insert(std::pair<Atom*, NodeItemType*>(atom_ptr, &(nodes_.back())));
return true;
}
template <typename Node, typename Edge>
bool TSimpleMolecularGraph<Node, Edge>::newEdge(const Bond& bond)
{
// Create convenience aliases for atoms.
Atom* first = const_cast<Atom*>(bond.getFirstAtom());
Atom* second = const_cast<Atom*>(bond.getSecondAtom());
// Make sure we have atoms/nodes for this bond/edge.
if (!atom_to_node_.has(first) || !atom_to_node_.has(second))
{
return false;
}
// Make sure not to create the same edge twice.
if (bond_to_edge_.has(const_cast<Bond*>(&bond)))
{
return true;
}
// Create the new edge und add it to the hash map relating
// the bond to the edge.
NodeItemType* first_item = atom_to_node_[first];
NodeItemType* second_item = atom_to_node_[second];
edges_.push_back(EdgeItemType(bond, first_item, second_item));
bond_to_edge_.insert(std::pair<Bond*, EdgeItemType*>(const_cast<Bond*>(&bond), &edges_.back()));
first_item->adjacent_edges_.push_back(&edges_.back());
second_item->adjacent_edges_.push_back(&edges_.back());
return true;
}
template <typename Node, typename Edge>
std::ostream& operator << (std::ostream& os, const TSimpleMolecularGraph<Node, Edge>& G)
{
os << "Nodes:" << std::endl;
typename TSimpleMolecularGraph<Node, Edge>::NodeConstIterator node = G.beginNode();
Size count = 0;
for (; node != G.endNode(); ++node)
{
os << count++ << ": " << node->getAtom()->getFullName() << " [" << node->getDegree() << "] '" << node->getAtom() << "'" << std::endl;
}
os << "Edges:" << std::endl;
typename TSimpleMolecularGraph<Node, Edge>::EdgeConstIterator edge = G.beginEdge();
count = 0;
for (; edge != G.endEdge(); ++edge)
{
os << count++ << ": " << edge->getSource().getAtom() << "-" << edge->getTarget().getAtom() << " '" << edge->getBond() << "'" << std::endl;
}
return os;
}
template <typename Node, typename Edge>
bool TSimpleMolecularGraph<Node, Edge>::deleteNode(const Atom& atom)
{
if (!atom_to_node_.has(const_cast<Atom*>(&atom)))
{
return false;
}
return deleteNode(*atom_to_node_[const_cast<Atom*>(&atom)]);
}
template <typename Node, typename Edge>
bool TSimpleMolecularGraph<Node, Edge>::deleteEdge(const Bond& bond)
{
if (!bond_to_edge_.has(const_cast<Bond*>(&bond)))
{
return false;
}
return deleteEdge(*bond_to_edge_[const_cast<Bond*>(&bond)]);
}
template <typename Node, typename Edge>
bool TSimpleMolecularGraph<Node, Edge>::deleteNode(typename TSimpleMolecularGraph<Node, Edge>::NodeItemType& node)
{
NodeIterator node_it = std::find(nodes_.begin(), nodes_.end(), node);
if (node_it == nodes_.end())
{
return false;
}
bool remove = true;
while (remove)
{
remove = false;
typename NodeItemType::Iterator edge(node.begin());
for (; edge != node.end(); ++edge)
{
deleteEdge(**edge);
break;
}
}
atom_to_node_.erase(node_it->getAtom());
nodes_.erase(node_it);
return true;
}
template <typename Node, typename Edge>
bool TSimpleMolecularGraph<Node, Edge>::deleteEdge(typename TSimpleMolecularGraph<Node, Edge>::EdgeItemType& edge)
{
typename std::list<EdgeItemType>::iterator edge_it = std::find(edges_.begin(), edges_.end(), edge);
if (edge_it == edges_.end())
{
return false;
}
edge.getSource().deleteEdge_(&edge);
edge.getTarget().deleteEdge_(&edge);
bond_to_edge_.erase(edge_it->getBond());
edges_.erase(edge_it);
return true;
}
template <typename Node, typename Edge>
NodeItem<Node, Edge>::NodeItem()
: atom_(0)
{
}
template <typename Node, typename Edge>
NodeItem<Node, Edge>::NodeItem(const Atom& atom)
: atom_(const_cast<Atom*>(&atom))
{
}
template <typename Node, typename Edge>
Node& NodeItem<Node, Edge>::getData()
{
return data_;
}
template <typename Node, typename Edge>
const Node& NodeItem<Node, Edge>::getData() const
{
return data_;
}
template <typename Node, typename Edge>
void NodeItem<Node, Edge>::setData(const Node& data)
{
data_ = data;
}
template <typename Node, typename Edge>
const Atom* NodeItem<Node, Edge>::getAtom() const
{
return atom_;
}
template <typename Node, typename Edge>
Atom* NodeItem<Node, Edge>::getAtom()
{
return atom_;
}
template <typename Node, typename Edge>
typename NodeItem<Node, Edge>::Iterator NodeItem<Node, Edge>::begin()
{
return adjacent_edges_.begin();
}
template <typename Node, typename Edge>
typename NodeItem<Node, Edge>::ConstIterator NodeItem<Node, Edge>::begin() const
{
return adjacent_edges_.begin();
}
template <typename Node, typename Edge>
typename NodeItem<Node, Edge>::Iterator NodeItem<Node, Edge>::end()
{
return adjacent_edges_.end();
}
template <typename Node, typename Edge>
typename NodeItem<Node, Edge>::ConstIterator NodeItem<Node, Edge>::end() const
{
return adjacent_edges_.end();
}
template <typename Node, typename Edge>
Size NodeItem<Node, Edge>::getDegree() const
{
return (Size)adjacent_edges_.size();
}
template <typename Node, typename Edge>
bool NodeItem<Node, Edge>::operator == (const NodeItem& item) const
{
return (atom_ == item.atom_);
}
template <typename Node, typename Edge>
bool NodeItem<Node, Edge>::operator != (const NodeItem& item) const
{
return (atom_ != item.atom_);
}
template <typename Node, typename Edge>
void NodeItem<Node, Edge>::deleteEdge_(EdgeItemType* item)
{
Iterator it(std::find(adjacent_edges_.begin(), adjacent_edges_.end(), item));
if (it != adjacent_edges_.end())
{
adjacent_edges_.erase(it);
}
}
template <typename Node, typename Edge>
BALL_INLINE
Size TSimpleMolecularGraph<Node, Edge>::getNumberOfNodes() const
{
return nodes_.size();
}
template <typename Node, typename Edge>
BALL_INLINE
Size TSimpleMolecularGraph<Node, Edge>::getNumberOfEdges() const
{
return edges_.size();
}
} // namespace BALL
#endif // BALL_STRUCTURE_MOLECULARGRAPH_H
atom.h
bond.h
BALL::Atom
Definition atom.h:90
BALL::Atom::BondConstIterator
ConstRandomAccessIterator< Atom, Bond, BondIteratorPosition, BondIteratorTraits > BondConstIterator
Constant random access iterator for bonds.
Definition atom.h:904
BALL::Bond
Definition bond.h:57
BALL::Bond::getFirstAtom
const Atom * getFirstAtom() const
Return a pointer to the first atom.
BALL::Bond::getSecondAtom
const Atom * getSecondAtom() const
Return a pointer to the second atom.
BALL::EdgeItem
Definition simpleMolecularGraph.h:94
BALL::EdgeItem::getData
Node & getData()
Definition simpleMolecularGraph.h:113
BALL::EdgeItem::setData
void setData(const Edge &data)
Definition simpleMolecularGraph.h:115
BALL::EdgeItem::ConstIterator
std::list< NodeItem< Node, Edge > * >::const_iterator ConstIterator
Definition simpleMolecularGraph.h:99
BALL::EdgeItem::EdgeItemType
EdgeItem< Node, Edge > EdgeItemType
Definition simpleMolecularGraph.h:97
BALL::EdgeItem::getSource
const NodeItemType & getSource() const
Definition simpleMolecularGraph.h:110
BALL::EdgeItem::getTarget
NodeItemType & getTarget()
Definition simpleMolecularGraph.h:109
BALL::EdgeItem::getSource
NodeItemType & getSource()
Definition simpleMolecularGraph.h:108
BALL::EdgeItem::getTarget
const NodeItemType & getTarget() const
Definition simpleMolecularGraph.h:111
BALL::EdgeItem::source_
NodeItemType * source_
Definition simpleMolecularGraph.h:126
BALL::EdgeItem::operator==
bool operator==(const EdgeItem &item) const
Definition simpleMolecularGraph.h:120
BALL::EdgeItem::getBond
const Bond * getBond() const
Definition simpleMolecularGraph.h:118
BALL::EdgeItem::getBond
Bond * getBond()
Definition simpleMolecularGraph.h:117
BALL::EdgeItem::operator!=
bool operator!=(const EdgeItem &item) const
Definition simpleMolecularGraph.h:121
BALL::EdgeItem::NodeItemType
NodeItem< Node, Edge > NodeItemType
Definition simpleMolecularGraph.h:96
BALL::EdgeItem::getData
const Node & getData() const
Definition simpleMolecularGraph.h:114
BALL::EdgeItem::EdgeItem
EdgeItem()
Definition simpleMolecularGraph.h:101
BALL::EdgeItem::bond_
Bond * bond_
Definition simpleMolecularGraph.h:125
BALL::EdgeItem::data_
Edge data_
Definition simpleMolecularGraph.h:124
BALL::EdgeItem::target_
NodeItemType * target_
Definition simpleMolecularGraph.h:127
BALL::EdgeItem::Iterator
std::list< NodeItem< Node, Edge > * >::iterator Iterator
Definition simpleMolecularGraph.h:98
BALL::HashMap
HashMap class based on the STL map (containing serveral convenience functions).
Definition hashMap.h:74
BALL::Molecule
Definition molecule.h:31
BALL::NodeItem
Definition simpleMolecularGraph.h:42
BALL::NodeItem::getAtom
const Atom * getAtom() const
Definition simpleMolecularGraph.h:417
BALL::NodeItem::operator!=
bool operator!=(const NodeItem &item) const
Definition simpleMolecularGraph.h:473
BALL::NodeItem::getDegree
Size getDegree() const
Definition simpleMolecularGraph.h:459
BALL::NodeItem::getData
Node & getData()
Definition simpleMolecularGraph.h:394
BALL::NodeItem::Iterator
std::list< EdgeItem< Node, Edge > * >::iterator Iterator
Definition simpleMolecularGraph.h:54
BALL::NodeItem::NodeItemType
NodeItem< Node, Edge > NodeItemType
Definition simpleMolecularGraph.h:48
BALL::NodeItem::deleteEdge_
void deleteEdge_(EdgeItemType *item)
Definition simpleMolecularGraph.h:480
BALL::NodeItem::operator==
bool operator==(const NodeItem &item) const
Definition simpleMolecularGraph.h:466
BALL::NodeItem::NodeItem
NodeItem()
Definition simpleMolecularGraph.h:380
BALL::NodeItem::begin
Iterator begin()
Definition simpleMolecularGraph.h:431
BALL::NodeItem::setData
void setData(const Node &data)
Definition simpleMolecularGraph.h:409
BALL::NodeItem::atom_
Atom * atom_
Definition simpleMolecularGraph.h:87
BALL::NodeItem::data_
Node data_
Definition simpleMolecularGraph.h:86
BALL::NodeItem::end
Iterator end()
Definition simpleMolecularGraph.h:445
BALL::NodeItem::adjacent_edges_
std::list< EdgeItemType * > adjacent_edges_
Definition simpleMolecularGraph.h:88
BALL::NodeItem::EdgeItemType
EdgeItem< Node, Edge > EdgeItemType
Definition simpleMolecularGraph.h:51
BALL::NodeItem::ConstIterator
std::list< EdgeItem< Node, Edge > * >::const_iterator ConstIterator
Definition simpleMolecularGraph.h:56
BALL::TSimpleMolecularGraph
Definition simpleMolecularGraph.h:146
BALL::TSimpleMolecularGraph::NodeItemType
NodeItem< Node, Edge > NodeItemType
Definition simpleMolecularGraph.h:148
BALL::TSimpleMolecularGraph::has
bool has(const Bond &bond) const
Definition simpleMolecularGraph.h:177
BALL::TSimpleMolecularGraph::EdgeConstIterator
std::list< EdgeItemType >::const_iterator EdgeConstIterator
Definition simpleMolecularGraph.h:153
BALL::TSimpleMolecularGraph::deleteNode
bool deleteNode(NodeItemType &node)
BALL::TSimpleMolecularGraph::beginNode
NodeIterator beginNode()
Definition simpleMolecularGraph.h:167
BALL::TSimpleMolecularGraph::endNode
NodeIterator endNode()
Definition simpleMolecularGraph.h:171
BALL::TSimpleMolecularGraph::has
bool has(const Atom &atom) const
Definition simpleMolecularGraph.h:176
BALL::TSimpleMolecularGraph::NodeIterator
std::list< NodeItemType >::iterator NodeIterator
Definition simpleMolecularGraph.h:150
BALL::TSimpleMolecularGraph< Index, Index >::bond_to_edge_
HashMap< Bond *, EdgeItemType * > bond_to_edge_
Definition simpleMolecularGraph.h:196
BALL::TSimpleMolecularGraph< Index, Index >::atom_to_node_
HashMap< Atom *, NodeItemType * > atom_to_node_
Definition simpleMolecularGraph.h:195
BALL::TSimpleMolecularGraph::NodeConstIterator
std::list< NodeItemType >::const_iterator NodeConstIterator
Definition simpleMolecularGraph.h:151
BALL::TSimpleMolecularGraph::getNumberOfEdges
Size getNumberOfEdges() const
Definition simpleMolecularGraph.h:500
BALL::TSimpleMolecularGraph::beginNode
NodeConstIterator beginNode() const
Definition simpleMolecularGraph.h:168
BALL::TSimpleMolecularGraph::getNode
const NodeItemType & getNode(Position index) const
Definition simpleMolecularGraph.h:180
BALL::TSimpleMolecularGraph::getNode
NodeItemType & getNode(Position index)
Definition simpleMolecularGraph.h:179
BALL::TSimpleMolecularGraph::newNode
bool newNode(const Atom &atom)
Definition simpleMolecularGraph.h:241
BALL::TSimpleMolecularGraph::getNumberOfNodes
Size getNumberOfNodes() const
Definition simpleMolecularGraph.h:492
BALL::TSimpleMolecularGraph::getEdge
EdgeItemType & getEdge(Position index)
Definition simpleMolecularGraph.h:181
BALL::TSimpleMolecularGraph::deleteEdge
bool deleteEdge(EdgeItemType &edge)
BALL::TSimpleMolecularGraph::EdgeIterator
std::list< EdgeItemType >::iterator EdgeIterator
Definition simpleMolecularGraph.h:152
BALL::TSimpleMolecularGraph::endEdge
EdgeConstIterator endEdge() const
Definition simpleMolecularGraph.h:174
BALL::TSimpleMolecularGraph< Index, Index >::nodes_
std::list< NodeItemType > nodes_
Definition simpleMolecularGraph.h:193
BALL::TSimpleMolecularGraph::endNode
NodeConstIterator endNode() const
Definition simpleMolecularGraph.h:172
BALL::TSimpleMolecularGraph< Index, Index >::edges_
std::list< EdgeItemType > edges_
Definition simpleMolecularGraph.h:194
BALL::TSimpleMolecularGraph::beginEdge
EdgeIterator beginEdge()
Definition simpleMolecularGraph.h:169
BALL::TSimpleMolecularGraph::deleteNode
bool deleteNode(const Atom &atom)
Definition simpleMolecularGraph.h:314
BALL::TSimpleMolecularGraph::TSimpleMolecularGraph
TSimpleMolecularGraph(const Molecule &molecule)
Definition simpleMolecularGraph.h:215
BALL::TSimpleMolecularGraph::endEdge
EdgeIterator endEdge()
Definition simpleMolecularGraph.h:173
BALL::TSimpleMolecularGraph::beginEdge
EdgeConstIterator beginEdge() const
Definition simpleMolecularGraph.h:170
BALL::TSimpleMolecularGraph::getEdge
const EdgeItemType & getEdge(Position index) const
Definition simpleMolecularGraph.h:182
BALL::TSimpleMolecularGraph::EdgeItemType
EdgeItem< Node, Edge > EdgeItemType
Definition simpleMolecularGraph.h:149
BALL::TSimpleMolecularGraph::deleteEdge
bool deleteEdge(const Bond &bond)
Definition simpleMolecularGraph.h:325
BALL::TSimpleMolecularGraph::TSimpleMolecularGraph
TSimpleMolecularGraph()
Definition simpleMolecularGraph.h:205
BALL::TSimpleMolecularGraph::newEdge
bool newEdge(const Bond &bond)
Definition simpleMolecularGraph.h:258
BALL_INLINE
#define BALL_INLINE
Definition debug.h:15
fragment.h
molecule.h
BALL
Definition constants.h:13
BALL::Size
BALL_SIZE_TYPE Size
Definition COMMON/global.h:114
BALL::AtomConstIterator
ConstBidirectionalIterator< Composite, Atom, Composite::CompositeIterator, AtomIteratorTraits > AtomConstIterator
Definition atomIterator.h:90
BALL::Position
BALL_SIZE_TYPE Position
Definition COMMON/global.h:140
BALL::SimpleMolecularGraph
TSimpleMolecularGraph< Index, Index > SimpleMolecularGraph
Definition simpleMolecularGraph.h:202