⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chset.ipp

📁 著名的Parser库Spirit在VC6上的Port
💻 IPP
📖 第 1 页 / 共 3 页
字号:
/*=============================================================================
    Character set implementation

    Spirit V1.2
    Copyright (c) 2001, Joel de Guzman

    This software is provided 'as-is', without any express or implied
    warranty. In no event will the copyright holder be held liable for
    any damages arising from the use of this software.

    Permission is granted to anyone to use this software for any purpose,
    including commercial applications, and to alter it and redistribute
    it freely, subject to the following restrictions:

    1.  The origin of this software must not be misrepresented; you must
        not claim that you wrote the original software. If you use this
        software in a product, an acknowledgment in the product documentation
        would be appreciated but is not required.

    2.  Altered source versions must be plainly marked as such, and must
        not be misrepresented as being the original software.

    3.  This notice may not be removed or altered from any source
        distribution.

    Acknowledgements:

        Special thanks to Dan Nuffer, John (EBo) David, Chris Uzdavinis,
        and Doug Gregor. These people are most instrumental in steering
        Spirit in the right direction.

        Special thanks also to people who have contributed to the code base
        and sample code, ported Spirit to various platforms and compilers,
        gave suggestions, reported and provided bug fixes. Alexander
        Hirner, Andy Elvey, Bogdan Kushnir, Brett Calcott, Bruce Florman,
        Changzhe Han, Colin McPhail, Hakki Dogusan, Jan Bares, Joseph
        Smith, Martijn W. van der Lee, Raghavendra Satish, Remi Delcos, Tom
        Spilman, Vladimir Prus, W. Scott Dillman, David A. Greene, Bob
        Bailey, Hartmut Kaiser.

        Finally special thanks also to people who gave feedback and
        valuable comments, particularly members of Spirit's Source Forge
        mailing list and boost.org.

    URL: http://spirit.sourceforge.net/

=============================================================================*/
#ifndef SPIRIT_CHSET_IPP
#define SPIRIT_CHSET_IPP

///////////////////////////////////////////////////////////////////////////////

#include <cassert>
#include <vector>
#include <algorithm>
#include <functional>
#include "boost/limits.hpp"
#include "boost/spirit/chset.hpp"

///////////////////////////////////////////////////////////////////////////////
namespace spirit {

namespace impl {

///////////////////////////////////////////////////////////////////////////////
struct chset_converter;

///////////////////////////////////////////////////////////////////////////////
//
//  range class
//
//      Implements a closed range of values. This class is used in
//      the implementation of the range_run class.
//
///////////////////////////////////////////////////////////////////////////////
template <typename CharT>
struct range {
                    range(CharT first, CharT last);

    bool            is_valid() const;
    bool            includes(CharT v) const;
    bool            includes(range const& r) const;
    bool            is_adjacent(range const& r) const;
    void            merge(range const& r);

    bool            operator<(range const& r) const;
    bool            operator<(CharT v) const;

    CharT first, last;
};

///////////////////////////////////////////////////////////////////////////////
//
//  range_run
//
//      An implementation of a sparse bit (boolean) set. The set uses a
//      sorted vector of disjoint ranges. This class implements the bare
//      minimum essentials from which the full range of set operators
//      can be implemented. The set is constructed from ranges.
//      Internally, adjacent or overlapping ranges are coalesced.
//
//      range_runs are very space-economical in situations where there
//      are lots of ranges and a few individual disjoint values.
//      Searching is O(log n) where n is the number of ranges.
//
///////////////////////////////////////////////////////////////////////////////
template <typename CharT>
class range_run {

public:

    typedef typename std::vector<range<CharT> >::iterator iterator;
    typedef typename std::vector<range<CharT> >::const_iterator const_iterator;

    bool            test(CharT v) const;
    void            set(range<CharT> const& r);
    void            clear(range<CharT> const& r);
    void            clear();

    const_iterator  begin() const;
    const_iterator  end() const;

private:

    void            merge(iterator iter, range<CharT> const& r);

