Apollo  6.0
Open source self driving car software
i_sort.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 #pragma once
17 
19 
20 namespace apollo {
21 namespace perception {
22 namespace common {
23 
24 // Strictly less
25 template <typename T>
26 inline bool ILess(const T &a, const T &b) {
27  return a < b;
28 }
29 
30 // Strictly larger
31 template <typename T>
32 inline bool ILarger(const T &a, const T &b) {
33  return a > b;
34 }
35 
36 template <typename T>
37 inline T IMedian3(const T &a, const T &b, const T &c) {
38  if (a <= b) {
39  if (c > a) {
40  if (c <= b) {
41  return (c);
42  }
43  return (b);
44  }
45  return (a);
46  }
47  if (c > b) {
48  if (c <= a) {
49  return (c);
50  }
51  return (a);
52  }
53  return (b);
54 }
55 
56 // Insertion sort
57 template <typename T, bool Compare(const T &, const T &)>
58 inline void IInsertionSort(T *a, int n) {
59  int i, j;
60  T tmp;
61  for (i = 1; i < n; i++) {
62  IMove(a[i], &tmp);
63  for (j = i; j > 0; j--) {
64  if (Compare(a[j - 1], tmp)) {
65  break;
66  }
67  IMove(a[j - 1], &a[j]);
68  }
69  IMove(tmp, &a[j]);
70  }
71 }
72 
73 // Move the indexed elements to the left hand side of array "a" in place.
74 // Array "a" has size "n * element_size".
75 // Array "indices" has valid size "nr_indexed_elements",
76 // its length should be >= "nr_indexed_elements".
77 // Array a and indices will be destroyed after calling this routine
78 // Move the indexed elements to the left hand side of array "a" in place
79 template <typename T>
80 inline void IIndexedShuffle(T *a, int *indices, int n, int element_size,
81  int nr_indexed_elements,
82  bool is_sort_indices = true) {
83  // pre-sort indices array into ascending order
84  if (is_sort_indices) {
85  IInsertionSort<int, ILess>(indices, nr_indexed_elements);
86  }
87  // move indexed elements to the left hand side of a
88  int j = IMin(n, nr_indexed_elements);
89  int m = IMin(n, nr_indexed_elements);
90  for (int i = 0; i < m; ++i) {
91  j = indices[i];
92  if (j != i) {
93  ISwap(a + i * element_size, a + j * element_size, element_size);
94  // swap indexed elements to the left
95  }
96  }
97 }
98 
99 // Move the indexed elements to the left hand side of array "a" and "b" in
100 // place.
101 // Array "a" has size "n*element_size_a".
102 // Array "b" has size "n*element_size_b".
103 // Array "indices" has valid size "nr_indexed_elements",
104 // its length should be >= "nr_indexed_elements".
105 // Array a, b, and indices will be destroyed after calling this routine
106 template <typename T>
107 inline void IIndexedShuffle(T *a, T *b, int *indices, int n, int element_size_a,
108  int element_size_b, int nr_indexed_elements,
109  bool is_sort_indices = true) {
110  // pre-sort indices array into ascending order
111  if (n <= 1) {
112  return;
113  }
114  if (is_sort_indices) {
115  IInsertionSort<int, ILess>(indices, nr_indexed_elements);
116  }
117  // move indexed elements to the left hand side of a
118  int j = IMin(n, nr_indexed_elements);
119  int m = IMin(n, nr_indexed_elements);
120  for (int i = 0; i < m; ++i) {
121  j = indices[i];
122  if (j != i) {
123  ISwap(a + i * element_size_a, a + j * element_size_a, element_size_a);
124  // swap indexed elements to the left
125  ISwap(b + i * element_size_b, b + j * element_size_b, element_size_b);
126  // swap indexed elements to the left
127  }
128  }
129 }
130 
131 } // namespace common
132 } // namespace perception
133 } // namespace apollo
void IMove(const T &a, T *b)
Definition: i_basic.h:566
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
T IMedian3(const T &a, const T &b, const T &c)
Definition: i_sort.h:37
void IInsertionSort(T *a, int n)
Definition: i_sort.h:58
bool ILarger(const T &a, const T &b)
Definition: i_sort.h:32
void ISwap(T &a, T &b)
Definition: i_basic.h:299
bool ILess(const T &a, const T &b)
Definition: i_sort.h:26
T IMin(T a, T b)
Definition: i_basic.h:155
void IIndexedShuffle(T *a, int *indices, int n, int element_size, int nr_indexed_elements, bool is_sort_indices=true)
Definition: i_sort.h:80