OpenNI 1.5.7
XnAlgorithms.h
Go to the documentation of this file.
1 /*****************************************************************************
2 * *
3 * OpenNI 1.x Alpha *
4 * Copyright (C) 2012 PrimeSense Ltd. *
5 * *
6 * This file is part of OpenNI. *
7 * *
8 * Licensed under the Apache License, Version 2.0 (the "License"); *
9 * you may not use this file except in compliance with the License. *
10 * You may obtain a copy of the License at *
11 * *
12 * http://www.apache.org/licenses/LICENSE-2.0 *
13 * *
14 * Unless required by applicable law or agreed to in writing, software *
15 * distributed under the License is distributed on an "AS IS" BASIS, *
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
17 * See the License for the specific language governing permissions and *
18 * limitations under the License. *
19 * *
20 *****************************************************************************/
21 #ifndef __XN_ALGORITHMS_H__
22 #define __XN_ALGORITHMS_H__
23 
24 //---------------------------------------------------------------------------
25 // Includes
26 //---------------------------------------------------------------------------
27 #include <XnPlatform.h>
28 
29 //---------------------------------------------------------------------------
30 // Types
31 //---------------------------------------------------------------------------
32 template<class T>
33 XnBool DefaultComparer(const T& arg1, const T& arg2)
34 {
35  return arg1 < arg2;
36 }
37 
39 {
40 public:
41  template<class T, class Comparer>
42  static void BubbleSort(T elements[], XnUInt32 nCount, Comparer comp)
43  {
44  XnUInt32 n = nCount;
45  XnBool bSwapped;
46  T temp;
47 
48  if (nCount == 0)
49  return;
50 
51  do
52  {
53  bSwapped = FALSE;
54  for (XnUInt32 i = 0; i < n - 1; ++i)
55  {
56  if (!comp(elements[i], elements[i+1]))
57  {
58  // swap
59  temp = elements[i];
60  elements[i] = elements[i+1];
61  elements[i+1] = temp;
62 
63  bSwapped = TRUE;
64  }
65  }
66 
67  n -= 1;
68 
69  } while (bSwapped);
70  }
71 
72  template<class T>
73  static void BubbleSort(T elements[], XnUInt32 nCount)
74  {
75  BubbleSort(elements, nCount, DefaultComparer);
76  }
77 
78 };
79 
80 #endif // __XN_ALGORITHMS_H__
#define FALSE
Definition: XnPlatform.h:84
#define TRUE
Definition: XnPlatform.h:80
XnBool DefaultComparer(const T &arg1, const T &arg2)
Definition: XnAlgorithms.h:33
static void BubbleSort(T elements[], XnUInt32 nCount, Comparer comp)
Definition: XnAlgorithms.h:42
Definition: XnAlgorithms.h:38
static void BubbleSort(T elements[], XnUInt32 nCount)
Definition: XnAlgorithms.h:73