71 #ifndef __TPIE_PIPELINING_TOKENS_H__
72 #define __TPIE_PIPELINING_TOKENS_H__
75 #include <tpie/pipelining/exception.h>
81 #include <boost/shared_ptr.hpp>
82 #include <boost/weak_ptr.hpp>
86 namespace pipelining {
98 typedef uint64_t id_t;
101 typedef std::map<id_t, val_t> map_t;
102 typedef map_t::const_iterator mapit;
104 typedef std::multimap<id_t, std::pair<id_t, node_relation> > relmap_t;
105 typedef relmap_t::const_iterator relmapit;
107 typedef boost::shared_ptr<node_map> ptr;
108 typedef boost::weak_ptr<node_map> wptr;
110 static inline ptr create() {
112 result->self = wptr(result);
116 inline id_t add_token(
val_t token) {
118 set_token(
id, token);
122 inline void set_token(id_t
id,
val_t token) {
123 assert_authoritative();
124 m_tokens[id] = token;
128 void link(ptr target);
130 inline void union_set(ptr target) {
131 find_authority()->link(target->find_authority());
134 inline val_t get(id_t id)
const {
135 mapit i = m_tokens.find(
id);
136 if (i == m_tokens.end())
return 0;
140 inline mapit begin()
const {
141 return m_tokens.begin();
144 inline mapit end()
const {
145 return m_tokens.end();
149 ptr find_authority();
151 inline void add_relation(id_t from, id_t to, node_relation rel) {
152 m_relations.insert(std::make_pair(from, std::make_pair(to, rel)));
153 m_relationsInv.insert(std::make_pair(to, std::make_pair(from, rel)));
156 inline const relmap_t & get_relations()
const {
160 inline size_t in_degree(id_t from, node_relation rel)
const {
161 return out_degree(m_relationsInv, from, rel);
164 inline size_t out_degree(id_t from, node_relation rel)
const {
165 return out_degree(m_relations, from, rel);
168 void assert_authoritative()
const {
172 void dump(std::ostream & os = std::cout)
const;
182 relmap_t m_relations;
183 relmap_t m_relationsInv;
185 size_t out_degree(
const relmap_t & map, id_t from, node_relation rel)
const;
208 typedef bits::node_map::id_t id_t;
213 : m_tokens(bits::node_map::create())
214 , m_id(m_tokens->add_token(owner))
223 : m_tokens(other.m_tokens->find_authority())
229 throw exception(
"Trying to take ownership of a non-free token");
230 if (m_tokens->get(m_id) != 0)
231 throw exception(
"A token already has an owner, but m_free is true - contradiction");
234 throw exception(
"Trying to copy a free token");
236 m_tokens->set_token(m_id, newOwner);
241 : m_tokens(bits::node_map::create())
242 , m_id(m_tokens->add_token(0))
247 inline id_t id()
const {
return m_id; }
249 inline bits::node_map::ptr map_union(
const node_token & with) {
250 if (m_tokens == with.m_tokens)
return m_tokens;
251 m_tokens->union_set(with.m_tokens);
252 return m_tokens = m_tokens->find_authority();
255 inline bits::node_map::ptr get_map()
const {
259 inline val_t get()
const {
260 return m_tokens->get(m_id);
264 bits::node_map::ptr m_tokens;
273 #endif // __TPIE_PIPELINING_TOKENS_H__
Defines the tp_assert macro.
Predeclarations of some pipelining classes.
void send_successors() const
Internal method called by graph_traits.