Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Merge branch 'develop' into 'master'
Browse files Browse the repository at this point in the history
Release 1.1.0

See merge request ii887522/nitro!53
  • Loading branch information
ii887522 committed May 15, 2021
2 parents 920474d + 6405f16 commit 1e937a3
Show file tree
Hide file tree
Showing 29 changed files with 1,462 additions and 15 deletions.
15 changes: 13 additions & 2 deletions nitro/nitro.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,38 @@
<ClInclude Include="src\main\Any\allocator.h" />
<ClInclude Include="src\main\Any\AnimatedAny.h" />
<ClInclude Include="src\main\Any\Reactive.h" />
<ClInclude Include="src\main\Functions\fs_ext.h" />
<ClInclude Include="src\main\Functions\math_ext.h" />
<ClInclude Include="src\main\Functions\queue_ext.h" />
<ClInclude Include="src\main\Functions\string_ext.h" />
<ClInclude Include="src\main\Functions\util.h" />
<ClInclude Include="src\main\Reactive\BinaryReactive.h" />
<ClInclude Include="src\main\Reactive\UnaryReactive.h" />
<ClInclude Include="src\main\Struct\Range.h" />
<ClInclude Include="src\main\Struct\Vector.h" />
<ClInclude Include="src\test\Any\AnimatedAny.test.h" />
<ClInclude Include="src\test\Any\Reactive.test.h" />
<ClInclude Include="src\test\Functions\fs_ext.test.h" />
<ClInclude Include="src\test\Functions\math_ext.test.h" />
<ClInclude Include="src\test\Functions\queue_ext.test.h" />
<ClInclude Include="src\test\Functions\string_ext.test.h" />
<ClInclude Include="src\test\Functions\util.test.h" />
<ClInclude Include="src\test\Reactive\BinaryReactive.test.h" />
<ClInclude Include="src\test\Reactive\UnaryReactive.test.h" />
<ClInclude Include="src\test\Struct\Vector.test.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\main\Allocator\linear_allocator.h" />
<ClCompile Include="src\main\Functions\math_ext.cpp" />
<ClCompile Include="src\main\Functions\string_ext.cpp" />
<ClCompile Include="src\test\Any\AnimatedAny.test.cpp" />
<ClCompile Include="src\test\Any\Reactive.test.cpp" />
<ClCompile Include="src\test\Functions\fs_ext.test.cpp" />
<ClCompile Include="src\test\Functions\math_ext.test.cpp" />
<ClCompile Include="src\test\Functions\queue_ext.test.cpp" />
<ClCompile Include="src\test\Functions\string_ext.test.cpp" />
<ClCompile Include="src\test\Functions\test.cpp" />
<ClCompile Include="src\test\Functions\util.test.cpp" />
<ClCompile Include="src\test\Reactive\BinaryReactive.test.cpp" />
<ClCompile Include="src\test\Reactive\UnaryReactive.test.cpp" />
<ClCompile Include="src\test\Struct\Vector.test.cpp" />
Expand Down Expand Up @@ -163,7 +174,7 @@
<ClCompile>
<WarningLevel>Level4</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>TEST;LINEAR_ALLOCATOR;LONG_TERM_ALLOCATOR_SIZE=5304u;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>TEST;LINEAR_ALLOCATOR;LONG_TERM_ALLOCATOR_SIZE=16384u;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpplatest</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
Expand Down Expand Up @@ -216,7 +227,7 @@
<ClCompile>
<WarningLevel>Level4</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>TEST;LINEAR_ALLOCATOR;LONG_TERM_ALLOCATOR_SIZE=8168u;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>TEST;LINEAR_ALLOCATOR;LONG_TERM_ALLOCATOR_SIZE=32768u;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpplatest</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
Expand Down
33 changes: 33 additions & 0 deletions nitro/nitro.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,24 @@
<ClInclude Include="src\main\Any\allocator.h">
<Filter>Header Files\Any</Filter>
</ClInclude>
<ClInclude Include="src\main\Functions\fs_ext.h">
<Filter>Header Files\Functions</Filter>
</ClInclude>
<ClInclude Include="src\main\Functions\string_ext.h">
<Filter>Header Files\Functions</Filter>
</ClInclude>
<ClInclude Include="src\main\Functions\util.h">
<Filter>Header Files\Functions</Filter>
</ClInclude>
<ClInclude Include="src\test\Functions\fs_ext.test.h">
<Filter>Test Header Files\Functions</Filter>
</ClInclude>
<ClInclude Include="src\test\Functions\string_ext.test.h">
<Filter>Test Header Files\Functions</Filter>
</ClInclude>
<ClInclude Include="src\test\Functions\util.test.h">
<Filter>Test Header Files\Functions</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\test\Functions\math_ext.test.cpp">
Expand Down Expand Up @@ -133,5 +151,20 @@
<ClCompile Include="src\main\Allocator\linear_allocator.h">
<Filter>Header Files\Allocator</Filter>
</ClCompile>
<ClCompile Include="src\main\Functions\math_ext.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\main\Functions\string_ext.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\test\Functions\fs_ext.test.cpp">
<Filter>Test Source Files\Functions</Filter>
</ClCompile>
<ClCompile Include="src\test\Functions\string_ext.test.cpp">
<Filter>Test Source Files\Functions</Filter>
</ClCompile>
<ClCompile Include="src\test\Functions\util.test.cpp">
<Filter>Test Source Files\Functions</Filter>
</ClCompile>
</ItemGroup>
</Project>
Empty file added nitro/res/test/a.dat
Empty file.
1 change: 1 addition & 0 deletions nitro/res/test/b.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
1 change: 1 addition & 0 deletions nitro/res/test/c.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ab
1 change: 1 addition & 0 deletions nitro/res/test/d.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
O�Z
1 change: 1 addition & 0 deletions nitro/res/test/e.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
O�Z�{�
Empty file added nitro/res/test/f.dat
Empty file.
Binary file added nitro/res/test/g.dat
Binary file not shown.
Binary file added nitro/res/test/h.dat
Binary file not shown.
Binary file added nitro/res/test/i.dat
Binary file not shown.
Binary file added nitro/res/test/j.dat
Binary file not shown.
35 changes: 35 additions & 0 deletions nitro/src/main/Functions/fs_ext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright ii887522

#ifndef NITRO_SRC_MAIN_FUNCTIONS_FS_EXT_H_
#define NITRO_SRC_MAIN_FUNCTIONS_FS_EXT_H_

#include <string>
#include <fstream>

using std::string;
using std::ifstream;
using std::ofstream;
using std::ios;

namespace ii887522::nitro {

// Param filePath: it must exist
template <typename T, template <typename> typename U> U<T> read(const string& filePath) {
ifstream file{ filePath, ios::binary | ios::ate };
file.exceptions(file.failbit);
U<T> fileContent(static_cast<unsigned int>(file.tellg()) / sizeof T);
file.seekg(0u);
file.read(reinterpret_cast<char*>(fileContent.data()), fileContent.size() * sizeof T);
file.close();
return fileContent;
}

template <typename T, template <typename> typename U> void write(const string& filePath, const U<T>& objects) {
ofstream file{ filePath, ios::binary };
file.write(reinterpret_cast<const char*>(objects.data()), objects.size() * sizeof T);
file.close();
}

} // namespace ii887522::nitro

#endif // NITRO_SRC_MAIN_FUNCTIONS_FS_EXT_H_
28 changes: 28 additions & 0 deletions nitro/src/main/Functions/math_ext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright ii887522

#include "math_ext.h" // NOLINT(build/include_subdir)
#include <string>

using std::string;

namespace ii887522::nitro {

bool isUint(const string& str) {
for (const auto ch : str) {
if (!isDigit(ch)) return false;
}
return true;
}

unsigned int parseUint(const string& uintStr) {
auto result{ 0u };
auto placeValue{ 1u };
constexpr auto radix{ 10u };
for (auto i{ static_cast<int>(uintStr.length() - 1u) }; i >= 0; --i) {
result += parseUint(uintStr[i]) * placeValue;
placeValue *= radix;
}
return result;
}

} // namespace ii887522::nitro
21 changes: 21 additions & 0 deletions nitro/src/main/Functions/math_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
#define NITRO_SRC_MAIN_FUNCTIONS_MATH_EXT_H_

#include <algorithm>
#include <string>
#include "../Struct/Range.h"

using std::string;

namespace ii887522::nitro {

// Param value: it must not be assigned to nullptr and integer
Expand All @@ -30,6 +33,24 @@ template <typename T> constexpr T max(const T& l, const T& r) {
return l > r ? l : r;
}

constexpr bool isDigit(const char ch) {
return ch >= '0' && ch <= '9';
}

bool isUint(const string&);

constexpr unsigned int parseUint(const char ch) {
return ch - '0';
}

unsigned int parseUint(const string& uintStr);

// Power of Two is a number that is equal to 2^n where n is an integer.
template <typename T> bool isPowerOfTwo(const T& n) {
const auto power{ log2f(static_cast<float>(n)) };
return power == static_cast<int>(power);
}

} // namespace ii887522::nitro

#endif // NITRO_SRC_MAIN_FUNCTIONS_MATH_EXT_H_
28 changes: 28 additions & 0 deletions nitro/src/main/Functions/string_ext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright ii887522

#include "string_ext.h" // NOLINT(build/include_subdir)
#include <string>

using std::string;

namespace ii887522::nitro {

size_t getLastPathDelimiterI(const string& filePath) {
auto result{ filePath.rfind('/') };
return result == string::npos ? filePath.rfind('\\') : result;
}

string getFileName(const string& filePath) {
const auto lastPathDelimiterI{ getLastPathDelimiterI(filePath) };
return filePath.substr(lastPathDelimiterI + 1u, filePath.rfind('.') - lastPathDelimiterI - 1u);
}

void toUpperCase(string*const str) {
for (auto& ch : *str) ch = toUpperCase(ch);
}

void toLowerCase(string*const str) {
for (auto& ch : *str) ch = toLowerCase(ch);
}

} // namespace ii887522::nitro
31 changes: 31 additions & 0 deletions nitro/src/main/Functions/string_ext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright ii887522

#ifndef NITRO_SRC_MAIN_FUNCTIONS_STRING_EXT_H_
#define NITRO_SRC_MAIN_FUNCTIONS_STRING_EXT_H_

#include <string>

using std::string;

namespace ii887522::nitro {

size_t getLastPathDelimiterI(const string& filePath);
string getFileName(const string& filePath);

constexpr char toUpperCase(const char ch) {
return ch >= 'a' && ch <= 'z' ? ch - 32u : ch;
}

constexpr char toLowerCase(const char ch) {
return ch >= 'A' && ch <= 'Z' ? ch + 32u : ch;
}

// Param str: it must not be assigned to nullptr or integer
void toUpperCase(string*const str);

// Param str: it must not be assigned to nullptr or integer
void toLowerCase(string*const str);

} // namespace ii887522::nitro

#endif // NITRO_SRC_MAIN_FUNCTIONS_STRING_EXT_H_
99 changes: 99 additions & 0 deletions nitro/src/main/Functions/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright ii887522

#ifndef NITRO_SRC_MAIN_FUNCTIONS_UTIL_H_
#define NITRO_SRC_MAIN_FUNCTIONS_UTIL_H_

#include <functional>
#include <cstring>
#include <string>
#include "math_ext.h" // NOLINT(build/include_subdir)
#include "../Struct/Range.h"

using std::function;
using std::string;

namespace ii887522::nitro {

template <typename T> constexpr void swap(T& l, T& r) {
const T aux{ l };
l = r;
r = aux;
}

// Param objects: it must not be assigned to nullptr or integer and must not be an empty vector
// Param indices: min must be lower than objects.size(), max must be higher than or equal to min,
// max can be higher than or equal to objects.size()
// Param compare: it returns true if two objects passed in needs to be sorted
template <typename T, template <typename> typename U> void insertionSort(U<T>*const objects, const Range<unsigned int>& indices = Range{ 0u, UINT_MAX },
const function<bool(const T&, const T&)>& compare = [](const T& l, const T& r) {
return l > r;
}) {
const auto maxIndex{ min(indices.max, static_cast<unsigned int>(objects->size() - 1u)) };
for (auto i{ indices.min + 1u }; i <= maxIndex; ++i) {
for (auto j{ i }; j > indices.min; --j) {
if (compare((*objects)[j - 1u], (*objects)[j])) swap((*objects)[j - 1u], (*objects)[j]);
else
break;
}
}
}

// Param objects: it must not be assigned to nullptr or integer and must be an empty vector
// Param startStep:
// it is a starting number of consecutive items in objects that will become sorted
// it must be a power of two and higher than 1
// Param compare: it returns true if two objects passed in needs to be sorted
template <typename T, template <typename> typename U> void mergeSort(U<T>*const objects, const unsigned int startStep = 2u, const function<bool(const T&, const T&)>& compare =
[](const T& l, const T& r) {
return l > r;
}) {
U<T> aux(objects->size());
auto step{ startStep };
U<T>* currentObjects{ &aux };
U<T>* nextObjects{ objects };
while (true) {
for (auto i{ 0u }; i < objects->size(); i += step) {
auto l{ i };
auto r{ i + (step >> 1u) };
for (auto j{ i }; j != min(i + step, static_cast<unsigned int>(objects->size())); ++j) {
if (r < objects->size() && r != i + step && (l == i + (step >> 1u) || compare((*nextObjects)[l], (*nextObjects)[r]))) {
(*currentObjects)[j] = (*nextObjects)[r];
++r;
} else {
(*currentObjects)[j] = (*nextObjects)[l];
++l;
}
}
}
ii887522::nitro::swap(currentObjects, nextObjects); // NOLINT(build/include_what_you_use)
if (step >= objects->size()) break;
step <<= 1u;
}
if (currentObjects == objects) memcpy(objects->data(), aux.data(), objects->size() * sizeof T);
}

// Param objects: it must not be assigned to nullptr or integer and must not be an empty vector
// Param runSize:
// it is a number of consecutive items in objects that will become sorted
// it must be higher than 0
// Param compare: it returns true if two objects passed in needs to be sorted
template <typename T, template <typename> typename U> void insertionSorts(U<T>*const objects, const unsigned int runSize = UINT_MAX, const function<bool(const T&, const T&)>& compare =
[](const T& l, const T& r) {
return l > r;
}) {
for (auto i{ 0u }; i < objects->size(); i += runSize) insertionSort<T, U>(objects, Range{ i, i + runSize - 1u }, compare);
}

// Param objects: it must not be assigned to nullptr or integer and must not be an empty vector
// Param compare: it returns true if two objects passed in needs to be sorted
template <typename T, template <typename> typename U> void sort(U<T>*const objects, const function<bool(const T&, const T&)>& compare = [](const T& l, const T& r) { // NOLINT(build/include_what_you_use)
return l > r;
}) {
constexpr auto runSize{ 32u };
insertionSorts<T, U>(objects, runSize, compare);
mergeSort<T, U>(objects, runSize, compare);
}

} // namespace ii887522::nitro

#endif // NITRO_SRC_MAIN_FUNCTIONS_UTIL_H_
13 changes: 3 additions & 10 deletions nitro/src/main/Struct/Range.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@
namespace ii887522::nitro {

template <typename T> struct Range final {
// remove copy semantics
Range(const Range&) = delete;
Range& operator=(const Range&) = delete;

// remove move semantics
Range(Range&&) = delete;
Range& operator=(Range&&) = delete;

const T min;
const T max;
T min;
T max;

public:
explicit constexpr Range() { }
explicit constexpr Range(const T& min, const T& max) : min{ min }, max{ max } { }
};

Expand Down
Loading

0 comments on commit 1e937a3

Please sign in to comment.