Qx v0.5.8
Qt Extensions Library
Loading...
Searching...
No Matches
qx-array.h
1#ifndef QX_ARRAY_H
2#define QX_ARRAY_H
3
4// Qt Includes
5#include <QHash>
6
7// Extra-component Includes
9
10// TODO: These functions are pretty underwhelming, see if more flexible ones that make use
11// of range/iterator based functions from std can be made instead.
12
13namespace Qx
14{
15
16class Array
17{
18//-Class Functions----------------------------------------------------------------------------------------------
19public:
20 template <typename T, int N>
21 static constexpr int constDim(const T(&)[N]) { return N; } // Allows using the size of a const array at runtime
22
23 template <typename T, int N>
24 static int indexOf(const T(&array) [N], const T query)
25 {
26 for(int i = 0; i < N; i++)
27 if(array[i] == query)
28 return i;
29
30 return -1;
31 }
32
33 template<typename T, int N>
34 requires arithmetic<T>
35 static T maxOf(const T(&array) [N])
36 {
37 T max = array[0];
38
39 for(int i = 1; i < N; i++)
40 if(array[i] > max)
41 max = array[i];
42
43 return max;
44 }
45
46 template<typename T, int N>
47 requires arithmetic<T>
48 static T minOf(const T(&array) [N])
49 {
50 T min = array[0];
51
52 for(int i = 1; i < N; i++)
53 if(array[i] < min)
54 min = array[i];
55
56 return min;
57 }
58
59 template<typename T, int N>
60 static T mostFrequent(const T(&array) [N])
61 {
62 /* TODO: The use of a hash here means that if more
63 * than one element is tied for the highest frequency that
64 * the one this function returns will essentially be random
65 * instead of something well defined like always being
66 * the first one.
67 */
68
69 // Load all array elements into a hash
70 QHash<T,int> hash;
71 for(int i = 0; i < N; i++)
72 hash[array[i]]++;
73
74 // Determine greatest frequency
75 int maxFreq = 0;
76 T maxFreqVal = array[0]; // Assume first value is most frequent to start
78
79 while(i.hasNext())
80 {
81 i.next();
82 if(maxFreq < i.value())
83 {
84 maxFreqVal = i.key();
85 maxFreq = i.value();
86 }
87 }
88
89 return maxFreqVal;
90 }
91};
92
93}
94
95#endif // QX_ARRAY_H
The Array class is a collection of static functions pertaining to C-style arrays.
Definition qx-array.h:17
static T maxOf(const T(&array)[N])
Definition qx-array.h:35
static constexpr int constDim(const T(&)[N])
Definition qx-array.h:21
static int indexOf(const T(&array)[N], const T query)
Definition qx-array.h:24
static T minOf(const T(&array)[N])
Definition qx-array.h:48
static T mostFrequent(const T(&array)[N])
Definition qx-array.h:60
Specifies that a type is an arithmetic type.
Definition qx-concepts.h:497
The Qx namespace is the main namespace through which all non-global functionality of the Qx library i...
Definition qx-processwaiter.cpp:5
bool hasNext() const const
const Key & key() const const
QHashIterator< Key, T >::Item next()
const T & value() const const
The qx-concepts header file provides a library of general purpose concepts as an extension of the sta...