| 1 | // =============================================================== // |
|---|
| 2 | // // |
|---|
| 3 | // File : SEC_drawn_pos.hxx // |
|---|
| 4 | // Purpose : store all drawn positions // |
|---|
| 5 | // // |
|---|
| 6 | // Coded by Ralf Westram (coder@reallysoft.de) in August 2007 // |
|---|
| 7 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 8 | // http://www.arb-home.de/ // |
|---|
| 9 | // // |
|---|
| 10 | // =============================================================== // |
|---|
| 11 | |
|---|
| 12 | #ifndef SEC_DRAWN_POS_HXX |
|---|
| 13 | #define SEC_DRAWN_POS_HXX |
|---|
| 14 | |
|---|
| 15 | #ifndef _GLIBCXX_MAP |
|---|
| 16 | #include <map> |
|---|
| 17 | #endif |
|---|
| 18 | #ifndef AW_POSITION_HXX |
|---|
| 19 | #include <aw_position.hxx> |
|---|
| 20 | #endif |
|---|
| 21 | |
|---|
| 22 | using namespace AW; |
|---|
| 23 | |
|---|
| 24 | typedef std::map<size_t, Position> PosMap; |
|---|
| 25 | |
|---|
| 26 | class SEC_drawn_positions : virtual Noncopyable { |
|---|
| 27 | PosMap drawnAt; |
|---|
| 28 | |
|---|
| 29 | public: |
|---|
| 30 | void clear() { drawnAt.clear(); } |
|---|
| 31 | void announce(size_t abs, const Position& drawn) { drawnAt[abs] = drawn; } |
|---|
| 32 | |
|---|
| 33 | bool empty() const { return drawnAt.empty(); } |
|---|
| 34 | const PosMap::const_iterator begin() const { return drawnAt.begin(); } |
|---|
| 35 | const PosMap::const_iterator end() const { return drawnAt.end(); } |
|---|
| 36 | |
|---|
| 37 | const Position *drawn_at(size_t abs) const { |
|---|
| 38 | PosMap::const_iterator found = drawnAt.find(abs); |
|---|
| 39 | return (found == drawnAt.end()) ? NULp : &(found->second); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | const Position& drawn_before(size_t abspos, size_t *before_abs) const { |
|---|
| 43 | sec_assert(!empty()); |
|---|
| 44 | PosMap::const_iterator found = drawnAt.lower_bound(abspos); // first position which is >= abspos |
|---|
| 45 | |
|---|
| 46 | if (found == drawnAt.begin()) { // no position drawn before abspos |
|---|
| 47 | found = drawnAt.end(); // wrap to (beyond) end |
|---|
| 48 | } |
|---|
| 49 | advance(found, -1); // now just use position before (will be last map position when found==.end()) |
|---|
| 50 | sec_assert(found != drawnAt.end()); |
|---|
| 51 | |
|---|
| 52 | if (before_abs) *before_abs = found->first; |
|---|
| 53 | return *&found->second; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | const Position& drawn_after(size_t abspos, size_t *after_abs) const { |
|---|
| 57 | sec_assert(!empty()); |
|---|
| 58 | PosMap::const_iterator found = drawnAt.upper_bound(abspos); // first pos which is > abspos |
|---|
| 59 | |
|---|
| 60 | if (found == drawnAt.end()) { // no position drawn behind abs |
|---|
| 61 | found = drawnAt.begin(); // wrap to start |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | if (after_abs) *after_abs = found->first; |
|---|
| 65 | return *&found->second; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | const Position& drawn_at_or_after(size_t abspos) const { |
|---|
| 69 | const Position *at = drawn_at(abspos); |
|---|
| 70 | return at ? *at : drawn_after(abspos, NULp); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | }; |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | #else |
|---|
| 77 | #error SEC_drawn_pos.hxx included twice |
|---|
| 78 | #endif // SEC_DRAWN_POS_HXX |
|---|