    friend class chset_converter;
    std::vector<range<CharT> > run;
};

///////////////////////////////////////////////////////////////////////////////
//
//  range class implementation
//
///////////////////////////////////////////////////////////////////////////////
template <typename CharT>
inline range<CharT>::range(CharT first_, CharT last_)
: first(first_), last(last_) {}

//////////////////////////////////
template <typename CharT>
inline bool
range<CharT>::is_valid() const
{
    return first <= last;
}

//////////////////////////////////
template <typename CharT>
inline bool
range<CharT>::includes(range const& r) const
{
    return (first <= r.first) && (last >= r.last);
}

//////////////////////////////////
template <typename CharT>
inline bool
range<CharT>::includes(CharT v) const
{
    return (first <= v) && (last >= v);
}

//////////////////////////////////
template <typename CharT>
inline bool
range<CharT>::is_adjacent(range const& r) const
{
    CharT   decr_first = first == std::numeric_limits<CharT>::min() 
                            ? first : first-1;
    CharT   incr_last = last == std::numeric_limits<CharT>::max() 
                            ? last : last+1;

    return ((decr_first <= r.first) && (incr_last >= r.first))
        || ((decr_first <= r.last) && (incr_last >= r.last));
}

//////////////////////////////////
template <typename CharT>
inline void
range<CharT>::merge(range const& r)
{
    first = std::min(first, r.first);
    last = std::max(last, r.last);
}

//////////////////////////////////
template <typename CharT>
inline bool
range<CharT>::operator < (range const& r) const
{
    return first < r.first;
}

//////////////////////////////////
template <typename CharT>
inline bool
range<CharT>::operator < (CharT v) const
{
    return first < v;
}

template <typename CharT>
struct range_char_compare : public std::binary_function<range<CharT>,CharT,bool>
{
    bool operator()(const range<CharT>& x, const CharT y) const
    {
        return x.first < y;
    }
}; 

///////////////////////////////////////////////////////////////////////////////
//
//  range_run class implementation
//
///////////////////////////////////////////////////////////////////////////////
template <typename CharT>
inline bool
range_run<CharT>::test(CharT v) const
{
    if (!run.empty())
    {
        const_iterator iter = std::lower_bound(run.begin(), run.end(), v,
            range_char_compare<CharT>());
        if (iter != run.end() && iter->includes(v))
            return true;
        if (iter != run.begin())
            return (--iter)->includes(v);
    }
    return false;
}

//////////////////////////////////
template <typename CharT>
void
range_run<CharT>::merge(iterator iter, range<CharT> const& r)
{
    iter->merge(r);
    iterator i = iter + 1;

    while (i != run.end() && iter->is_adjacent(*i))
        iter->merge(*i++);

    run.erase(iter+1, i);
}

//////////////////////////////////
template <typename CharT>
void
range_run<CharT>::set(range<CharT> const& r)
{
    assert(r.is_valid());
    if (!run.empty())
    {
        iterator iter = std::lower_bound(run.begin(), run.end(), r);
        if (iter != run.end() && iter->includes(r) ||
            ((iter != run.begin()) && (iter - 1)->includes(r)))
            return;

        if (iter != run.begin() && (iter - 1)->is_adjacent(r))
            merge(--iter, r);

        else if (iter != run.end() && iter->is_adjacent(r))
            merge(iter, r);

        else
            run.insert(iter, r);
    }
    else
    {
        run.push_back(r);
    }
}

//////////////////////////////////
template <typename CharT>
void
range_run<CharT>::clear(range<CharT> const& r)
{
    assert(r.is_valid());
    if (!run.empty())
    {
        iterator iter = std::lower_bound(run.begin(), run.end(), r);
        iterator left_iter;

        if ((iter != run.begin()) && 
                (left_iter = (iter - 1))->includes(r.first))
            if (left_iter->last > r.last)
            {
                CharT save_last = left_iter->last;
                left_iter->last = r.first-1;
                run.insert(iter, range<CharT>(r.last+1, save_last));
                return;
            }
            else
            {
                left_iter->last = r.first-1;
            }

        iterator i = iter;
        while (i != run.end() && r.includes(*i))
            i++;
        if (i != run.end() && i->includes(r.last))
            i->first = r.last+1;
        run.erase(iter, i);
    }
}

//////////////////////////////////
template <typename CharT>
void
range_run<CharT>::clear()
{
    run.clear();
}

//////////////////////////////////
template <typename CharT>
inline typename range_run<CharT>::const_iterator
range_run<CharT>::begin() const
{
    return run.begin();
}

//////////////////////////////////
template <typename CharT>
inline typename range_run<CharT>::const_iterator
range_run<CharT>::end() const
{
    return run.end();
}

///////////////////////////////////////////////////////////////////////////////
//
//  utility functions
//
///////////////////////////////////////////////////////////////////////////////
template <typename CharT>
inline range<CharT> const&
full_range()
{
    static range<CharT> full(std::numeric_limits<CharT>::min(),
        std::numeric_limits<CharT>::max());
    return full;
}

///////////////////////////////////////////////////////////////////////////////
}   //  namespace spirit::impl

///////////////////////////////////////////////////////////////////////////////
//
//  chset::impl class implementation
//
///////////////////////////////////////////////////////////////////////////////
template <typename CharT>
class chset<CharT>::rep {

public:
    typedef typename std::vector<impl::range<CharT> >::iterator iterator;
    typedef typename std::vector<impl::range<CharT> >::const_iterator 
        const_iterator;

                        rep();
                        rep(rep const& arg);

    bool                test(CharT v) const;
    void                set(impl::range<CharT> const& r);
    void                clear(impl::range<CharT> const& r);

    const_iterator      begin() const;
    const_iterator      end() const;

    static rep*         ref(rep* ptr);
    static void         deref(rep* ptr);
    static void         detach(rep*& ptr);
    static void         detach_clear(rep*& ptr);

#ifndef __GNUC__
private:    //  I can't get G++ to make impl::chset_converter a friend
#endif      //  of this class. Complains that rep_of is private.

    rep&                operator=(rep const& rhs);  //  No definition

    static rep*&        rep_of(chset<CharT>& set);
    static rep const*   rep_of(chset<CharT> const& set);

#ifndef __GNUC__
    friend chset<CharT> operator|<>(chset<CharT> const&, chset<CharT> const&);
    friend chset<CharT> operator-<>(chset<CharT> const&, chset<CharT> const&);
    friend class impl::chset_converter;
#endif

    unsigned uc;
    impl::range_run<CharT> rr;
};

//////////////////////////////////
template <typename CharT>
inline chset<CharT>::rep::rep()
:   uc(1)
{
}

//////////////////////////////////
template <typename CharT>
inline chset<CharT>::rep::rep(rep const& arg)
:   uc(1)
,   rr(arg.rr)
{
}

//////////////////////////////////
template <typename CharT>
inline bool
chset<CharT>::rep::test(CharT v) const
{
    return rr.test(v);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -