OpenNI 1.5.4: XnAlgorithms.h Source File

OpenNI

OpenNI 1.5.4
XnAlgorithms.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 *                                                                           *
00003 *  OpenNI 1.x Alpha                                                         *
00004 *  Copyright (C) 2011 PrimeSense Ltd.                                       *
00005 *                                                                           *
00006 *  This file is part of OpenNI.                                             *
00007 *                                                                           *
00008 *  OpenNI is free software: you can redistribute it and/or modify           *
00009 *  it under the terms of the GNU Lesser General Public License as published *
00010 *  by the Free Software Foundation, either version 3 of the License, or     *
00011 *  (at your option) any later version.                                      *
00012 *                                                                           *
00013 *  OpenNI is distributed in the hope that it will be useful,                *
00014 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00015 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             *
00016 *  GNU Lesser General Public License for more details.                      *
00017 *                                                                           *
00018 *  You should have received a copy of the GNU Lesser General Public License *
00019 *  along with OpenNI. If not, see <http://www.gnu.org/licenses/>.           *
00020 *                                                                           *
00021 ****************************************************************************/
00022 #ifndef __XN_ALGORITHMS_H__
00023 #define __XN_ALGORITHMS_H__
00024 
00025 //---------------------------------------------------------------------------
00026 // Includes
00027 //---------------------------------------------------------------------------
00028 #include <XnPlatform.h>
00029 
00030 //---------------------------------------------------------------------------
00031 // Types
00032 //---------------------------------------------------------------------------
00033 template<class T>
00034 XnBool DefaultComparer(const T& arg1, const T& arg2)
00035 {
00036     return arg1 < arg2;
00037 }
00038 
00039 class XnAlgorithms
00040 {
00041 public:
00042     template<class T, class Comparer>
00043     static void BubbleSort(T elements[], XnUInt32 nCount, Comparer comp)
00044     {
00045         XnUInt32 n = nCount;
00046         XnBool bSwapped;
00047         T temp;
00048 
00049         if (nCount == 0)
00050             return;
00051 
00052         do
00053         {
00054             bSwapped = FALSE;
00055             for (XnUInt32 i = 0; i < n - 1; ++i)
00056             {
00057                 if (!comp(elements[i], elements[i+1]))
00058                 {
00059                     // swap
00060                     temp = elements[i];
00061                     elements[i] = elements[i+1];
00062                     elements[i+1] = temp;
00063 
00064                     bSwapped = TRUE;
00065                 }
00066             }
00067 
00068             n -= 1;
00069 
00070         } while (bSwapped);
00071     }
00072 
00073     template<class T>
00074     static void BubbleSort(T elements[], XnUInt32 nCount)
00075     {
00076         BubbleSort(elements, nCount, DefaultComparer);
00077     }
00078 
00079 };
00080 
00081 #endif // __XN_ALGORITHMS_H__
Generated on Wed May 16 2012 10:16:05 for OpenNI 1.5.4 by   doxygen 1.7.5.1