dbertoni    2002/09/25 18:36:42

  Modified:    c/src/XPath XPathFunctionTable.cpp XPathFunctionTable.hpp
  Log:
  New implementation.
  
  Revision  Changes    Path
  1.22      +83 -138   xml-xalan/c/src/XPath/XPathFunctionTable.cpp
  
  Index: XPathFunctionTable.cpp
  ===================================================================
  RCS file: /home/cvs/xml-xalan/c/src/XPath/XPathFunctionTable.cpp,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- XPathFunctionTable.cpp    25 Sep 2002 06:51:44 -0000      1.21
  +++ XPathFunctionTable.cpp    26 Sep 2002 01:36:42 -0000      1.22
  @@ -98,12 +98,10 @@
   
   
   XPathFunctionTable::XPathFunctionTable(bool          fCreateTable) :
  -     m_FunctionCollection(),
  -     m_FunctionNameIndex(),
        m_functionTable(),
        m_functionTableEnd(m_functionTable + (sizeof(m_functionTable) / 
sizeof(m_functionTable[0])) - 1)
   {
  -     assert(int(s_functionNamesSize) == eTableSize);
  +     assert(int(s_functionNamesSize) == TableSize);
   
   #if defined(XALAN_STRICT_ANSI_HEADERS)
        std::memset(m_functionTable, 0, sizeof(m_functionTable));
  @@ -128,76 +126,67 @@
   
   void
   XPathFunctionTable::InstallFunction(
  -                     const XalanDOMString&   theFunctionName,
  +                     const XalanDOMChar*             theFunctionName,
                        const Function&                 theFunction)
   {
  -     assert(length(theFunctionName) != 0);
  -
  -     // See if a function of that name is already installed...
  -     const FunctionNameIndexMapType::iterator        i =
  -             m_FunctionNameIndex.find(theFunctionName);
  +     const int       theFunctionID =
  +                     getFunctionIndex(theFunctionName);
   
  -     if (i != m_FunctionNameIndex.end())
  +     if (theFunctionID == InvalidFunctionNumberID)
        {
  -             assert(CollectionType::size_type((*i).second) < 
m_FunctionCollection.size());
  -
  -             // It is, so delete the old one, and add the new one...
  -#if defined(XALAN_CANNOT_DELETE_CONST)
  -             delete (Function*)m_FunctionCollection[(*i).second];
  -#else
  -             delete m_FunctionCollection[(*i).second];
  -#endif
  -
  -             m_FunctionCollection[(*i).second] = theFunction.clone();
  +             throw XPathExceptionFunctionNotSupported(theFunctionName);
        }
        else
        {
  -             const CollectionType::size_type         theIndex = 
m_FunctionCollection.size();
  +             if (m_functionTable[theFunctionID] == 0)
  +             {
  +                     m_functionTable[theFunctionID] = theFunction.clone();
  +             }
  +             else
  +             {
  +                     const Function* const   theOldFunction = 
m_functionTable[theFunctionID];
   
  -             m_FunctionCollection.push_back(theFunction.clone());
  +                     m_functionTable[theFunctionID] = theFunction.clone();
   
  -             m_FunctionNameIndex[theFunctionName] = int(theIndex);
  +#if defined(XALAN_CANNOT_DELETE_CONST)
  +                     delete (Function*)theOldFunction;
  +#else
  +                     delete theOldFunction;
  +#endif
  +             }
        }
   }
   
   
   
   bool
  -XPathFunctionTable::UninstallFunction(const XalanDOMString&          
theFunctionName)
  +XPathFunctionTable::UninstallFunction(const XalanDOMChar*    theFunctionName)
   {
  -     assert(length(theFunctionName) != 0);
  -
  -     // See if a function of that name is installed...
  -     const FunctionNameIndexMapType::iterator        i =
  -             m_FunctionNameIndex.find(theFunctionName);
  +     const int       theFunctionID =
  +                     getFunctionIndex(theFunctionName);
   
  -     if (i == m_FunctionNameIndex.end())
  +     if (theFunctionID == InvalidFunctionNumberID)
        {
                return false;
        }
        else
        {
  -             assert(CollectionType::size_type((*i).second) < 
m_FunctionCollection.size());
  +             const Function* const   theFunction = 
m_functionTable[theFunctionID];
   
  -#if !defined(XALAN_NO_NAMESPACES)
  -             using std::find;
  -#endif
  +             m_functionTable[theFunctionID] = 0;
   
  -             // Delete the function...
   #if defined(XALAN_CANNOT_DELETE_CONST)
  -             delete (Function*)m_FunctionCollection[(*i).second];
  +             delete (Function*)theFunction;
   #else
  -             delete m_FunctionCollection[(*i).second];
  +             delete theFunction;
   #endif
   
  -             // Set the entry in the table to 0...
  -             m_FunctionCollection[(*i).second] = 0;
  -
                return true;
        }
   }
   
   
  +
   #if 0
   #include <fstream>
   
  @@ -285,172 +274,112 @@
   {
        try
        {
  -             m_FunctionCollection.reserve(eTableSize);
  -
  -             // Start with the longest function name, so we only have
  -             // one allocation for this string.
  -             XalanDOMString  theFunctionName;
  -
  -             theFunctionName = s_substringBefore;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_substringBefore,
                                FunctionSubstringBefore());
   
  -             theFunctionName = s_last;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_last,
                                FunctionLast());
   
  -             theFunctionName = s_position;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_position,
                                FunctionPosition());
   
  -             theFunctionName = s_count;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_count,
                                FunctionCount());
   
  -             theFunctionName = s_id;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_id,
                                FunctionID());
   
  -             theFunctionName = s_localName;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_localName,
                                FunctionLocalName());
   
  -             theFunctionName = s_namespaceUri;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_namespaceUri,
                                FunctionNamespaceURI());
   
  -             theFunctionName = s_name;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_name,
                                FunctionName());
   
  -             theFunctionName = s_string;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_string,
                                FunctionString());
   
  -             theFunctionName = s_concat;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_concat,
                                FunctionConcat());
   
  -             theFunctionName = s_startsWith;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_startsWith,
                                FunctionStartsWith());
   
  -             theFunctionName = s_contains;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_contains,
                                FunctionContains());
   
  -             theFunctionName = s_substringAfter;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_substringAfter,
                                FunctionSubstringAfter());
   
  -             theFunctionName = s_substring;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_substring,
                                FunctionSubstring());
   
  -             theFunctionName = s_stringLength;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_stringLength,
                                FunctionStringLength());
   
  -             theFunctionName = s_normalizeSpace;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_normalizeSpace,
                                FunctionNormalizeSpace());
   
  -             theFunctionName = s_translate;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_translate,
                                FunctionTranslate());
   
  -             theFunctionName = s_boolean;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_boolean,
                                FunctionBoolean());
   
  -             theFunctionName = s_not;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_not,
                                FunctionNot());
   
  -             theFunctionName = s_true;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_true,
                                FunctionTrue());
   
  -             theFunctionName = s_false;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_false,
                                FunctionFalse());
   
  -             theFunctionName = s_lang;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_lang,
                                FunctionLang());
   
  -             theFunctionName = s_number;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_number,
                                FunctionNumber());
   
  -             theFunctionName = s_sum;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_sum,
                                FunctionSum());
   
  -             theFunctionName = s_floor;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_floor,
                                FunctionFloor());
   
  -             theFunctionName = s_ceiling;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_ceiling,
                                FunctionCeiling());
   
  -             theFunctionName = s_round;
  -
                InstallFunction(
  -                             theFunctionName,
  +                             s_round,
                                FunctionRound());
   
   #if 0
  @@ -479,13 +408,10 @@
                using std::for_each;
   #endif
   
  -             for_each(m_FunctionCollection.begin(),
  -                              m_FunctionCollection.end(),
  -                              DeleteFunctorType());
  -
  -             CollectionType().swap(m_FunctionCollection);
  -
  -             FunctionNameIndexMapType().swap(m_FunctionNameIndex);
  +             for_each(
  +                     m_functionTable,
  +                     m_functionTable + TableSize,
  +                     DeleteFunctorType());
        }
        catch(...)
        {
  @@ -495,8 +421,12 @@
   
   
   int
  -XPathFunctionTable::getFunctionIndex(const XalanDOMString&   theName)
  +XPathFunctionTable::getFunctionIndex(
  +                     const XalanDOMChar*             theName,
  +                     StringSizeType                  theNameLength)
   {
  +     assert(theName != 0);
  +
        // Do a binary search...
        const FunctionNameTableEntry*   theFirst = s_functionNames;
        const FunctionNameTableEntry*   theLast = s_lastFunctionName;
  @@ -508,8 +438,8 @@
                assert(theCurrent->m_size == length(theCurrent->m_name));
   
                const int       theResult = compare(
  -                             theName.c_str(),
  -                             theName.length(),
  +                             theName,
  +                             theNameLength,
                                theCurrent->m_name,
                                theCurrent->m_size);
   
  @@ -574,6 +504,21 @@
   
   
   
  +
  +XPathExceptionFunctionNotSupported::XPathExceptionFunctionNotSupported(const 
XalanDOMChar*   theFunctionName) :
  +     XalanXPathException(TranscodeFromLocalCodePage("The specified function 
is not supported: ") + theFunctionName)
  +{
  +}
  +
  +
  +
  +XPathExceptionFunctionNotSupported::~XPathExceptionFunctionNotSupported()
  +{
  +}
  +
  +
  +
  +
   const XalanDOMChar   XPathFunctionTable::s_id[] =
   {
        XalanUnicode::charLetter_i,
  @@ -1229,4 +1174,4 @@
   
   
   const SizeType               XPathFunctionTable::s_functionNamesSize =
  -     sizeof(s_functionNames) / sizeof(s_functionNames[0]);
  \ No newline at end of file
  +     sizeof(s_functionNames) / sizeof(s_functionNames[0]);
  
  
  
  1.20      +81 -126   xml-xalan/c/src/XPath/XPathFunctionTable.hpp
  
  Index: XPathFunctionTable.hpp
  ===================================================================
  RCS file: /home/cvs/xml-xalan/c/src/XPath/XPathFunctionTable.hpp,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- XPathFunctionTable.hpp    25 Sep 2002 06:51:44 -0000      1.19
  +++ XPathFunctionTable.hpp    26 Sep 2002 01:36:42 -0000      1.20
  @@ -65,9 +65,6 @@
   
   
   #include <algorithm>
  -#if !defined(XPATH_FUNCTION_TABLE_NEW)
  -#include <map>
  -#endif
   
   
   
  @@ -113,25 +110,28 @@
   
   
   /**
  + * Exception class thrown when an installFunction() is called with a 
  + * function name that is not supported.
  + */
  +class XALAN_XPATH_EXPORT XPathExceptionFunctionNotSupported : public 
XalanXPathException
  +{
  +public:
  +
  +     XPathExceptionFunctionNotSupported(const XalanDOMChar*  
theFunctionName);
  +
  +     ~XPathExceptionFunctionNotSupported();
  +};
  +
  +
  +
  +/**
    * Class defines a table of functions that can be called in XPath expresions.
    */
   class XALAN_XPATH_EXPORT XPathFunctionTable
   {
   public:
   
  -#if !defined(XPATH_FUNCTION_TABLE_NEW)
  -#if defined(XALAN_NO_NAMESPACES)
  -     typedef vector<const Function*>                 CollectionType;
  -     typedef map<XalanDOMString,
  -                             int,
  -                             less<XalanDOMString> >          
FunctionNameIndexMapType;
  -#else
  -     typedef std::vector<const Function*>    CollectionType;
  -     typedef std::map<XalanDOMString, int>   FunctionNameIndexMapType;
  -#endif
  -#endif
  -
  -     enum { InvalidFunctionNumberID = -1, eTableSize = 36 };
  +     enum { InvalidFunctionNumberID = -1, TableSize = 36 };
   
        typedef size_t                                          SizeType;
        typedef XalanDOMString::size_type       StringSizeType;
  @@ -167,7 +167,6 @@
        const Function&
        operator[](const XalanDOMString&        theFunctionName) const
        {
  -#if defined(XPATH_FUNCTION_TABLE_NEW)
                const int       theFunctionID =
                        getFunctionIndex(theFunctionName);
   
  @@ -175,15 +174,6 @@
                {
                        return *m_functionTable[theFunctionID];
                }
  -#else
  -             FunctionNameIndexMapType::const_iterator        i =
  -                     m_FunctionNameIndex.find(theFunctionName);
  -
  -             if (i != m_FunctionNameIndex.end())
  -             {
  -                     return *m_FunctionCollection[(*i).second];
  -             }
  -#endif
                else
                {
                        throw 
XPathExceptionFunctionNotAvailable(theFunctionName);
  @@ -199,16 +189,10 @@
        const Function&
        operator[](int  theFunctionID) const
        {
  -#if defined(XPATH_FUNCTION_TABLE_NEW)
  -             assert(theFunctionID >= 0 && theFunction < eTableSize);
  +             assert(theFunctionID >= 0 && theFunctionID < TableSize);
  +             assert(m_functionTable[theFunctionID] != 0);
   
                return *m_functionTable[theFunctionID];
  -#else
  -             assert(theFunctionID >= 0 &&
  -                        CollectionType::size_type(theFunctionID) < 
m_FunctionCollection.size());
  -
  -             return *m_FunctionCollection[theFunctionID];
  -#endif
        }
   
        /**
  @@ -222,29 +206,12 @@
        {
                XalanDOMString  theName;
   
  -#if defined(XPATH_FUNCTION_TABLE_NEW)
  -             if (theFunctionID >= 0 && theFunction < eTableSize)
  +             if (theFunctionID >= 0 && theFunctionID < TableSize)
                {
  -                     theName = s_functionNames[theFunctionID];
  +                     theName.assign(
  +                             s_functionNames[theFunctionID].m_name,
  +                             s_functionNames[theFunctionID].m_size);
                }
  -#else
  -             if (theFunctionID >= 0 &&
  -                     CollectionType::size_type(theFunctionID) < 
m_FunctionCollection.size())
  -             {
  -                     FunctionNameIndexMapType::const_iterator        i =
  -                             m_FunctionNameIndex.begin();
  -
  -                     while (i != m_FunctionNameIndex.end())
  -                     {
  -                             if ((*i).second == theFunctionID)
  -                             {
  -                                     theName = (*i).first;
  -
  -                                     break;
  -                             }
  -                     }
  -             }
  -#endif
   
                return theName;
        }
  @@ -258,21 +225,7 @@
        int
        nameToID(const XalanDOMString&  theName) const
        {
  -#if defined(XPATH_FUNCTION_TABLE_NEW)
                return getFunctionIndex(theName);
  -#else
  -             const FunctionNameIndexMapType::const_iterator  i =
  -                     m_FunctionNameIndex.find(theName);
  -
  -             if (i != m_FunctionNameIndex.end())
  -             {
  -                     return (*i).second;
  -             }
  -             else
  -             {
  -                     return InvalidFunctionNumberID;
  -             }
  -#endif
        }
   
        /**
  @@ -284,6 +237,32 @@
        void
        InstallFunction(
                        const XalanDOMString&   theFunctionName,
  +                     const Function&                 theFunction)
  +     {
  +             InstallFunction(theFunctionName.c_str(), theFunction);
  +     }
  +
  +     /**
  +      * Remove a named function from the function table.
  +      * 
  +      * @param theFunctionName name of function
  +      * @return true if the function was found and removed.
  +      */
  +     bool
  +     UninstallFunction(const XalanDOMString&         theFunctionName)
  +     {
  +             return UninstallFunction(theFunctionName.c_str());
  +     }
  +
  +     /**
  +      * Insert a named function into the function table.
  +      * 
  +      * @param theFunctionName name of function
  +      * @param theFunction     function object corresponding to name
  +      */
  +     void
  +     InstallFunction(
  +                     const XalanDOMChar*             theFunctionName,
                        const Function&                 theFunction);
   
        /**
  @@ -293,7 +272,7 @@
         * @return true if the function was found and removed.
         */
        bool
  -     UninstallFunction(const XalanDOMString&         theFunctionName);
  +     UninstallFunction(const XalanDOMChar*   theFunctionName);
   
        /**
         * Whether a named function is in the function table.
  @@ -304,18 +283,7 @@
        bool
        isInstalledFunction(const XalanDOMString&       theFunctionName) const
        {
  -#if defined(XPATH_FUNCTION_TABLE_NEW)
  -             if (getFunctionIndex(theFunctionName) != 
InvalidFunctionNumberID)
  -#else
  -             if (m_FunctionNameIndex.find(theFunctionName) != 
m_FunctionNameIndex.end())
  -#endif
  -             {
  -                     return true;
  -             }
  -             else
  -             {
  -                     return false;
  -             }
  +             return getFunctionIndex(theFunctionName) != 
InvalidFunctionNumberID ? true : false;
        }
   
   #if defined(XALAN_NO_MEMBER_TEMPLATES)
  @@ -334,7 +302,6 @@
        void
        getInstalledFunctionNames(InstalledFunctionNameVectorType&      
theVector) const
        {
  -#if defined(XPATH_FUNCTION_TABLE_NEW)
                XalanDOMString  theString;
   
                for (int i = 0; i < TableSize; ++i)
  @@ -349,18 +316,6 @@
                        }
                }
   #else
  -             FunctionNameIndexMapType::const_iterator        i =
  -                     m_FunctionNameIndex.begin();
  -
  -             while(i != m_FunctionNameIndex.end())
  -             {
  -                     theVector.push_back((*i).first);
  -
  -                     ++i;
  -             }
  -     }
  -#endif
  -#else
   
        /**
         * Add a list of the names of installed functions to a vector of names.
  @@ -371,7 +326,6 @@
        void
        getInstalledFunctionNames(OutputIteratorType    theIterator) const
        {
  -#if defined(XPATH_FUNCTION_TABLE_NEW)
                XalanDOMString  theString;
   
                for (int i = 0; i < TableSize; ++i)
  @@ -387,18 +341,6 @@
                                ++theIterator;
                        }
                }
  -#else
  -             FunctionNameIndexMapType::const_iterator        i =
  -                     m_FunctionNameIndex.begin();
  -
  -             while(i != m_FunctionNameIndex.end())
  -             {
  -                     *theIterator = (*i).first;
  -
  -                     ++i;
  -                     ++theIterator;
  -             }
  -#endif
        }
   #endif
   
  @@ -409,21 +351,6 @@
                StringSizeType                  m_size;
        };
   
  -private:
  -
  -     static int
  -     getFunctionIndex(const XalanDOMString&  theName);
  -
  -#if !defined(XPATH_FUNCTION_TABLE_NEW)
  -     CollectionType                          m_FunctionCollection;
  -
  -     FunctionNameIndexMapType        m_FunctionNameIndex;
  -#endif
  -
  -     const Function*                         m_functionTable[eTableSize];
  -
  -     const Function** const          m_functionTableEnd;
  -
        // These are static strings for the functions supported.
        // Note that the XSLT functions are also here, since it's
        // just easier to do it this way.
  @@ -539,11 +466,39 @@
        // A table of function names.
        static const FunctionNameTableEntry                     
s_functionNames[];
   
  -     // The last one in the table of function names.
  -     static const FunctionNameTableEntry* const      s_lastFunctionName;
  -
        // The size of the table.
        static const SizeType                                           
s_functionNamesSize;
  +
  +private:
  +
  +     static int
  +     getFunctionIndex(const XalanDOMString&  theName)
  +     {
  +             return getFunctionIndex(
  +                             theName.c_str(),
  +                             theName.length());
  +     }
  +
  +     static int
  +     getFunctionIndex(const XalanDOMChar*    theName)
  +     {
  +             return getFunctionIndex(
  +                             theName,
  +                             XalanDOMString::length(theName));
  +     }
  +
  +     static int
  +     getFunctionIndex(
  +                     const XalanDOMChar*             theName,
  +                     StringSizeType                  theNameLength);
  +
  +
  +     const Function*                         m_functionTable[TableSize];
  +
  +     const Function** const          m_functionTableEnd;
  +
  +     // The last one in the table of function names.
  +     static const FunctionNameTableEntry* const      s_lastFunctionName;
   };
   
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to