src/foundation/ZCompare.h

00001 /*  @(#) $Id: ZCompare.h,v 1.4 2006/07/23 22:00:42 agreen Exp $ */
00002 
00003 /* ------------------------------------------------------------
00004 Copyright (c) 2005 Andrew Green and Learning in Motion, Inc.
00005 http://www.zoolib.org
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a copy
00008 of this software and associated documentation files (the "Software"), to deal
00009 in the Software without restriction, including without limitation the rights
00010 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011 copies of the Software, and to permit persons to whom the Software is
00012 furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020 COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00021 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00022 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00023 ------------------------------------------------------------ */
00024 
00025 #ifndef __ZCompare__
00026 #define __ZCompare__ 1
00027 #include "zconfig.h"
00028 
00029 #include "ZDebug.h"
00030 #include "ZTypes.h"
00031 
00032 #include <string>
00033 #include <list>
00034 #include <vector>
00035 
00036 namespace ZooLib {
00037 
00038 // A default implementation of sCompare_T is nice to have, but it does make
00039 // it hard to tell when we accidentally call operator<. So we declare but
00040 // do not implement it.
00041 template <class T> int sCompare_T(const T& iL, const T& iR);
00042 
00043 // sCompare_T definitions for simple types. They require that int have more bits than int16.
00044 template <> inline int sCompare_T(const ZType& iL, const ZType& iR)
00045         { return int(iL) - int(iR); }
00046 
00047 template <> inline int sCompare_T(const bool& iL, const bool& iR)
00048         { return int(iL) - int(iR); }
00049 
00050 template <> inline int sCompare_T(const int8& iL, const int8& iR)
00051         { return iL - iR; }
00052 
00053 template <> inline int sCompare_T(const int16& iL, const int16& iR)
00054         { return iL - iR; }
00055 
00056 template <> inline int sCompare_T(const int32& iL, const int32& iR)
00057         { return iL < iR ? -1 : iR < iL ? 1 : 0; }
00058 
00059 template <> inline int sCompare_T(const int64& iL, const int64& iR)
00060         { return iL < iR ? -1 : iR < iL ? 1 : 0; }
00061 
00062 template <> inline int sCompare_T(const uint8& iL, const uint8& iR)
00063         { return int(iL) - int(iR); }
00064 
00065 template <> inline int sCompare_T(const uint16& iL, const uint16& iR)
00066         { return int(iL) - int(iR); }
00067 
00068 template <> inline int sCompare_T(const uint32& iL, const uint32& iR)
00069         { return iL < iR ? -1 : iR < iL ? 1 : 0; }
00070 
00071 template <> inline int sCompare_T(const uint64& iL, const uint64& iR)
00072         { return iL < iR ? -1 : iR < iL ? 1 : 0; }
00073 
00074 typedef void* voidstar; // Ugh.
00075 template <> inline int sCompare_T(const voidstar& iL, const voidstar& iR)
00076         { return iL < iR ? -1 : iR < iL ? 1 : 0; }
00077 
00078 // sCompare_T using string's compare function.
00079 template <> inline int sCompare_T(const std::string& iL, const std::string& iR)
00080         { return iL.compare(iR); }
00081 
00082 // sCompare_T defined out-of-line in ZCompare.cpp
00083 template <> int sCompare_T(const ZRectPOD& iL, const ZRectPOD& iR);
00084 template <> int sCompare_T(const ZPointPOD& iL, const ZPointPOD& iR);
00085 template <> int sCompare_T(const float& iL, const float& iR);
00086 template <> int sCompare_T(const double& iL, const double& iR);
00087 
00088 template <class InputIterator>
00089 int sCompare_T(InputIterator leftIter, InputIterator leftEnd,
00090         InputIterator rightIter, InputIterator rightEnd)
00091         {
00092         for (/*no init*/; /*no test*/; ++leftIter, ++rightIter)
00093                 {
00094                 if (leftIter != leftEnd)
00095                         {
00096                         // Left is not exhausted.
00097                         if (rightIter != rightEnd)
00098                                 {
00099                                 // Right is not exhausted either, so we compare their current values.
00100                                 if (int compare = sCompare_T(*leftIter, *rightIter))
00101                                         {
00102                                         // The current values of left and right
00103                                         // are different, so we have a result.
00104                                         return compare;
00105                                         }
00106                                 }
00107                         else
00108                                 {
00109                                 // Exhausted right, but still have left
00110                                 // remaining, so left is greater than right.
00111                                 return 1;
00112                                 }
00113                         }
00114                 else
00115                         {
00116                         // Exhausted left.
00117                         if (rightIter != rightEnd)
00118                                 {
00119                                 // Still have right remaining, so left is less than right.
00120                                 return -1;
00121                                 }
00122                         else
00123                                 {
00124                                 // Exhausted right. And as left is also
00125                                 // exhausted left equals right.
00126                                 return 0;
00127                                 }
00128                         }
00129                 }
00130         }
00131 
00132 template <class S>
00133 int sCompare_T(const std::vector<S>& iLeft, const std::vector<S>& iRight)
00134         { return sCompare_T(iLeft.begin(), iLeft.end(), iRight.begin(), iRight.end()); }
00135 
00136 template <class S>
00137 int sCompare_T(const std::list<S>& iLeft, const std::list<S>& iRight)
00138         { return sCompare_T(iLeft.begin(), iLeft.end(), iRight.begin(), iRight.end()); }
00139 
00140 } // namespace ZooLib
00141 
00142 #endif // __ZCompare__

Generated on Thu Jul 26 11:21:51 2007 for ZooLib by  doxygen 1.4.7