OpenNI 1.5.4
XnAlgorithms.h
Go to the documentation of this file.
1/****************************************************************************
2* *
3* OpenNI 1.x Alpha *
4* Copyright (C) 2011 PrimeSense Ltd. *
5* *
6* This file is part of OpenNI. *
7* *
8* OpenNI is free software: you can redistribute it and/or modify *
9* it under the terms of the GNU Lesser General Public License as published *
10* by the Free Software Foundation, either version 3 of the License, or *
11* (at your option) any later version. *
12* *
13* OpenNI is distributed in the hope that it will be useful, *
14* but WITHOUT ANY WARRANTY; without even the implied warranty of *
15* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16* GNU Lesser General Public License for more details. *
17* *
18* You should have received a copy of the GNU Lesser General Public License *
19* along with OpenNI. If not, see <http://www.gnu.org/licenses/>. *
20* *
21****************************************************************************/
22#ifndef __XN_ALGORITHMS_H__
23#define __XN_ALGORITHMS_H__
24
25//---------------------------------------------------------------------------
26// Includes
27//---------------------------------------------------------------------------
28#include <XnPlatform.h>
29
30//---------------------------------------------------------------------------
31// Types
32//---------------------------------------------------------------------------
33template<class T>
34XnBool DefaultComparer(const T& arg1, const T& arg2)
35{
36 return arg1 < arg2;
37}
38
40{
41public:
42 template<class T, class Comparer>
43 static void BubbleSort(T elements[], XnUInt32 nCount, Comparer comp)
44 {
45 XnUInt32 n = nCount;
46 XnBool bSwapped;
47 T temp;
48
49 if (nCount == 0)
50 return;
51
52 do
53 {
54 bSwapped = FALSE;
55 for (XnUInt32 i = 0; i < n - 1; ++i)
56 {
57 if (!comp(elements[i], elements[i+1]))
58 {
59 // swap
60 temp = elements[i];
61 elements[i] = elements[i+1];
62 elements[i+1] = temp;
63
64 bSwapped = TRUE;
65 }
66 }
67
68 n -= 1;
69
70 } while (bSwapped);
71 }
72
73 template<class T>
74 static void BubbleSort(T elements[], XnUInt32 nCount)
75 {
76 BubbleSort(elements, nCount, DefaultComparer);
77 }
78
79};
80
81#endif // __XN_ALGORITHMS_H__
XnBool DefaultComparer(const T &arg1, const T &arg2)
Definition: XnAlgorithms.h:34
#define TRUE
Definition: XnPlatform.h:93
#define FALSE
Definition: XnPlatform.h:97
Definition: XnAlgorithms.h:40
static void BubbleSort(T elements[], XnUInt32 nCount)
Definition: XnAlgorithms.h:74
static void BubbleSort(T elements[], XnUInt32 nCount, Comparer comp)
Definition: XnAlgorithms.h:43