16#ifndef AEONGAMES_UTILITIES_H
17#define AEONGAMES_UTILITIES_H
33 DLL
bool FileExists (
const std::string& aFilePath );
37 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
38 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
44 constexpr uint32_t
ffs ( uint32_t value )
46 return DeBruijnSequence[ ( ( ( value & ( -
static_cast<int64_t
> ( value ) ) ) * 0x077CB531ULL ) & 0xFFFFFFFF ) >> 27];
48 static_assert (
ffs ( 0x1 ) == 0,
"Find First Bit Set Failure." );
49 static_assert (
ffs ( 0x2 ) == 1,
"Find First Bit Set Failure." );
50 static_assert (
ffs ( 0x4 ) == 2,
"Find First Bit Set Failure." );
51 static_assert (
ffs ( 0x8 ) == 3,
"Find First Bit Set Failure." );
52 static_assert (
ffs ( 0x10 ) == 4,
"Find First Bit Set Failure." );
53 static_assert (
ffs ( 0x20 ) == 5,
"Find First Bit Set Failure." );
54 static_assert (
ffs ( 0x40 ) == 6,
"Find First Bit Set Failure." );
55 static_assert (
ffs ( 0x80 ) == 7,
"Find First Bit Set Failure." );
56 static_assert (
ffs ( 0x100 ) == 8,
"Find First Bit Set Failure." );
57 static_assert (
ffs ( 0x200 ) == 9,
"Find First Bit Set Failure." );
58 static_assert (
ffs ( 0x400 ) == 10,
"Find First Bit Set Failure." );
59 static_assert (
ffs ( 0x800 ) == 11,
"Find First Bit Set Failure." );
60 static_assert (
ffs ( 0x1000 ) == 12,
"Find First Bit Set Failure." );
61 static_assert (
ffs ( 0x2000 ) == 13,
"Find First Bit Set Failure." );
62 static_assert (
ffs ( 0x4000 ) == 14,
"Find First Bit Set Failure." );
63 static_assert (
ffs ( 0x8000 ) == 15,
"Find First Bit Set Failure." );
64 static_assert (
ffs ( 0x10000 ) == 16,
"Find First Bit Set Failure." );
65 static_assert (
ffs ( 0x20000 ) == 17,
"Find First Bit Set Failure." );
66 static_assert (
ffs ( 0x40000 ) == 18,
"Find First Bit Set Failure." );
67 static_assert (
ffs ( 0x80000 ) == 19,
"Find First Bit Set Failure." );
68 static_assert (
ffs ( 0x100000 ) == 20,
"Find First Bit Set Failure." );
69 static_assert (
ffs ( 0x200000 ) == 21,
"Find First Bit Set Failure." );
70 static_assert (
ffs ( 0x400000 ) == 22,
"Find First Bit Set Failure." );
71 static_assert (
ffs ( 0x800000 ) == 23,
"Find First Bit Set Failure." );
72 static_assert (
ffs ( 0x1000000 ) == 24,
"Find First Bit Set Failure." );
73 static_assert (
ffs ( 0x2000000 ) == 25,
"Find First Bit Set Failure." );
74 static_assert (
ffs ( 0x4000000 ) == 26,
"Find First Bit Set Failure." );
75 static_assert (
ffs ( 0x8000000 ) == 27,
"Find First Bit Set Failure." );
76 static_assert (
ffs ( 0x10000000 ) == 28,
"Find First Bit Set Failure." );
77 static_assert (
ffs ( 0x20000000 ) == 29,
"Find First Bit Set Failure." );
78 static_assert (
ffs ( 0x40000000 ) == 30,
"Find First Bit Set Failure." );
79 static_assert (
ffs ( 0x80000000 ) == 31,
"Find First Bit Set Failure." );
80 static_assert (
ffs ( ~0x1 ) == 1,
"Find First Bit Set Failure." );
81 static_assert (
ffs ( ~0x3 ) == 2,
"Find First Bit Set Failure." );
82 static_assert (
ffs ( ~0x7 ) == 3,
"Find First Bit Set Failure." );
83 static_assert (
ffs ( ~0xf ) == 4,
"Find First Bit Set Failure." );
84 static_assert (
ffs ( ~0x1f ) == 5,
"Find First Bit Set Failure." );
85 static_assert (
ffs ( ~0x3f ) == 6,
"Find First Bit Set Failure." );
86 static_assert (
ffs ( ~0x7f ) == 7,
"Find First Bit Set Failure." );
87 static_assert (
ffs ( ~0xff ) == 8,
"Find First Bit Set Failure." );
88 static_assert (
ffs ( ~0x1ff ) == 9,
"Find First Bit Set Failure." );
89 static_assert (
ffs ( ~0x3ff ) == 10,
"Find First Bit Set Failure." );
90 static_assert (
ffs ( ~0x7ff ) == 11,
"Find First Bit Set Failure." );
91 static_assert (
ffs ( ~0xfff ) == 12,
"Find First Bit Set Failure." );
92 static_assert (
ffs ( ~0x1fff ) == 13,
"Find First Bit Set Failure." );
93 static_assert (
ffs ( ~0x3fff ) == 14,
"Find First Bit Set Failure." );
94 static_assert (
ffs ( ~0x7fff ) == 15,
"Find First Bit Set Failure." );
95 static_assert (
ffs ( ~0xffff ) == 16,
"Find First Bit Set Failure." );
96 static_assert (
ffs ( ~0x1ffff ) == 17,
"Find First Bit Set Failure." );
97 static_assert (
ffs ( ~0x3ffff ) == 18,
"Find First Bit Set Failure." );
98 static_assert (
ffs ( ~0x7ffff ) == 19,
"Find First Bit Set Failure." );
99 static_assert (
ffs ( ~0xfffff ) == 20,
"Find First Bit Set Failure." );
100 static_assert (
ffs ( ~0x1fffff ) == 21,
"Find First Bit Set Failure." );
101 static_assert (
ffs ( ~0x3fffff ) == 22,
"Find First Bit Set Failure." );
102 static_assert (
ffs ( ~0x7fffff ) == 23,
"Find First Bit Set Failure." );
103 static_assert (
ffs ( ~0xffffff ) == 24,
"Find First Bit Set Failure." );
104 static_assert (
ffs ( ~0x1ffffff ) == 25,
"Find First Bit Set Failure." );
105 static_assert (
ffs ( ~0x3ffffff ) == 26,
"Find First Bit Set Failure." );
106 static_assert (
ffs ( ~0x7ffffff ) == 27,
"Find First Bit Set Failure." );
107 static_assert (
ffs ( ~0xfffffff ) == 28,
"Find First Bit Set Failure." );
108 static_assert (
ffs ( ~0x1fffffff ) == 29,
"Find First Bit Set Failure." );
109 static_assert (
ffs ( ~0x3fffffff ) == 30,
"Find First Bit Set Failure." );
110 static_assert (
ffs ( ~0x7fffffff ) == 31,
"Find First Bit Set Failure." );
118 return ( ( ( ( ( ( v - ( ( v >> 1 ) & 0x55555555 ) ) & 0x33333333 ) +
119 ( ( ( v - ( ( v >> 1 ) & 0x55555555 ) ) >> 2 ) & 0x33333333 ) ) +
120 ( ( ( ( v - ( ( v >> 1 ) & 0x55555555 ) ) & 0x33333333 ) +
121 ( ( ( v - ( ( v >> 1 ) & 0x55555555 ) ) >> 2 ) & 0x33333333 ) ) >> 4 ) ) &
122 0xF0F0F0F ) * 0x1010101 ) >> 24;
124 static_assert (
popcount ( 0x0 ) == 0,
"Popcount Failure." );
125 static_assert (
popcount ( 0x1 ) == 1,
"Popcount Failure." );
126 static_assert (
popcount ( 0x2 ) == 1,
"Popcount Failure." );
127 static_assert (
popcount ( 0x3 ) == 2,
"Popcount Failure." );
128 static_assert (
popcount ( 0x4 ) == 1,
"Popcount Failure." );
129 static_assert (
popcount ( 0x5 ) == 2,
"Popcount Failure." );
130 static_assert (
popcount ( 0x6 ) == 2,
"Popcount Failure." );
131 static_assert (
popcount ( 0x7 ) == 3,
"Popcount Failure." );
132 static_assert (
popcount ( 0x8 ) == 1,
"Popcount Failure." );
133 static_assert (
popcount ( 0x9 ) == 2,
"Popcount Failure." );
134 static_assert (
popcount ( 0xa ) == 2,
"Popcount Failure." );
135 static_assert (
popcount ( 0xb ) == 3,
"Popcount Failure." );
136 static_assert (
popcount ( 0xc ) == 2,
"Popcount Failure." );
137 static_assert (
popcount ( 0xd ) == 3,
"Popcount Failure." );
138 static_assert (
popcount ( 0xe ) == 3,
"Popcount Failure." );
139 static_assert (
popcount ( 0xf ) == 4,
"Popcount Failure." );
147 template<
class T,
class U>
150 T result ( aFirst.begin(), aFirst.end() );
151 result.insert ( result.end(), aSecond.begin(), aSecond.end() );
165 DLL
OptionHandler (
const char aShortOption,
const char* aLongOption,
void ( *aHandler ) (
const char*,
void* ),
void* aUserData =
nullptr );
177 DLL
void operator() (
const char* aArgument,
void* aUserData )
const;
179 void ( *mHandler ) (
const char*,
void* );
180 const char mShortOption{};
181 const char* mLongOption{
nullptr};
182 void* mUserData{
nullptr};
Command-line option handler.
DLL void * GetUserData() const
Get the user data pointer.
DLL const char * GetLongOption() const
Get the long option string.
DLL const char GetShortOption() const
Get the short option character.
DLL void operator()(const char *aArgument, void *aUserData) const
Invoke the handler callback.
DLL OptionHandler(const char aShortOption, const char *aLongOption, void(*aHandler)(const char *, void *), void *aUserData=nullptr)
Construct an OptionHandler.
<- This is here just for the literals
DLL void ProcessOpts(int argc, char *argv[], const OptionHandler *aOptionHandler, size_t aOptionHandlerCount)
Process command-line options.
const T Concatenate(const T &aFirst, U &aSecond)
Concatenate two containers.
DLL bool FileExists(const std::string &aFilePath)
Check whether a file exists at the given path.
constexpr uint32_t ffs(uint32_t value)
Find the index of the first set bit using a De Bruijn sequence.
DLL const std::string GetFileExtension(const std::string &aFilePath)
Extract the file extension from a file path.
constexpr uint32_t DeBruijnSequence[32]
De Bruijn lookup table used by ffs().
constexpr uint32_t popcount(uint32_t v)
Count the number of set bits in a 32-bit unsigned integer.