MADARA  3.1.8
Utility.inl
Go to the documentation of this file.
1 #ifndef _MADARA_UTILITY_INL_
2 #define _MADARA_UTILITY_INL_
3 
4 #include "Utility.h"
5 
6 inline
8  const std::string & prefix)
9 {
10  bool result = false;
11 
12  if (prefix.length () <= input.length ())
13  {
14  result = input.substr (0, prefix.length ()) == prefix;
15  }
16 
17  return result;
18 }
19 
20 inline
22  const std::string & match)
23 {
24  bool result = false;
25 
26  if (match.length () <= input.length ())
27  {
28  result =
29  input.substr (input.size () - match.length (), match.length ()) == match;
30  }
31 
32  return result;
33 }
34 
35 
36 template <typename T>
37 T madara::utility::bitmask_add (T mask, T values)
38 {
39  return mask | values;
40 }
41 
45 template <typename T>
46 bool madara::utility::bitmask_check (T mask, T values)
47 {
48  return (mask & values) > 0;
49 }
50 
54 template <typename T>
55 T madara::utility::bitmask_remove (T mask, T values)
56 {
57  return mask & ~values;
58 }
59 
60 
61 template <typename T>
62 bool
63 madara::utility::less_compare (const T & left, const T & right)
64 {
65  return left < right;
66 }
67 
68 template <typename T>
69 bool
70 madara::utility::greater_compare (const T & left, const T & right)
71 {
72  return right < left;
73 }
74 
75 template <typename T>
76 void
77 madara::utility::sift_down (T * input, int start, int end,
78  bool (*comparator) (const T & left, const T & right))
79 {
80  int root = start;
81  for (int child = root * 2 + 1; child <= end; child = root * 2 + 1)
82  {
83  int swap = root;
84 
85  // check left child
86  if (comparator (input[child], input[swap]))
87  swap = child;
88 
89  // check right child
90  ++child;
91  if (child <= end && comparator (input[child], input[swap]))
92  swap = child;
93 
94  if (swap != root)
95  {
96  std::swap (input[root], input[swap]);
97  root = swap;
98  }
99  else
100  break;
101  }
102 }
103 
104 template <typename T>
105 void
106 madara::utility::heapify (T * input, int size,
107  bool (*comparator) (const T & left, const T & right))
108 {
109  // start at lowest parent node and work our way back
110  for (int start = (size - 2) / 2; start >= 0; --start)
111  {
112  sift_down (input, start, size - 1, comparator);
113  }
114 }
115 
116 template <typename T>
117 void
118 madara::utility::heap_sort (T * input, int size,
119  bool (*comparator) (const T & left, const T & right))
120 {
121  heapify (input, size, comparator);
122 
123  for (int end = size - 1; end > 0; --end)
124  {
125  std::swap (input[end], input[0]);
126  sift_down (input, 0, end - 1);
127  }
128 }
129 
130 #endif // _MADARA_UTILITY_INL_
131 
bool bitmask_check(T mask, T values)
Returns true if mask contains values.
Definition: Utility.inl:46
T bitmask_add(T mask, T values)
Adds values to a bit mask.
Definition: Utility.inl:37
void sift_down(T *input, int start, int end, bool(*comparator)(const T &left, const T &right)=greater_compare< T >)
Sifts elements down a heap according to a comparator.
Definition: Utility.inl:77
bool greater_compare(const T &left, const T &right)
Returns true if right < left.
Definition: Utility.inl:70
void heap_sort(T *input, int size, bool(*comparator)(const T &left, const T &right)=greater_compare< T >)
Sorts an array with heap sort.
Definition: Utility.inl:118
void heapify(T *input, int size, bool(*comparator)(const T &left, const T &right)=greater_compare< T >)
Builds a heap out of an array of elements.
Definition: Utility.inl:106
static constexpr struct madara::knowledge::tags::string_t string
T bitmask_remove(T mask, T values)
Removes values from a bit mask.
Definition: Utility.inl:55
bool less_compare(const T &left, const T &right)
Returns true if left < right.
Definition: Utility.inl:63
MADARA_Export bool begins_with(const std::string &input, const std::string &prefix)
Check if input contains prefix at the beginning.
Definition: Utility.inl:7
MADARA_Export bool ends_with(const std::string &input, const std::string &ending)
Check if input contains a pattern at the end.
Definition: Utility.inl:21