Sierra Toolkit Version of the Day
functional_eastl.h
00001 /*
00002 Copyright (C) 2005,2009-2010 Electronic Arts, Inc.  All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions
00006 are met:
00007 
00008 1.  Redistributions of source code must retain the above copyright
00009     notice, this list of conditions and the following disclaimer.
00010 2.  Redistributions in binary form must reproduce the above copyright
00011     notice, this list of conditions and the following disclaimer in the
00012     documentation and/or other materials provided with the distribution.
00013 3.  Neither the name of Electronic Arts, Inc. ("EA") nor the names of
00014     its contributors may be used to endorse or promote products derived
00015     from this software without specific prior written permission.
00016 
00017 THIS SOFTWARE IS PROVIDED BY ELECTRONIC ARTS AND ITS CONTRIBUTORS "AS IS" AND ANY
00018 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00019 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00020 DISCLAIMED. IN NO EVENT SHALL ELECTRONIC ARTS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
00021 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 */
00028 
00030 // EASTL/functional.h
00031 // Written and maintained by Paul Pedriana - 2005
00033 
00034 
00035 #ifndef EASTL_FUNCTIONAL_H
00036 #define EASTL_FUNCTIONAL_H
00037 
00038 
00039 #include <stk_util/util/config_eastl.h>
00040 #include <stk_util/util/type_traits_eastl.h>
00041 
00042 
00043 namespace eastl
00044 {
00045 
00047     // Primary C++ functions
00049 
00050     template <typename Argument, typename Result>
00051     struct unary_function
00052     {
00053         typedef Argument argument_type;
00054         typedef Result   result_type;
00055     };
00056 
00057 
00058     template <typename Argument1, typename Argument2, typename Result>
00059     struct binary_function
00060     {
00061         typedef Argument1 first_argument_type;
00062         typedef Argument2 second_argument_type;
00063         typedef Result    result_type;
00064     };
00065 
00066 
00067     template <typename T>
00068     struct plus : public binary_function<T, T, T>
00069     {
00070         T operator()(const T& a, const T& b) const
00071             { return a + b; }
00072     };
00073 
00074     template <typename T>
00075     struct minus : public binary_function<T, T, T>
00076     {
00077         T operator()(const T& a, const T& b) const
00078             { return a - b; }
00079     };
00080 
00081     template <typename T>
00082     struct multiplies : public binary_function<T, T, T>
00083     {
00084         T operator()(const T& a, const T& b) const
00085             { return a * b; }
00086     };
00087 
00088     template <typename T>
00089     struct divides : public binary_function<T, T, T>
00090     {
00091         T operator()(const T& a, const T& b) const
00092             { return a / b; }
00093     };
00094 
00095     template <typename T>
00096     struct modulus : public binary_function<T, T, T>
00097     {
00098         T operator()(const T& a, const T& b) const
00099             { return a % b; }
00100     };
00101 
00102     template <typename T>
00103     struct negate : public unary_function<T, T>
00104     {
00105         T operator()(const T& a) const
00106             { return -a; }
00107     };
00108 
00109     template <typename T>
00110     struct equal_to : public binary_function<T, T, bool>
00111     {
00112         bool operator()(const T& a, const T& b) const
00113             { return a == b; }
00114     };
00115 
00116     template <typename T, typename Compare>
00117     bool validate_equal_to(const T& a, const T& b, Compare compare)
00118     {
00119         return compare(a, b) == compare(b, a);
00120     }
00121 
00122     template <typename T>
00123     struct not_equal_to : public binary_function<T, T, bool>
00124     {
00125         bool operator()(const T& a, const T& b) const
00126             { return a != b; }
00127     };
00128 
00129     template <typename T, typename Compare>
00130     bool validate_not_equal_to(const T& a, const T& b, Compare compare)
00131     {
00132         return compare(a, b) == compare(b, a); // We want the not equal comparison results to be equal.
00133     }
00134 
00149     template <typename T>
00150     struct str_equal_to : public binary_function<T, T, bool>
00151     {
00152         bool operator()(T a, T b) const
00153         {
00154             while(*a && (*a == *b))
00155             {
00156                 ++a;
00157                 ++b;
00158             }
00159             return (*a == *b);
00160         }
00161     };
00162 
00163     template <typename T>
00164     struct greater : public binary_function<T, T, bool>
00165     {
00166         bool operator()(const T& a, const T& b) const
00167             { return a > b; }
00168     };
00169 
00170     template <typename T, typename Compare>
00171     bool validate_greater(const T& a, const T& b, Compare compare)
00172     {
00173         return !compare(a, b) || !compare(b, a); // If (a > b), then !(b > a)
00174     }
00175 
00176     template <typename T>
00177     struct less : public binary_function<T, T, bool>
00178     {
00179         bool operator()(const T& a, const T& b) const
00180             { return a < b; }
00181     };
00182 
00183     template <typename T, typename Compare>
00184     bool validate_less(const T& a, const T& b, Compare compare)
00185     {
00186         return !compare(a, b) || !compare(b, a); // If (a < b), then !(b < a)
00187     }
00188 
00189     template <typename T>
00190     struct greater_equal : public binary_function<T, T, bool>
00191     {
00192         bool operator()(const T& a, const T& b) const
00193         { return a >= b; }
00194     };
00195 
00196     template <typename T, typename Compare>
00197     bool validate_greater_equal(const T& a, const T& b, Compare compare)
00198     {
00199         return !compare(a, b) || !compare(b, a); // If (a >= b), then !(b >= a)
00200     }
00201 
00202     template <typename T>
00203     struct less_equal : public binary_function<T, T, bool>
00204     {
00205         bool operator()(const T& a, const T& b) const
00206             { return a <= b; }
00207     };
00208 
00209     template <typename T, typename Compare>
00210     bool validate_less_equal(const T& a, const T& b, Compare compare)
00211     {
00212         return !compare(a, b) || !compare(b, a); // If (a <= b), then !(b <= a)
00213     }
00214 
00215     template <typename T>
00216     struct logical_and : public binary_function<T, T, bool>
00217     {
00218         bool operator()(const T& a, const T& b) const
00219             { return a && b; }
00220     };
00221 
00222     template <typename T>
00223     struct logical_or : public binary_function<T, T, bool>
00224     {
00225         bool operator()(const T& a, const T& b) const
00226             { return a || b; }
00227     };
00228 
00229     template <typename T>
00230     struct logical_not : public unary_function<T, bool>
00231     {
00232         bool operator()(const T& a) const
00233             { return !a; }
00234     };
00235 
00236 
00237 
00239     // Dual type functions
00241 
00242     template <typename T, typename U>
00243     struct equal_to_2 : public binary_function<T, U, bool>
00244     {
00245         bool operator()(const T& a, const U& b) const
00246             { return a == b; }
00247     };
00248 
00249     template <typename T, typename U>
00250     struct not_equal_to_2 : public binary_function<T, U, bool>
00251     {
00252         bool operator()(const T& a, const U& b) const
00253             { return a != b; }
00254     };
00255 
00256     template <typename T, typename U>
00257     struct less_2 : public binary_function<T, U, bool>
00258     {
00259         bool operator()(const T& a, const U& b) const
00260             { return a < b; }
00261     };
00262 
00263 
00264 
00265 
00268     template <typename Predicate>
00269     class unary_negate : public unary_function<typename Predicate::argument_type, bool>
00270     {
00271         protected:
00272             Predicate mPredicate;
00273         public:
00274             explicit unary_negate(const Predicate& a)
00275                 : mPredicate(a) {}
00276             bool operator()(const typename Predicate::argument_type& a) const
00277                 { return !mPredicate(a); }
00278     };
00279 
00280     template <typename Predicate>
00281     inline unary_negate<Predicate> not1(const Predicate& predicate)
00282         { return unary_negate<Predicate>(predicate); }
00283 
00284 
00285 
00288     template <typename Predicate>
00289     class binary_negate : public binary_function<typename Predicate::first_argument_type, typename Predicate::second_argument_type, bool>
00290     {
00291         protected:
00292             Predicate mPredicate;
00293         public:
00294             explicit binary_negate(const Predicate& a)
00295                 : mPredicate(a) { }
00296             bool operator()(const typename Predicate::first_argument_type& a, const typename Predicate::second_argument_type& b) const
00297                 { return !mPredicate(a, b); }
00298     };
00299 
00300     template <typename Predicate>
00301     inline binary_negate<Predicate> not2(const Predicate& predicate)
00302         { return binary_negate<Predicate>(predicate); }
00303 
00304 
00305 
00306 
00308     // bind
00310 
00313     template <typename Operation>
00314     class binder1st : public unary_function<typename Operation::second_argument_type, typename Operation::result_type>
00315     {
00316         protected:
00317             typename Operation::first_argument_type value;
00318             Operation op;
00319 
00320         public:
00321             binder1st(const Operation& x, const typename Operation::first_argument_type& y)
00322                 : value(y), op(x) { }
00323 
00324             typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const
00325                 { return op(value, x); }
00326 
00327             typename Operation::result_type operator()(typename Operation::second_argument_type& x) const
00328                 { return op(value, x); }
00329     };
00330 
00331 
00332     template <typename Operation, typename T>
00333     inline binder1st<Operation> bind1st(const Operation& op, const T& x)
00334     {
00335         typedef typename Operation::first_argument_type value;
00336         return binder1st<Operation>(op, value(x));
00337     }
00338 
00339 
00342     template <typename Operation>
00343     class binder2nd : public unary_function<typename Operation::first_argument_type, typename Operation::result_type>
00344     {
00345         protected:
00346             Operation op;
00347             typename Operation::second_argument_type value;
00348 
00349         public:
00350             binder2nd(const Operation& x, const typename Operation::second_argument_type& y)
00351                 : op(x), value(y) { }
00352 
00353             typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const
00354                 { return op(x, value); }
00355 
00356             typename Operation::result_type operator()(typename Operation::first_argument_type& x) const
00357                 { return op(x, value); }
00358     };
00359 
00360 
00361     template <typename Operation, typename T>
00362     inline binder2nd<Operation> bind2nd(const Operation& op, const T& x)
00363     {
00364         typedef typename Operation::second_argument_type value;
00365         return binder2nd<Operation>(op, value(x));
00366     }
00367 
00368 
00369 
00370 
00372     // pointer_to_unary_function
00374 
00386     template <typename Arg, typename Result>
00387     class pointer_to_unary_function : public unary_function<Arg, Result>
00388     {
00389     protected:
00390         Result (*mpFunction)(Arg);
00391 
00392     public:
00393         pointer_to_unary_function()
00394             { }
00395 
00396         explicit pointer_to_unary_function(Result (*pFunction)(Arg))
00397             : mpFunction(pFunction) { }
00398 
00399         Result operator()(Arg x) const
00400             { return mpFunction(x); }
00401     };
00402 
00403 
00412     template <typename Arg, typename Result>
00413     inline pointer_to_unary_function<Arg, Result> ptr_fun(Result (*pFunction)(Arg))
00414         { return pointer_to_unary_function<Arg, Result>(pFunction); }
00415 
00416 
00417 
00418 
00419 
00421     // pointer_to_binary_function
00423 
00430     template <typename Arg1, typename Arg2, typename Result>
00431     class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result>
00432     {
00433     protected:
00434         Result (*mpFunction)(Arg1, Arg2);
00435 
00436     public:
00437         pointer_to_binary_function()
00438             { }
00439 
00440         explicit pointer_to_binary_function(Result (*pFunction)(Arg1, Arg2))
00441             : mpFunction(pFunction) {}
00442 
00443         Result operator()(Arg1 x, Arg2 y) const
00444             { return mpFunction(x, y); }
00445     };
00446 
00447 
00454     template <typename Arg1, typename Arg2, typename Result>
00455     inline pointer_to_binary_function<Arg1, Arg2, Result> ptr_fun(Result (*pFunction)(Arg1, Arg2))
00456         { return pointer_to_binary_function<Arg1, Arg2, Result>(pFunction); }
00457 
00458 
00459 
00460 
00461 
00462 
00464     // mem_fun
00465     // mem_fun1
00466     //
00467     // Note that mem_fun calls member functions via *pointers* to classes
00468     // and not instances of classes. mem_fun_ref is for calling functions
00469     // via instances of classes or references to classes.
00470     //
00472 
00477     template <typename Result, typename T>
00478     class mem_fun_t : public unary_function<T*, Result>
00479     {
00480     public:
00481         typedef Result (T::*MemberFunction)();
00482 
00483         EA_FORCE_INLINE explicit mem_fun_t(MemberFunction pMemberFunction)
00484             : mpMemberFunction(pMemberFunction)
00485         {
00486             // Empty
00487         }
00488 
00489         EA_FORCE_INLINE Result operator()(T* pT) const
00490         {
00491             return (pT->*mpMemberFunction)();
00492         }
00493 
00494     protected:
00495         MemberFunction mpMemberFunction;
00496     };
00497 
00498 
00503     template <typename Result, typename T, typename Argument>
00504     class mem_fun1_t : public binary_function<T*, Argument, Result>
00505     {
00506     public:
00507         typedef Result (T::*MemberFunction)(Argument);
00508 
00509         EA_FORCE_INLINE explicit mem_fun1_t(MemberFunction pMemberFunction)
00510             : mpMemberFunction(pMemberFunction)
00511         {
00512             // Empty
00513         }
00514 
00515         EA_FORCE_INLINE Result operator()(T* pT, Argument arg) const
00516         {
00517             return (pT->*mpMemberFunction)(arg);
00518         }
00519 
00520     protected:
00521         MemberFunction mpMemberFunction;
00522     };
00523 
00524 
00532     template <typename Result, typename T>
00533     class const_mem_fun_t : public unary_function<const T*, Result>
00534     {
00535     public:
00536         typedef Result (T::*MemberFunction)() const;
00537 
00538         EA_FORCE_INLINE explicit const_mem_fun_t(MemberFunction pMemberFunction)
00539             : mpMemberFunction(pMemberFunction)
00540         {
00541             // Empty
00542         }
00543 
00544         EA_FORCE_INLINE Result operator()(const T* pT) const
00545         {
00546             return (pT->*mpMemberFunction)();
00547         }
00548 
00549     protected:
00550         MemberFunction mpMemberFunction;
00551     };
00552 
00553 
00561     template <typename Result, typename T, typename Argument>
00562     class const_mem_fun1_t : public binary_function<const T*, Argument, Result>
00563     {
00564     public:
00565         typedef Result (T::*MemberFunction)(Argument) const;
00566 
00567         EA_FORCE_INLINE explicit const_mem_fun1_t(MemberFunction pMemberFunction)
00568             : mpMemberFunction(pMemberFunction)
00569         {
00570             // Empty
00571         }
00572 
00573         EA_FORCE_INLINE Result operator()(const T* pT, Argument arg) const
00574         {
00575             return (pT->*mpMemberFunction)(arg);
00576         }
00577 
00578     protected:
00579         MemberFunction mpMemberFunction;
00580     };
00581 
00582 
00592     template <typename Result, typename T>
00593     EA_FORCE_INLINE mem_fun_t<Result, T>
00594     mem_fun(Result (T::*MemberFunction)())
00595     {
00596         return eastl::mem_fun_t<Result, T>(MemberFunction);
00597     }
00598 
00599     template <typename Result, typename T, typename Argument>
00600     EA_FORCE_INLINE mem_fun1_t<Result, T, Argument>
00601     mem_fun(Result (T::*MemberFunction)(Argument))
00602     {
00603         return eastl::mem_fun1_t<Result, T, Argument>(MemberFunction);
00604     }
00605 
00606     template <typename Result, typename T>
00607     EA_FORCE_INLINE const_mem_fun_t<Result, T>
00608     mem_fun(Result (T::*MemberFunction)() const)
00609     {
00610         return eastl::const_mem_fun_t<Result, T>(MemberFunction);
00611     }
00612 
00613     template <typename Result, typename T, typename Argument>
00614     EA_FORCE_INLINE const_mem_fun1_t<Result, T, Argument>
00615     mem_fun(Result (T::*MemberFunction)(Argument) const)
00616     {
00617         return eastl::const_mem_fun1_t<Result, T, Argument>(MemberFunction);
00618     }
00619 
00620 
00621 
00622 
00623 
00625     // mem_fun_ref
00626     // mem_fun1_ref
00627     //
00629 
00632     template <typename Result, typename T>
00633     class mem_fun_ref_t : public unary_function<T, Result>
00634     {
00635     public:
00636         typedef Result (T::*MemberFunction)();
00637 
00638         EA_FORCE_INLINE explicit mem_fun_ref_t(MemberFunction pMemberFunction)
00639             : mpMemberFunction(pMemberFunction)
00640         {
00641             // Empty
00642         }
00643 
00644         EA_FORCE_INLINE Result operator()(T& t) const
00645         {
00646             return (t.*mpMemberFunction)();
00647         }
00648 
00649     protected:
00650         MemberFunction mpMemberFunction;
00651     };
00652 
00653 
00656     template <typename Result, typename T, typename Argument>
00657     class mem_fun1_ref_t : public binary_function<T, Argument, Result>
00658     {
00659     public:
00660         typedef Result (T::*MemberFunction)(Argument);
00661 
00662         EA_FORCE_INLINE explicit mem_fun1_ref_t(MemberFunction pMemberFunction)
00663             : mpMemberFunction(pMemberFunction)
00664         {
00665             // Empty
00666         }
00667 
00668         EA_FORCE_INLINE Result operator()(T& t, Argument arg) const
00669         {
00670             return (t.*mpMemberFunction)(arg);
00671         }
00672 
00673     protected:
00674         MemberFunction mpMemberFunction;
00675     };
00676 
00677 
00680     template <typename Result, typename T>
00681     class const_mem_fun_ref_t : public unary_function<T, Result>
00682     {
00683     public:
00684         typedef Result (T::*MemberFunction)() const;
00685 
00686         EA_FORCE_INLINE explicit const_mem_fun_ref_t(MemberFunction pMemberFunction)
00687             : mpMemberFunction(pMemberFunction)
00688         {
00689             // Empty
00690         }
00691 
00692         EA_FORCE_INLINE Result operator()(const T& t) const
00693         {
00694             return (t.*mpMemberFunction)();
00695         }
00696 
00697     protected:
00698         MemberFunction mpMemberFunction;
00699     };
00700 
00701 
00704     template <typename Result, typename T, typename Argument>
00705     class const_mem_fun1_ref_t : public binary_function<T, Argument, Result>
00706     {
00707     public:
00708         typedef Result (T::*MemberFunction)(Argument) const;
00709 
00710         EA_FORCE_INLINE explicit const_mem_fun1_ref_t(MemberFunction pMemberFunction)
00711             : mpMemberFunction(pMemberFunction)
00712         {
00713             // Empty
00714         }
00715 
00716         EA_FORCE_INLINE Result operator()(const T& t, Argument arg) const
00717         {
00718             return (t.*mpMemberFunction)(arg);
00719         }
00720 
00721     protected:
00722         MemberFunction mpMemberFunction;
00723     };
00724 
00725 
00732     template <typename Result, typename T>
00733     EA_FORCE_INLINE mem_fun_ref_t<Result, T>
00734     mem_fun_ref(Result (T::*MemberFunction)())
00735     {
00736         return eastl::mem_fun_ref_t<Result, T>(MemberFunction);
00737     }
00738 
00739     template <typename Result, typename T, typename Argument>
00740     EA_FORCE_INLINE mem_fun1_ref_t<Result, T, Argument>
00741     mem_fun_ref(Result (T::*MemberFunction)(Argument))
00742     {
00743         return eastl::mem_fun1_ref_t<Result, T, Argument>(MemberFunction);
00744     }
00745 
00746     template <typename Result, typename T>
00747     EA_FORCE_INLINE const_mem_fun_ref_t<Result, T>
00748     mem_fun_ref(Result (T::*MemberFunction)() const)
00749     {
00750         return eastl::const_mem_fun_ref_t<Result, T>(MemberFunction);
00751     }
00752 
00753     template <typename Result, typename T, typename Argument>
00754     EA_FORCE_INLINE const_mem_fun1_ref_t<Result, T, Argument>
00755     mem_fun_ref(Result (T::*MemberFunction)(Argument) const)
00756     {
00757         return eastl::const_mem_fun1_ref_t<Result, T, Argument>(MemberFunction);
00758     }
00759 
00760 
00761 
00762 
00764     // hash
00766 
00767     template <typename T> struct hash;
00768 
00769     template <typename T> struct hash<T*> // Note that we use the pointer as-is and don't divide by sizeof(T*). This is because the table is of a prime size and this division doesn't benefit distribution.
00770         { size_t operator()(T* p) const { return size_t(uintptr_t(p)); } };
00771 
00772     template <> struct hash<bool>
00773         { size_t operator()(bool val) const { return static_cast<size_t>(val); } };
00774 
00775     template <> struct hash<char>
00776         { size_t operator()(char val) const { return static_cast<size_t>(val); } };
00777 
00778     template <> struct hash<signed char>
00779         { size_t operator()(signed char val) const { return static_cast<size_t>(val); } };
00780 
00781     template <> struct hash<unsigned char>
00782         { size_t operator()(unsigned char val) const { return static_cast<size_t>(val); } };
00783 
00784     #ifndef EA_WCHAR_T_NON_NATIVE // If wchar_t is a native type instead of simply a define to an existing type...
00785         template <> struct hash<wchar_t>
00786             { size_t operator()(wchar_t val) const { return static_cast<size_t>(val); } };
00787     #endif
00788 
00789     template <> struct hash<signed short>
00790         { size_t operator()(short val) const { return static_cast<size_t>(val); } };
00791 
00792     template <> struct hash<unsigned short>
00793         { size_t operator()(unsigned short val) const { return static_cast<size_t>(val); } };
00794 
00795     template <> struct hash<signed int>
00796         { size_t operator()(signed int val) const { return static_cast<size_t>(val); } };
00797 
00798     template <> struct hash<unsigned int>
00799         { size_t operator()(unsigned int val) const { return static_cast<size_t>(val); } };
00800 
00801     template <> struct hash<signed long>
00802         { size_t operator()(signed long val) const { return static_cast<size_t>(val); } };
00803 
00804     template <> struct hash<unsigned long>
00805         { size_t operator()(unsigned long val) const { return static_cast<size_t>(val); } };
00806 
00807     template <> struct hash<signed long long>
00808         { size_t operator()(signed long long val) const { return static_cast<size_t>(val); } };
00809 
00810     template <> struct hash<unsigned long long>
00811         { size_t operator()(unsigned long long val) const { return static_cast<size_t>(val); } };
00812 
00813     template <> struct hash<float>
00814         { size_t operator()(float val) const { return static_cast<size_t>(val); } };
00815 
00816     template <> struct hash<double>
00817         { size_t operator()(double val) const { return static_cast<size_t>(val); } };
00818 
00819     template <> struct hash<long double>
00820         { size_t operator()(long double val) const { return static_cast<size_t>(val); } };
00821 
00822 
00824     // string hashes
00825     //
00826     // Note that our string hashes here intentionally are slow for long strings.
00827     // The reasoning for this is so:
00828     //    - The large majority of hashed strings are only a few bytes long.
00829     //    - The hash function is significantly more efficient if it can make this assumption.
00830     //    - The user is welcome to make a custom hash for those uncommon cases where
00831     //      long strings need to be hashed. Indeed, the user can probably make a
00832     //      special hash customized for such strings that's better than what we provide.
00834 
00835     template <> struct hash<char8_t*>
00836     {
00837         size_t operator()(const char8_t* p) const
00838         {
00839             size_t c, result = 2166136261U;  // FNV1 hash. Perhaps the best string hash.
00840             while((c = (uint8_t)*p++) != 0)  // Using '!=' disables compiler warnings.
00841                 result = (result * 16777619) ^ c;
00842             return (size_t)result;
00843         }
00844     };
00845 
00846     template <> struct hash<const char8_t*>
00847     {
00848         size_t operator()(const char8_t* p) const
00849         {
00850             size_t c, result = 2166136261U;
00851             while((c = (uint8_t)*p++) != 0) // cast to unsigned 8 bit.
00852                 result = (result * 16777619) ^ c;
00853             return (size_t)result;
00854         }
00855     };
00856 
00857     template <> struct hash<char16_t*>
00858     {
00859         size_t operator()(const char16_t* p) const
00860         {
00861             size_t c, result = 2166136261U;
00862             while((c = (uint16_t)*p++) != 0) // cast to unsigned 16 bit.
00863                 result = (result * 16777619) ^ c;
00864             return (size_t)result;
00865         }
00866     };
00867 
00868     template <> struct hash<const char16_t*>
00869     {
00870         size_t operator()(const char16_t* p) const
00871         {
00872             size_t c, result = 2166136261U;
00873             while((c = (uint16_t)*p++) != 0) // cast to unsigned 16 bit.
00874                 result = (result * 16777619) ^ c;
00875             return (size_t)result;
00876         }
00877     };
00878 
00879     template <> struct hash<char32_t*>
00880     {
00881         size_t operator()(const char32_t* p) const
00882         {
00883             size_t c, result = 2166136261U;
00884             while((c = (uint32_t)*p++) != 0) // cast to unsigned 32 bit.
00885                 result = (result * 16777619) ^ c;
00886             return (size_t)result;
00887         }
00888     };
00889 
00890     template <> struct hash<const char32_t*>
00891     {
00892         size_t operator()(const char32_t* p) const
00893         {
00894             size_t c, result = 2166136261U;
00895             while((c = (uint32_t)*p++) != 0) // cast to unsigned 32 bit.
00896                 result = (result * 16777619) ^ c;
00897             return (size_t)result;
00898         }
00899     };
00900 
00908     template <typename String>
00909     struct string_hash
00910     {
00911         typedef String                                         string_type;
00912         typedef typename String::value_type                    value_type;
00913         typedef typename eastl::add_unsigned<value_type>::type unsigned_value_type;
00914 
00915         size_t operator()(const string_type& s) const
00916         {
00917             const unsigned_value_type* p = (const unsigned_value_type*)s.c_str();
00918             size_t c, result = 2166136261U;
00919             while((c = *p++) != 0)
00920                 result = (result * 16777619) ^ c;
00921             return (size_t)result;
00922         }
00923     };
00924 
00925 
00926 } // namespace eastl
00927 
00928 
00929 #endif // Header include guard
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends