| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #ifndef GUARD_DB_THREADED_HH |
|---|
| 21 | #define GUARD_DB_THREADED_HH |
|---|
| 22 | |
|---|
| 23 | #include "group.hh" |
|---|
| 24 | #include "msg.hh" |
|---|
| 25 | |
|---|
| 26 | #include "../tlate/tlate.hh" |
|---|
| 27 | |
|---|
| 28 | #include <boost/enable_shared_from_this.hpp> |
|---|
| 29 | #include <boost/noncopyable.hpp> |
|---|
| 30 | #include <list> |
|---|
| 31 | #include <map> |
|---|
| 32 | |
|---|
| 33 | namespace db |
|---|
| 34 | { |
|---|
| 35 | class thread_node : public boost::enable_shared_from_this<thread_node> |
|---|
| 36 | { |
|---|
| 37 | public: |
|---|
| 38 | typedef boost::shared_ptr<thread_node> ptr; |
|---|
| 39 | typedef boost::shared_ptr<const thread_node> const_ptr; |
|---|
| 40 | private: |
|---|
| 41 | std::map<std::string, thread_node::ptr> &msgids; |
|---|
| 42 | std::string msgid; |
|---|
| 43 | group::number grinx; |
|---|
| 44 | boost::shared_ptr<msg> art; |
|---|
| 45 | boost::posix_time::ptime my_date, latest_date; |
|---|
| 46 | std::list<ptr> children; |
|---|
| 47 | ptr parent; |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | std::list<ptr>::iterator sibling_iterator; |
|---|
| 51 | |
|---|
| 52 | void set_parent(ptr); |
|---|
| 53 | void disown_children(); |
|---|
| 54 | void recalc_latest_date(); |
|---|
| 55 | |
|---|
| 56 | ptr restrict_to_group(boost::shared_ptr<group> gr, |
|---|
| 57 | ptr parent) const; |
|---|
| 58 | friend class threaded; |
|---|
| 59 | public: |
|---|
| 60 | thread_node(std::map<std::string, thread_node::ptr> &msgids, |
|---|
| 61 | std::string msgid) |
|---|
| 62 | : msgids(msgids) |
|---|
| 63 | , msgid(msgid) |
|---|
| 64 | , grinx(-1) |
|---|
| 65 | , my_date(boost::posix_time::neg_infin) |
|---|
| 66 | , latest_date(boost::posix_time::neg_infin) |
|---|
| 67 | {} |
|---|
| 68 | |
|---|
| 69 | ptr previous() const; |
|---|
| 70 | ptr next() const; |
|---|
| 71 | |
|---|
| 72 | boost::posix_time::ptime get_latest_date() const { |
|---|
| 73 | return latest_date; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | std::string get_msgid() const { return msgid; } |
|---|
| 77 | |
|---|
| 78 | void add_msg(group::number grinx, |
|---|
| 79 | boost::shared_ptr<msg> art); |
|---|
| 80 | void prune(); |
|---|
| 81 | bool has_descendant(ptr); |
|---|
| 82 | bool is_empty() { return !art && children.empty(); } |
|---|
| 83 | ptr get_principal(); |
|---|
| 84 | |
|---|
| 85 | size_t cardinality() const; |
|---|
| 86 | |
|---|
| 87 | ptr get_parent() const { |
|---|
| 88 | if (parent && parent->msgid.empty()) return ptr(); |
|---|
| 89 | return parent; |
|---|
| 90 | } |
|---|
| 91 | const std::list<ptr> &get_children() const { return children; } |
|---|
| 92 | boost::shared_ptr<msg> get_article() const { return art; } |
|---|
| 93 | template <typename Cmp> |
|---|
| 94 | void sort_children(Cmp cmp) { |
|---|
| 95 | children.sort(cmp); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | template <typename Cmp> |
|---|
| 99 | void sort_children_recursive(Cmp cmp) { |
|---|
| 100 | sort_children(cmp); |
|---|
| 101 | for (std::list<ptr>::iterator it |
|---|
| 102 | = children.begin(); |
|---|
| 103 | it != children.end(); it++) |
|---|
| 104 | { |
|---|
| 105 | (*it)->sibling_iterator = it; |
|---|
| 106 | (*it)->sort_children_recursive(cmp); |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | static bool cmp_date_rev(thread_node::const_ptr a, |
|---|
| 111 | thread_node::const_ptr b) { |
|---|
| 112 | return a != b && !cmp_date(a,b); |
|---|
| 113 | } |
|---|
| 114 | static bool cmp_latest_date_rev(thread_node::const_ptr a, |
|---|
| 115 | thread_node::const_ptr b) { |
|---|
| 116 | return a != b && !cmp_latest_date(a,b); |
|---|
| 117 | } |
|---|
| 118 | static bool cmp_date(thread_node::const_ptr a, |
|---|
| 119 | thread_node::const_ptr b) { |
|---|
| 120 | if (!a->my_date.is_not_a_date_time() && |
|---|
| 121 | !b->my_date.is_not_a_date_time() ) |
|---|
| 122 | { |
|---|
| 123 | return a->my_date < b->my_date; |
|---|
| 124 | } |
|---|
| 125 | return a->grinx < b->grinx; |
|---|
| 126 | } |
|---|
| 127 | static bool cmp_latest_date(thread_node::const_ptr a, |
|---|
| 128 | thread_node::const_ptr b) { |
|---|
| 129 | boost::posix_time::ptime ad = a->latest_date; |
|---|
| 130 | boost::posix_time::ptime bd = b->latest_date; |
|---|
| 131 | if (!ad.is_neg_infinity() && |
|---|
| 132 | !bd.is_neg_infinity()) |
|---|
| 133 | { |
|---|
| 134 | return ad < bd; |
|---|
| 135 | } |
|---|
| 136 | return a->grinx < b->grinx; |
|---|
| 137 | } |
|---|
| 138 | }; |
|---|
| 139 | |
|---|
| 140 | class threaded : public boost::noncopyable |
|---|
| 141 | { |
|---|
| 142 | public: |
|---|
| 143 | threaded() |
|---|
| 144 | : root(new thread_node(msgids, "")) |
|---|
| 145 | { msgids[""] = root; } |
|---|
| 146 | |
|---|
| 147 | void add_msg(group::number grinx, |
|---|
| 148 | boost::shared_ptr<msg> art); |
|---|
| 149 | const std::list<thread_node::ptr> &get_threads() const |
|---|
| 150 | { return root->get_children(); } |
|---|
| 151 | const std::list<thread_node::ptr> &get_threads |
|---|
| 152 | (boost::shared_ptr<group> gr) const; |
|---|
| 153 | |
|---|
| 154 | thread_node::ptr lookup_msgid(std::string s) const { |
|---|
| 155 | |
|---|
| 156 | if (s.empty()) return thread_node::ptr(); |
|---|
| 157 | |
|---|
| 158 | std::map<std::string, thread_node::ptr>::const_iterator |
|---|
| 159 | it = msgids.find(s); |
|---|
| 160 | if (it == msgids.end()) return thread_node::ptr(); |
|---|
| 161 | return it->second; |
|---|
| 162 | } |
|---|
| 163 | void recalc_roots(); |
|---|
| 164 | void sort(); |
|---|
| 165 | |
|---|
| 166 | private: |
|---|
| 167 | std::map<std::string, thread_node::ptr> msgids; |
|---|
| 168 | thread_node::ptr root; |
|---|
| 169 | |
|---|
| 170 | static const std::list<thread_node::ptr> empty_list; |
|---|
| 171 | }; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | #endif |
|---|