OpenNI 1.5.7
XnAlgorithms.h
Go to the documentation of this file.
00001 /*****************************************************************************
00002 *                                                                            *
00003 *  OpenNI 1.x Alpha                                                          *
00004 *  Copyright (C) 2012 PrimeSense Ltd.                                        *
00005 *                                                                            *
00006 *  This file is part of OpenNI.                                              *
00007 *                                                                            *
00008 *  Licensed under the Apache License, Version 2.0 (the "License");           *
00009 *  you may not use this file except in compliance with the License.          *
00010 *  You may obtain a copy of the License at                                   *
00011 *                                                                            *
00012 *      http://www.apache.org/licenses/LICENSE-2.0                            *
00013 *                                                                            *
00014 *  Unless required by applicable law or agreed to in writing, software       *
00015 *  distributed under the License is distributed on an "AS IS" BASIS,         *
00016 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
00017 *  See the License for the specific language governing permissions and       *
00018 *  limitations under the License.                                            *
00019 *                                                                            *
00020 *****************************************************************************/
00021 #ifndef __XN_ALGORITHMS_H__
00022 #define __XN_ALGORITHMS_H__
00023 
00024 //---------------------------------------------------------------------------
00025 // Includes
00026 //---------------------------------------------------------------------------
00027 #include <XnPlatform.h>
00028 
00029 //---------------------------------------------------------------------------
00030 // Types
00031 //---------------------------------------------------------------------------
00032 template<class T>
00033 XnBool DefaultComparer(const T& arg1, const T& arg2)
00034 {
00035     return arg1 < arg2;
00036 }
00037 
00038 class XnAlgorithms
00039 {
00040 public:
00041     template<class T, class Comparer>
00042     static void BubbleSort(T elements[], XnUInt32 nCount, Comparer comp)
00043     {
00044         XnUInt32 n = nCount;
00045         XnBool bSwapped;
00046         T temp;
00047 
00048         if (nCount == 0)
00049             return;
00050 
00051         do
00052         {
00053             bSwapped = FALSE;
00054             for (XnUInt32 i = 0; i < n - 1; ++i)
00055             {
00056                 if (!comp(elements[i], elements[i+1]))
00057                 {
00058                     // swap
00059                     temp = elements[i];
00060                     elements[i] = elements[i+1];
00061                     elements[i+1] = temp;
00062 
00063                     bSwapped = TRUE;
00064                 }
00065             }
00066 
00067             n -= 1;
00068 
00069         } while (bSwapped);
00070     }
00071 
00072     template<class T>
00073     static void BubbleSort(T elements[], XnUInt32 nCount)
00074     {
00075         BubbleSort(elements, nCount, DefaultComparer);
00076     }
00077 
00078 };
00079 
00080 #endif // __XN_ALGORITHMS_H__