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

📄 symbols.hpp

📁 著名的Parser库Spirit在VC6上的Port
💻 HPP
📖 第 1 页 / 共 2 页
字号:
/*=============================================================================
    Symbol Tables

    Spirit V1.3.1
    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_SYMBOLS_HPP
#define SPIRIT_SYMBOLS_HPP

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

#include "boost/spirit/MSVC/parser.hpp"
#include <memory>

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

///////////////////////////////////////////////////////////////////////////////
//  Forward Declarations

template <typename CharT, typename T> class tst;
template <typename T> class symbol_match;
template <typename MatchTraitsT, typename T> class ast_symbol_match;
template <typename T, typename SetT> class symbol_inserter;
template <typename ParserT, typename ActionT> class symbol_action;
///////////////////////////////////////////////////////////////////////////////
//
//  tst class
//
//      Ternary Search Tree implementation. This is the default
//      implementation for string sets. The data structure is faster
//      than hashing for many typical search problems especially when
//      the search interface is iterator based. Searching for a string
//      of length k in a ternary search tree with n strings will require
//      at most O(log n+k) character comparisons. TSTs are many times
//      faster than hash tables for unsuccessful searches since
//      mismatches are discovered earlier after examining only a few
//      characters. Hash tables always examine an entire key when
//      searching.
//
//      For details see http://www.cs.princeton.edu/~rs/strings/.
//
///////////////////////////////////////////////////////////////////////////////
template <typename T, typename CharT>
class tst {

public:

    struct search_info {

        T*          data;
        unsigned    length;
    };

    tst();
    tst(tst const& other);
    ~tst();

    tst&
    operator=(tst const& other);

    template <typename IteratorT>
    T*
    add(IteratorT first, IteratorT const& last, T const& data)
    {
        if (first == last)
            return 0;
    
        node**  np = &root;
        CharT   ch = *first;
    
        while (true)
        {
            if (*np == 0 || ch == 0)
            {
                node* right = 0;
                if (np != 0)
                    right = *np;
                *np = new node(ch);
                if (right)
                    (**np).right = right;
            }
    
            if (ch < (**np).value)
            {
                np = &(**np).left;
            }
            else
            {
                if (ch == (**np).value)
                {
                    if (ch == 0)
                    {
                        if ((**np).middle.data == 0)
                        {
                            (**np).middle.data = new T(data);
                            return (**np).middle.data;
                        }
                        else
                        {
                            //  re-addition is disallowed
                            return 0;
                        }
                   }
                    ++first;
                    ch = (first == last) ? 0 : *first;
                    np = &(**np).middle.link;
                }
                else
                {
                    np = &(**np).right;
                }
            }
        }
    }


    template <typename IteratorT>
    search_info
        find(IteratorT& first, IteratorT const& last) const
    {
        node*       np = root;
        CharT       ch = *first;
        IteratorT   save = first;
        search_info result = { 0, 0 };
    
        while (np)
        {
            if (ch < np->value)
            {
                if (np->value == 0)
                    result.data = np->middle.data;
                np = np->left;
            }
            else
            {
                if (ch == np->value)
                {
                    if (first == last)
                    {
                        result.data = np->middle.data;
                        break;
                    }
                    ch = *(++first);
                    np = np->middle.link;
                    ++result.length;
                }
                else
                {
                    if (np->value == 0)
                        result.data = np->middle.data;
                    np = np->right;
                }
            }
        }
    
        if (result.data == 0)
            first = save;
        return result;
    }

private:

    struct node
    {
       node(CharT value_)
       :  value(value_),
         left(0),
         right(0)
       {
           middle.link = 0;
       }

       //////////////////////////////////
      ~node()
      {
        delete left;
        delete right;
        if (value)
            delete middle.link;
        else
            delete middle.data;
      }


       node*
       clone() const
       {
         std::auto_ptr<node> copy(new node(value));

         if (left)
             copy->left = left->clone();
         if (right)
            copy->right = right->clone();
         if (value && middle.link)
            copy->middle.link = middle.link->clone();
         else
             copy->middle.data = new T(*middle.data);

         return copy.release();
       }


      union center {

          node*   link;
          T*      data;
       };

       CharT   value;
       node*   left;
       center  middle;
       node*   right;
     };
  

    node* root;
};


///////////////////////////////////////////////////////////////////////////////
//
//  symbols class

⌨️ 快捷键说明

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