/*============================================================================= Copyright (c) 2001-2003 Joel de Guzman http://spirit.sourceforge.net/ Use, modification and distribution is subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) =============================================================================*/ #ifndef BOOST_XPRESSIVE_SPIRIT_RANGE_RUN_IPP #define BOOST_XPRESSIVE_SPIRIT_RANGE_RUN_IPP /////////////////////////////////////////////////////////////////////////////// #include // for std::lower_bound #include #include #include /////////////////////////////////////////////////////////////////////////////// namespace boost { namespace xpressive { namespace detail { /////////////////////////////////////////////////////////////////////// // // range class implementation // /////////////////////////////////////////////////////////////////////// template inline range::range(Char first, Char last) : first_(first) , last_(last) { } ////////////////////////////////// template inline bool range::is_valid() const { return this->first_ <= this->last_; } ////////////////////////////////// template inline bool range::includes(range const &r) const { return (this->first_ <= r.first_) && (this->last_ >= r.last_); } ////////////////////////////////// template inline bool range::includes(Char v) const { return (this->first_ <= v) && (this->last_ >= v); } ////////////////////////////////// template inline bool range::overlaps(range const &r) const { Char decr_first = (std::min)(this->first_, Char(this->first_-1)); Char incr_last = (std::max)(this->last_, Char(this->last_+1)); return (decr_first <= r.last_) && (incr_last >= r.first_); } ////////////////////////////////// template inline void range::merge(range const &r) { this->first_ = (std::min)(this->first_, r.first_); this->last_ = (std::max)(this->last_, r.last_); } /////////////////////////////////////////////////////////////////////// // // range_run class implementation // /////////////////////////////////////////////////////////////////////// template inline bool range_run::empty() const { return this->run_.empty(); } template inline bool range_run::test(Char v) const { if(this->run_.empty()) { return false; } const_iterator iter = std::lower_bound( this->run_.begin() , this->run_.end() , range(v, v) , range_compare() ); return (iter != this->run_.end() && iter->includes(v)) || (iter != this->run_.begin() && (--iter)->includes(v)); } template template inline bool range_run::test(Char v, Traits const &traits) const { const_iterator begin = this->run_.begin(); const_iterator end = this->run_.end(); for(; begin != end; ++begin) { if(traits.in_range_nocase(begin->first_, begin->last_, v)) { return true; } } return false; } ////////////////////////////////// template inline void range_run::swap(range_run &rr) { this->run_.swap(rr.run_); } ////////////////////////////////// template void range_run::merge(iterator iter, range const &r) { BOOST_ASSERT(iter != this->run_.end()); iter->merge(r); iterator i = iter; while(++i != this->run_.end() && iter->overlaps(*i)) { iter->merge(*i); } this->run_.erase(++iter, i); } ////////////////////////////////// template void range_run::set(range const &r) { BOOST_ASSERT(r.is_valid()); if(!this->run_.empty()) { iterator iter = std::lower_bound(this->run_.begin(), this->run_.end(), r, range_compare()); if((iter != this->run_.end() && iter->includes(r)) || (iter != this->run_.begin() && (iter - 1)->includes(r))) { return; } else if(iter != this->run_.begin() && (iter - 1)->overlaps(r)) { this->merge(--iter, r); } else if(iter != this->run_.end() && iter->overlaps(r)) { this->merge(iter, r); } else { this->run_.insert(iter, r); } } else { this->run_.push_back(r); } } ////////////////////////////////// template void range_run::clear(range const &r) { BOOST_ASSERT(r.is_valid()); if(!this->run_.empty()) { iterator iter = std::lower_bound(this->run_.begin(), this->run_.end(), r, range_compare()); iterator left_iter; if((iter != this->run_.begin()) && (left_iter = (iter - 1))->includes(r.first_)) { if(left_iter->last_ > r.last_) { Char save_last = left_iter->last_; left_iter->last_ = r.first_-1; this->run_.insert(iter, range(r.last_+1, save_last)); return; } else { left_iter->last_ = r.first_-1; } } iterator i = iter; for(; i != this->run_.end() && r.includes(*i); ++i) {} if(i != this->run_.end() && i->includes(r.last_)) { i->first_ = r.last_+1; } this->run_.erase(iter, i); } } ////////////////////////////////// template inline void range_run::clear() { this->run_.clear(); } ////////////////////////////////// template inline typename range_run::const_iterator range_run::begin() const { return this->run_.begin(); } ////////////////////////////////// template inline typename range_run::const_iterator range_run::end() const { return this->run_.end(); } }}} // namespace boost::xpressive::detail #endif