27 #ifndef MLN_CORE_SITE_SET_P_KEY_HH
28 # define MLN_CORE_SITE_SET_P_KEY_HH
37 # include <mln/core/concept/function.hh>
38 # include <mln/core/site_set/p_set.hh>
39 # include <mln/core/site_set/p_double.hh>
40 # include <mln/core/internal/site_set_base.hh>
41 # include <mln/util/ord.hh>
48 template <
typename K,
typename P>
class p_key;
55 template <
typename K,
typename P>
56 struct site_set_< p_key<K,P> >
58 typedef trait::site_set::nsites::known
nsites;
59 typedef trait::site_set::bbox::unknown
bbox;
60 typedef trait::site_set::contents::growing
contents;
61 typedef trait::site_set::arity::unique
arity;
72 template <
typename K,
typename P>
108 bool has(
const P&
p)
const;
125 void insert(
const K& k,
const P&
p);
129 void unsafe_insert_(
const K& k,
const P&
p);
138 void remove(
const P&
p);
147 template <
typename F>
162 const K&
key(
const P&
p)
const;
191 typedef std::map<P, K, util::ord<P> >
k_t;
203 template <
typename K,
typename P>
204 std::ostream& operator<<(std::ostream& ostr, const p_key<K,P>& pk);
208 # ifndef MLN_INCLUDE_ONLY
210 template <
typename K,
typename P>
215 mln_invariant(run_());
218 template <
typename K,
typename P>
223 mln_invariant(run_());
228 template <
typename K,
typename P>
233 mln_invariant(run_());
234 return k_.find(p) != k_.end();
237 template <
typename K,
typename P>
242 mln_invariant(run_());
246 template <
typename K,
typename P>
251 mln_invariant(run_());
256 template <
typename K,
typename P>
259 p_key<K,P>::unsafe_insert_(
const K& k,
const P& p)
265 mln_invariant(run_());
269 template <
typename K,
typename P>
274 mln_invariant(run_());
275 typename k_t::iterator p_k = k_.find(p);
279 K p_key = p_k->second;
280 mln_assertion(b_.has(p_key));
281 mln_assertion(s_[p_key].
has(p));
299 mln_invariant(run_());
302 template <
typename K,
typename P>
307 this->insert(k_p.first, k_p.second);
310 template <
typename K,
typename P>
315 mln_invariant(run_());
316 typename k_t::iterator p_k = k_.find(p);
322 K p_key = p_k->second;
323 mln_assertion(b_.has(p_key));
329 typename s_t::iterator k_s = s_.find(p_key);
330 mln_assertion(k_s != s_.end());
331 p_set<P>& s = k_s->second;
332 mln_assertion(s.has(p));
349 mln_invariant(run_());
352 template <
typename K,
typename P>
357 mln_invariant(run_());
358 typename s_t::iterator k_s = s_.find(k);
368 p_set<P>& s = k_s->second;
369 mln_piter(p_set<P>) p(s);
379 mln_invariant(run_());
383 template <typename K, typename P>
386 p_key<K,P>::change_key(const K& k, const K& new_k)
388 mln_invariant(run_());
394 typename s_t::iterator k_s = s_.find(k);
404 p_set<P>& s = k_s->second;
405 if (s.nsites() < n_ / 10)
408 mln_piter(p_set<P>) p(s);
415 typename k_t::iterator p_k;
416 for (p_k = k_.begin(); p_k != k_.end(); ++p_k)
417 if (p_k->second == k)
425 mln_invariant(run_());
428 template <
typename K,
typename P>
429 template <
typename F>
434 mln_invariant(run_());
436 const F& f =
exact(f_);
442 mln_eiter(util::set<K>) k(b_);
444 new_b.insert(lut[k] = f(k));
451 typename k_t::iterator p_k;
452 for (p_k = k_.begin(); p_k != k_.end(); ++p_k)
454 p_k->second = lut[p_k->second];
455 new_s[p_k->second].insert(p_k->first);
460 mln_invariant(run_());
463 template <
typename K,
typename P>
468 mln_invariant(run_());
473 mln_invariant(run_());
476 template <
typename K,
typename P>
481 mln_invariant(run_());
490 template <
typename K,
typename P>
495 static const p_set<P> nil_ = p_set<P>();
497 return s_.find(key)->second;
502 template <
typename K,
typename P>
507 mln_invariant(run_());
508 mln_precondition(k_.find(p) != k_.end());
509 return k_.find(p)->second;
512 template <
typename K,
typename P>
517 mln_invariant(run_());
521 template <
typename K,
typename P>
526 mln_invariant(run_());
530 template <
typename K,
typename P>
533 p_key<K,P>::set_1_()
const
538 template <
typename K,
typename P>
541 p_key<K,P>::set_2_(
const K& key)
const
543 mln_precondition(b_.has(key));
544 return s_.find(key)->second;
547 template <
typename K,
typename P>
552 if (!
implies(n_ == 0, b_.is_empty()))
554 std::cerr <<
"#1" << std::endl;
558 if (b_.nelements() != s_.size())
561 std::cerr <<
"#2" << std::endl;
568 std::cerr <<
"#3: k_.size=" << k_.size() <<
" n_=" << n_ << std::endl;
573 mln_eiter(util::set<K>) key(b_);
576 typename s_t::const_iterator k_s = s_.find(key);
581 std::cerr <<
"#4" << std::endl;
585 const p_set<P>& s = k_s->second;
590 std::cerr <<
"#5" << std::endl;
596 mln_piter(p_set<P>) p(s);
599 typename k_t::const_iterator p_k = k_.find(p);
604 std::cerr <<
"#6" << std::endl;
608 if (p_k->second != key)
611 std::cerr <<
"#7" << std::endl;
620 std::cerr <<
"#8" << std::endl;
633 template <
typename K,
typename P>
634 std::ostream& operator<<(std::ostream& ostr, const p_key<K,P>& pk)
637 mln_fwd_eiter(util::set<K>) k(pk.keys());
640 ostr <<
' ' << k <<
':';
641 ostr << pk.set_2_(k);
647 # endif // ! MLN_INCLUDE_ONLY
652 #endif // ! MLN_CORE_SITE_SET_P_KEY_HH