savepoint, changelog later..

This commit is contained in:
Dmitry 2019-07-27 17:36:24 +03:00 committed by jeefo
commit 1bc1fd1913
45 changed files with 12866 additions and 10981 deletions

78
include/crlib/cr-alloc.h Normal file
View file

@ -0,0 +1,78 @@
//
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team.
//
// This software is licensed under the BSD-style license.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://yapb.ru/license
//
#pragma once
#include <new>
#include <crlib/cr-basic.h>
#include <crlib/cr-movable.h>
#include <crlib/cr-platform.h>
CR_NAMESPACE_BEGIN
// default allocator for cr-objects
class Allocator : public Singleton <Allocator> {
public:
Allocator () = default;
~Allocator () = default;
public:
template <typename T> T *allocate (const size_t length = 1) {
auto ptr = reinterpret_cast <T *> (calloc (length, sizeof (T)));
if (!ptr) {
plat.abort ();
}
// calloc on linux with debug enabled doesn't always zero out memory
#if defined (CR_DEBUG) && !defined (CR_WINDOWS)
auto *zeroing = reinterpret_cast <uint8 *> (ptr);
for (size_t i = 0; i < length; ++i) {
zeroing[i] = 0;
}
#endif
return ptr;
}
template <typename T> void deallocate (T *ptr) {
if (ptr) {
free (reinterpret_cast <T *> (ptr));
}
}
public:
template <typename T, typename ...Args> void construct (T *d, Args &&...args) {
new (d) T (cr::forward <Args> (args)...);
}
template <typename T> void destruct (T *d) {
d->~T ();
}
public:
template <typename T, typename ...Args> T *create (Args &&...args) {
auto d = allocate <T> ();
new (d) T (cr::forward <Args> (args)...);
return d;
}
template <typename T> void destroy (T *ptr) {
if (ptr) {
destruct (ptr);
deallocate (ptr);
}
}
};
static auto &alloc = Allocator::get ();
CR_NAMESPACE_END

390
include/crlib/cr-array.h Normal file
View file

@ -0,0 +1,390 @@
//
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team.
//
// This software is licensed under the BSD-style license.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://yapb.ru/license
//
#pragma once
#include <crlib/cr-basic.h>
#include <crlib/cr-alloc.h>
#include <crlib/cr-movable.h>
#include <crlib/cr-random.h>
// policy to reserve memory
CR_DECLARE_SCOPED_ENUM (ReservePolicy,
MultipleByTwo,
PlusOne
);
CR_NAMESPACE_BEGIN
// simple array class like std::vector
template <typename T, ReservePolicy R = ReservePolicy::MultipleByTwo> class Array : public DenyCopying {
public:
T *m_data = nullptr;
size_t m_capacity = 0;
size_t m_length = 0;
public:
explicit Array () = default;
Array (const size_t amount) {
reserve (amount);
}
Array (Array &&rhs) noexcept {
m_data = rhs.m_data;
m_length = rhs.m_length;
m_capacity = rhs.m_capacity;
rhs.reset ();
}
~Array () {
destroy ();
}
private:
void destructElements () noexcept {
for (size_t i = 0; i < m_length; ++i) {
alloc.destruct (&m_data[i]);
}
}
void transferElements (T *dest, T *src, size_t length) noexcept {
for (size_t i = 0; i < length; ++i) {
alloc.construct (&dest[i], cr::move (src[i]));
alloc.destruct (&src[i]);
}
}
void destroy () {
destructElements ();
alloc.deallocate (m_data);
}
void reset () {
m_data = nullptr;
m_capacity = 0;
m_length = 0;
}
public:
bool reserve (const size_t amount) {
if (m_length + amount < m_capacity) {
return true;
}
auto capacity = m_capacity ? m_capacity : 8;
if (cr::fix (R == ReservePolicy::MultipleByTwo)) {
while (m_length + amount > capacity) {
capacity *= 2;
}
}
else {
capacity = amount + m_capacity + 1;
}
auto data = alloc.allocate <T> (capacity);
transferElements (data, m_data, m_length);
alloc.deallocate (m_data);
m_data = data;
m_capacity = capacity;
return true;
}
bool resize (const size_t amount) {
if (amount < m_length) {
while (amount < m_length) {
discard ();
}
}
else if (amount > m_length) {
if (!ensure (amount)) {
return false;
}
size_t pushLength = amount - m_length;
for (size_t i = 0; i < pushLength; ++i) {
push (T ());
}
}
return true;
}
bool ensure (const size_t amount) {
if (amount <= m_length) {
return true;
}
return reserve (amount - m_length);
}
size_t length () const {
return m_length;
}
size_t capacity () const {
return m_capacity;
}
template <typename U> bool set (size_t index, U &&object) {
if (index >= m_capacity) {
if (!reserve (index + 1)) {
return false;
}
}
alloc.construct (&m_data[index], cr::forward <U> (object));
if (index >= m_length) {
m_length = index + 1;
}
return true;
}
template <typename U> bool insert (size_t index, U &&object) {
return insert (index, &object, 1);
}
template <typename U> bool insert (size_t index, U *objects, size_t count = 1) {
if (!objects || !count) {
return false;
}
const size_t capacity = (m_length > index ? m_length : index) + count;
if (capacity >= m_capacity && !reserve (capacity)) {
return false;
}
if (index >= m_length) {
for (size_t i = 0; i < count; ++i) {
alloc.construct (&m_data[i + index], cr::forward <U> (objects[i]));
}
m_length = capacity;
}
else {
size_t i = 0;
for (i = m_length; i > index; --i) {
m_data[i + count - 1] = cr::move (m_data[i - 1]);
}
for (i = 0; i < count; ++i) {
alloc.construct (&m_data[i + index], cr::forward <U> (objects[i]));
}
m_length += count;
}
return true;
}
bool insert (size_t at, const Array &rhs) {
if (&rhs == this) {
return false;
}
return insert (at, &rhs.m_data[0], rhs.m_length);
}
bool erase (const size_t index, const size_t count) {
if (index + count > m_capacity) {
return false;
}
m_length -= count;
for (size_t i = index; i < m_length; ++i) {
m_data[i] = cr::move (m_data[i + count]);
}
return true;
}
bool shift () {
return erase (0, 1);
}
template <typename U> bool unshift (U &&object) {
return insert (0, &object);
}
bool remove (const T &object) {
return erase (index (object), 1);
}
template <typename U> bool push (U &&object) {
if (!reserve (1)) {
return false;
}
alloc.construct (&m_data[m_length], cr::forward <U> (object));
m_length++;
return true;
}
template <typename ...Args> bool emplace (Args &&...args) {
if (!reserve (1)) {
return false;
}
alloc.construct (&m_data[m_length], cr::forward <Args> (args)...);
m_length++;
return true;
}
T pop () {
auto object = cr::move (m_data[m_length - 1]);
discard ();
return object;
}
void discard () {
erase (m_length - 1, 1);
}
size_t index (const T &object) const {
return &object - &m_data[0];
}
void shuffle () {
for (size_t i = m_length; i >= 1; --i) {
cr::swap (m_data[i - 1], m_data[rg.int_ (i, m_length - 2)]);
}
}
void reverse () {
for (size_t i = 0; i < m_length / 2; ++i) {
cr::swap (m_data[i], m_data[m_length - 1 - i]);
}
}
template <typename U> bool extend (U &&rhs) {
if (m_length == 0) {
*this = cr::move (rhs);
}
else {
for (size_t i = 0; i < rhs.length (); ++i) {
if (!push (cr::move (rhs[i]))) {
return false;
}
}
}
return true;
}
template <typename U> bool assign (U &&rhs) {
clear ();
return extend (cr::move (rhs));
}
void clear () {
destructElements ();
m_length = 0;
}
bool empty () const {
return m_length == 0;
}
bool shrink () {
if (m_length == m_capacity || !m_length) {
return false;
}
auto data = alloc.allocate <T> (m_length);
transferElements (data, m_data, m_length);
alloc.deallocate (m_data);
m_data = data;
m_capacity = m_length;
return true;
}
const T &at (size_t index) const {
return m_data[index];
}
T &at (size_t index) {
return m_data[index];
}
const T &first () const {
return m_data[0];
}
T &first () {
return m_data[0];
}
T &last () {
return m_data[m_length - 1];
}
const T &last () const {
return m_data[m_length - 1];
}
const T &random () const {
return m_data[rg.int_ <size_t> (0, m_length - 1)];
}
T &random () {
return m_data[rg.int_ <size_t> (0u, m_length - 1u)];
}
T *data () {
return m_data;
}
T *data () const {
return m_data;
}
public:
Array &operator = (Array &&rhs) noexcept {
if (this != &rhs) {
destroy ();
m_data = rhs.m_data;
m_length = rhs.m_length;
m_capacity = rhs.m_capacity;
rhs.reset ();
}
return *this;
}
public:
const T &operator [] (size_t index) const {
return at (index);
}
T &operator [] (size_t index) {
return at (index);
}
// for range-based loops
public:
T *begin () {
return m_data;
}
T *begin () const {
return m_data;
}
T *end () {
return m_data + m_length;
}
T *end () const {
return m_data + m_length;
}
};
CR_NAMESPACE_END

128
include/crlib/cr-basic.h Normal file
View file

@ -0,0 +1,128 @@
//
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team.
//
// This software is licensed under the BSD-style license.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://yapb.ru/license
//
#pragma once
// our global namaespace
#define CR_NAMESPACE_BEGIN namespace cr {
#define CR_NAMESPACE_END }
// undef base max
#if defined (max)
# undef max
#endif
// undef base min
#if defined (min)
# undef min
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <limits.h>
CR_NAMESPACE_BEGIN
//
// some useful type definitions
//
namespace types {
using int8 = signed char;
using int16 = signed short;
using int32 = signed int;
using uint8 = unsigned char;
using uint16 = unsigned short;
using uint32 = unsigned int;
using uint64 = unsigned long long;
};
// make types available for our own use
using namespace cr::types;
//
// global helper stuff
//
template <typename T, size_t N> constexpr size_t bufsize (const T (&)[N]) {
return N - 1;
}
template <typename T> constexpr T abs (const T &a) {
return a > 0 ? a : -a;
}
template <typename T> constexpr T bit (const T &a) {
return static_cast <T> (1ULL << a);
}
template <typename T> constexpr T min (const T &a, const T &b) {
return a < b ? a : b;
}
template <typename T> constexpr T max (const T &a, const T &b) {
return a > b ? a : b;
}
template <typename T> constexpr T square (const T &value) {
return value * value;
}
template <typename T> constexpr T clamp (const T &x, const T &a, const T &b) {
return min (max (x, a), b);
}
template <typename T> const T &fix (const T &type) {
return type;
}
//
// base classes
//
// simple non-copying base class
class DenyCopying {
protected:
explicit DenyCopying () = default;
~DenyCopying () = default;
public:
DenyCopying (const DenyCopying &) = delete;
DenyCopying &operator = (const DenyCopying &) = delete;
};
// singleton for objects
template <typename T> struct Singleton : private DenyCopying {
public:
static inline T &get () {
static T ref_;
return ref_;
};
};
// simple scoped enum, instaed of enum class
#define CR_DECLARE_SCOPED_ENUM_TYPE(enumName, enumType, ...) \
CR_NAMESPACE_BEGIN \
namespace enums { \
struct _##enumName : protected DenyCopying { \
enum Type : enumType { \
__VA_ARGS__ \
}; \
}; \
}; \
CR_NAMESPACE_END \
using enumName = ::cr::enums::_##enumName::Type; \
// same as above, but with int32 type
#define CR_DECLARE_SCOPED_ENUM(enumName, ...) \
CR_DECLARE_SCOPED_ENUM_TYPE(enumName, int32, __VA_ARGS__); \
CR_NAMESPACE_END
// platform-dependant-stuff
#include <crlib/cr-platform.h>

141
include/crlib/cr-binheap.h Normal file
View file

@ -0,0 +1,141 @@
//
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team.
//
// This software is licensed under the BSD-style license.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://yapb.ru/license
//
#pragma once
#include <crlib/cr-array.h>
CR_NAMESPACE_BEGIN
// simple priority queue
template <typename T> class BinaryHeap final : public DenyCopying {
private:
Array <T> m_data;
public:
explicit BinaryHeap () = default;
BinaryHeap (BinaryHeap &&rhs) noexcept : m_data (cr::move (rhs.m_data))
{ }
~BinaryHeap () = default;
public:
template <typename U> bool push (U &&item) {
if (!m_data.push (cr::move (item))) {
return false;
}
size_t length = m_data.length ();
if (length > 1) {
percolateUp (length - 1);
}
return true;
}
template <typename ...Args> bool emplace (Args &&...args) {
if (!m_data.emplace (cr::forward <Args> (args)...)) {
return false;
}
size_t length = m_data.length ();
if (length > 1) {
percolateUp (length - 1);
}
return true;
}
const T &top () const {
return m_data[0];
}
T pop () {
if (m_data.length () == 1) {
return m_data.pop ();
}
auto key (cr::move (m_data[0]));
m_data[0] = cr::move (m_data.last ());
m_data.discard ();
percolateDown (0);
return key;
}
public:
size_t length () const {
return m_data.length ();
}
bool empty () const {
return !m_data.length ();
}
void clear () {
m_data.clear ();
}
private:
void percolateUp (size_t index) {
while (index != 0) {
size_t parent = this->parent (index);
if (m_data[parent] <= m_data[index]) {
break;
}
cr::swap (m_data[index], m_data[parent]);
index = parent;
}
}
void percolateDown (size_t index) {
while (this->left (index) < m_data.length ()) {
size_t best = this->left (index);
if (this->right (index) < m_data.length ()) {
size_t right_index = this->right (index);
if (m_data[right_index] < m_data[best]) {
best = right_index;
}
}
if (m_data[index] <= m_data[best]) {
break;
}
cr::swap (m_data[index], m_data[best]);
index = best;
best = this->left (index);
}
}
private:
BinaryHeap &operator = (BinaryHeap &&rhs) noexcept {
if (this != &rhs) {
m_data = cr::move (rhs.m_data);
}
return *this;
}
private:
static constexpr size_t left (size_t index) {
return index << 1 | 1;
}
static constexpr size_t right (size_t index) {
return ++index << 1;
}
static constexpr size_t parent (size_t index) {
return --index >> 1;
}
};
CR_NAMESPACE_END

41
include/crlib/cr-color.h Normal file
View file

@ -0,0 +1,41 @@
//
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team.
//
// This software is licensed under the BSD-style license.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://yapb.ru/license
//
#pragma once
#include <crlib/cr-basic.h>
CR_NAMESPACE_BEGIN
// simple color holder
class Color final {
public:
int32 red = 0, green = 0, blue = 0;
public:
explicit Color (int32 r, int32 g, int32 b) : red (r), green (g), blue (b) { }
Color () = default;
~Color () = default;
public:
void reset () {
red = green = blue = 0;
}
int32 avg () const {
return sum () / (sizeof (Color) / sizeof (int32));
}
int32 sum () const {
return red + green + blue;
}
};
CR_NAMESPACE_END

View file

@ -0,0 +1,40 @@
//
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team.
//
// This software is licensed under the BSD-style license.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://yapb.ru/license
//
#pragma once
#include <stdio.h>
#include <crlib/cr-platform.h>
#include <crlib/cr-basic.h>
#include <crlib/cr-alloc.h>
#include <crlib/cr-array.h>
#include <crlib/cr-binheap.h>
#include <crlib/cr-files.h>
#include <crlib/cr-lambda.h>
#include <crlib/cr-http.h>
#include <crlib/cr-library.h>
#include <crlib/cr-dict.h>
#include <crlib/cr-logger.h>
#include <crlib/cr-math.h>
#include <crlib/cr-vector.h>
#include <crlib/cr-random.h>
#include <crlib/cr-ulz.h>
#include <crlib/cr-color.h>
CR_NAMESPACE_BEGIN
namespace types {
using StringArray = Array <String>;
using IntArray = Array <int>;
};
using namespace cr::types;
CR_NAMESPACE_END

237
include/crlib/cr-dict.h Normal file
View file

@ -0,0 +1,237 @@
//
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team.
//
// This software is licensed under the BSD-style license.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://yapb.ru/license
//
#pragma once
#include <crlib/cr-basic.h>
#include <crlib/cr-array.h>
#include <crlib/cr-string.h>
#include <crlib/cr-twin.h>
CR_NAMESPACE_BEGIN
// template for hashing our string
template <typename K> struct StringHash {
uint32 operator () (const K &key) const {
char *str = const_cast <char *> (key.chars ());
uint32 hash = 0;
while (*str++) {
hash = ((hash << 5) + hash) + *str;
}
return hash;
}
};
// template for hashing integers
template <typename K> struct IntHash {
uint32 operator () (K key) const {
key = ((key >> 16) ^ key) * 0x119de1f3;
key = ((key >> 16) ^ key) * 0x119de1f3;
key = (key >> 16) ^ key;
return key;
}
};
namespace detail {
struct DictionaryList {
uint32 index;
DictionaryList *next;
};
template <typename K, typename V> struct DictionaryBucket {
uint32 hash = static_cast <size_t> (-1);
K key = K ();
V value = V ();
public:
DictionaryBucket () = default;
~DictionaryBucket () = default;
};
}
// basic dictionary
template <class K, class V, class H = StringHash <K>, size_t HashSize = 36> class Dictionary final : public DenyCopying {
public:
static constexpr size_t kInvalidIndex = static_cast <size_t> (-1);
private:
Array <detail::DictionaryList *> m_table;
Array <detail::DictionaryBucket <K, V>> m_buckets;
H m_hasher;
private:
uint32 hash (const K &key) const {
return m_hasher (key);
}
size_t findIndexOrAllocate (const K &key, bool allocate) {
auto hashed = hash (key);
auto pos = hashed % m_table.length ();
for (auto bucket = m_table[pos]; bucket != nullptr; bucket = bucket->next) {
if (m_buckets[bucket->index].hash == hashed) {
return bucket->index;
}
}
if (allocate) {
size_t created = m_buckets.length ();
m_buckets.resize (created + 1);
auto allocated = alloc.allocate <detail::DictionaryList> ();
allocated->index = created;
allocated->next = m_table[pos];
m_table[pos] = allocated;
m_buckets[created].key = key;
m_buckets[created].hash = hashed;
return created;
}
return kInvalidIndex;
}
size_t findIndex (const K &key) const {
return const_cast <Dictionary *> (this)->findIndexOrAllocate (key, false);
}
public:
explicit Dictionary () {
reset ();
}
Dictionary (Dictionary &&rhs) noexcept : m_table (cr::move (rhs.m_table)), m_buckets (cr::move (rhs.m_buckets)), m_hasher (cr::move (rhs.m_hasher))
{ }
~Dictionary () {
clear ();
}
public:
bool exists (const K &key) const {
return findIndex (key) != kInvalidIndex;
}
bool empty () const {
return m_buckets.empty ();
}
size_t length () const {
return m_buckets.length ();
}
bool find (const K &key, V &value) const {
size_t index = findIndex (key);
if (index == kInvalidIndex) {
return false;
}
value = m_buckets[index].value;
return true;
}
template <typename U> bool push (const K &key, U &&value) {
operator [] (key) = cr::forward <U> (value);
return true;
}
bool remove (const K &key) {
auto hashed = hash (key);
auto pos = hashed % m_table.length ();
auto *bucket = m_table[pos];
detail::DictionaryList *next = nullptr;
while (bucket != nullptr) {
if (m_buckets[bucket->index].hash == hashed) {
if (!next) {
m_table[pos] = bucket->next;
}
else {
next->next = bucket->next;
}
m_buckets.erase (bucket->index, 1);
alloc.deallocate (bucket);
bucket = nullptr;
return true;
}
next = bucket;
bucket = bucket->next;
}
return false;
}
void clear () {
for (auto object : m_table) {
while (object != nullptr) {
auto next = object->next;
alloc.deallocate (object);
object = next;
}
}
m_table.clear ();
m_buckets.clear ();
reset ();
}
void reset () {
m_table.resize (HashSize);
for (size_t i = 0; i < HashSize; ++i) {
m_table[i] = nullptr;
}
}
public:
V &operator [] (const K &key) {
return m_buckets[findIndexOrAllocate (key, true)].value;
}
const V &operator [] (const K &key) const {
return m_buckets[findIndex (key)].value;
}
Dictionary &operator = (Dictionary &&rhs) noexcept {
if (this != &rhs) {
m_table = cr::move (rhs.m_table);
m_buckets = cr::move (rhs.m_buckets);
m_hasher = cr::move (rhs.m_hasher);
}
return *this;
}
// for range-based loops
public:
detail::DictionaryBucket <K, V> *begin () {
return m_buckets.begin ();
}
detail::DictionaryBucket <K, V> *begin () const {
return m_buckets.begin ();
}
detail::DictionaryBucket <K, V> *end () {
return m_buckets.end ();
}
detail::DictionaryBucket <K, V> *end () const {
return m_buckets.end ();
}
};
CR_NAMESPACE_END

334
include/crlib/cr-files.h Normal file
View file

@ -0,0 +1,334 @@
//
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team.
//
// This software is licensed under the BSD-style license.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://yapb.ru/license
//
#pragma once
#include <stdio.h>
#include <crlib/cr-string.h>
#include <crlib/cr-lambda.h>
CR_NAMESPACE_BEGIN
// simple stdio file wrapper
class File final : private DenyCopying {
private:
FILE *m_handle = nullptr;
size_t m_length = 0;
public:
explicit File () = default;
File (const String &file, const String &mode = "rt") {
open (file, mode);
}
~File () {
close ();
}
public:
bool open (const String &file, const String &mode) {
if ((m_handle = fopen (file.chars (), mode.chars ())) == nullptr) {
return false;
}
fseek (m_handle, 0L, SEEK_END);
m_length = ftell (m_handle);
fseek (m_handle, 0L, SEEK_SET);
return true;
}
void close () {
if (m_handle != nullptr) {
fclose (m_handle);
m_handle = nullptr;
}
m_length = 0;
}
bool eof () const {
return !!feof (m_handle);
}
bool flush () const {
return !!fflush (m_handle);
}
int getChar () const {
return fgetc (m_handle);
}
char *getString (char *buffer, int count) const {
return fgets (buffer, count, m_handle);
}
bool getLine (String &line) {
line.clear ();
int ch;
while ((ch = getChar ()) != EOF && !eof ()) {
line += static_cast <char> (ch);
if (ch == '\n') {
break;
}
}
return !eof ();
}
template <typename ...Args> size_t puts (const char *fmt, Args ...args) {
if (!*this) {
return false;
}
return fprintf (m_handle, fmt, cr::forward <Args> (args)...);
}
bool puts (const String &buffer) {
if (!*this) {
return false;
}
if (fputs (buffer.chars (), m_handle) < 0) {
return false;
}
return true;
}
int putChar (int ch) {
return fputc (ch, m_handle);
}
size_t read (void *buffer, size_t size, size_t count = 1) {
return fread (buffer, size, count, m_handle);
}
size_t write (void *buffer, size_t size, size_t count = 1) {
return fwrite (buffer, size, count, m_handle);
}
bool seek (long offset, int origin) {
if (fseek (m_handle, offset, origin) != 0) {
return false;
}
return true;
}
void rewind () {
::rewind (m_handle);
}
size_t length () const {
return m_length;
}
public:
explicit operator bool () const {
return m_handle != nullptr;
}
public:
static inline bool exists (const String &file) {
File fp;
if (fp.open (file, "rb")) {
fp.close ();
return true;
}
return false;
}
static inline void createPath (const char *path) {
for (auto str = const_cast <char *> (path) + 1; *str; ++str) {
if (*str == '/') {
*str = 0;
plat.createDirectory (path);
*str = '/';
}
}
plat.createDirectory (path);
}
};
// wrapper for memory file for loading data into the memory
class MemFileStorage : public Singleton <MemFileStorage> {
private:
using LoadFunction = Lambda <uint8 * (const char *, int *)>;
using FreeFunction = Lambda <void (void *)>;
private:
LoadFunction m_load = nullptr;
FreeFunction m_free = nullptr;
public:
inline MemFileStorage () = default;
inline ~MemFileStorage () = default;
public:
void initizalize (LoadFunction loader, FreeFunction unloader) {
m_load = cr::move (loader);
m_free = cr::move (unloader);
}
public:
uint8 *load (const String &file, int *size) {
if (m_load) {
return m_load (file.chars (), size);
}
return nullptr;
}
void unload (void *buffer) {
if (m_free) {
m_free (buffer);
}
}
};
class MemFile final : public DenyCopying {
private:
static constexpr char kEOF = static_cast <char> (-1);
private:
uint8 *m_data = nullptr;
size_t m_length = 0, m_pos = 0;
public:
explicit MemFile () = default;
MemFile (const String &file) {
open (file);
}
~MemFile () {
close ();
}
public:
bool open (const String &file) {
m_length = 0;
m_pos = 0;
m_data = MemFileStorage::get ().load (file.chars (), reinterpret_cast <int *> (&m_length));
if (!m_data) {
return false;
}
return true;
}
void close () {
MemFileStorage::get ().unload (m_data);
m_length = 0;
m_pos = 0;
m_data = nullptr;
}
char getChar () {
if (!m_data || m_pos >= m_length) {
return kEOF;
}
auto ch = m_data[m_pos];
m_pos++;
return static_cast <char> (ch);
}
char *getString (char *buffer, size_t count) {
if (!m_data || m_pos >= m_length) {
return nullptr;
}
size_t index = 0;
buffer[0] = 0;
for (; index < count - 1;) {
if (m_pos < m_length) {
buffer[index] = m_data[m_pos++];
if (buffer[index++] == '\n') {
break;
}
}
else {
break;
}
}
buffer[index] = 0;
return index ? buffer : nullptr;
}
bool getLine (String &line) {
line.clear ();
char ch;
while ((ch = getChar ()) != kEOF) {
line += ch;
if (ch == '\n') {
break;
}
}
return !eof ();
}
size_t read (void *buffer, size_t size, size_t count = 1) {
if (!m_data || m_length <= m_pos || !buffer || !size || !count) {
return 0;
}
size_t blocks_read = size * count <= m_length - m_pos ? size * count : m_length - m_pos;
memcpy (buffer, &m_data[m_pos], blocks_read);
m_pos += blocks_read;
return blocks_read / size;
}
bool seek (size_t offset, int origin) {
if (!m_data || m_pos >= m_length) {
return false;
}
if (origin == SEEK_SET) {
if (offset >= m_length) {
return false;
}
m_pos = offset;
}
else if (origin == SEEK_END) {
if (offset >= m_length) {
return false;
}
m_pos = m_length - offset;
}
else {
if (m_pos + offset >= m_length) {
return false;
}
m_pos += offset;
}
return true;
}
size_t length () const {
return m_length;
}
bool eof () const {
return m_pos >= m_length;
}
void rewind () {
m_pos = 0;
}
public:
explicit operator bool () const {
return !!m_data && m_length > 0;
}
};
CR_NAMESPACE_END

425
include/crlib/cr-http.h Normal file
View file

@ -0,0 +1,425 @@
//
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team.
//
// This software is licensed under the BSD-style license.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://yapb.ru/license
//
#pragma once
#include <stdio.h>
#include <crlib/cr-string.h>
#include <crlib/cr-files.h>
#include <crlib/cr-logger.h>
#include <crlib/cr-platform.h>
#if defined (CR_LINUX) || defined (CR_OSX)
# include <netinet/in.h>
# include <sys/socket.h>
# include <sys/types.h>
# include <arpa/inet.h>
# include <unistd.h>
# include <errno.h>
# include <netdb.h>
# include <fcntl.h>
#elif defined (CR_WINDOWS)
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# include <winsock2.h>
#endif
// status codes for http client
CR_DECLARE_SCOPED_ENUM (HttpClientResult,
OK = 0,
NotFound,
Forbidden,
SocketError,
ConnectError,
HttpOnly,
Undefined,
NoLocalFile = -1,
LocalFileExists = -2
);
CR_NAMESPACE_BEGIN
class Socket final : public DenyCopying {
private:
int32 m_socket;
uint32 m_timeout;
public:
Socket () : m_socket (-1), m_timeout (2) {
#if defined(CR_WINDOWS)
WSADATA wsa;
if (WSAStartup (MAKEWORD (1, 1), &wsa) != 0) {
logger.error ("Unable to inialize sockets.");
}
#endif
}
~Socket () {
disconnect ();
#if defined (CR_WINDOWS)
WSACleanup ();
#endif
}
public:
bool connect (const String &hostname) {
auto host = gethostbyname (hostname.chars ());
if (!host) {
return false;
}
m_socket = static_cast <int> (socket (AF_INET, SOCK_STREAM, 0));
if (m_socket < 0) {
return false;
}
auto getTimeouts = [&] () -> Twin <char *, size_t> {
#if defined (CR_WINDOWS)
DWORD tv = m_timeout * 1000;
#else
timeval tv { static_cast <time_t> (m_timeout), 0 };
#endif
return { reinterpret_cast <char *> (&tv), sizeof (tv) };
};
auto timeouts = getTimeouts ();
setsockopt (m_socket, SOL_SOCKET, SO_RCVTIMEO, timeouts.first, timeouts.second);
setsockopt (m_socket, SOL_SOCKET, SO_SNDTIMEO, timeouts.first, timeouts.second);
sockaddr_in dest;
memset (&dest, 0, sizeof (dest));
dest.sin_family = AF_INET;
dest.sin_port = htons (80);
dest.sin_addr.s_addr = inet_addr (inet_ntoa (*(reinterpret_cast <in_addr *> (host->h_addr))));
if (::connect (m_socket, reinterpret_cast <sockaddr *> (&dest), static_cast <int> (sizeof (dest))) == -1) {
disconnect ();
return false;
}
return true;
}
void setTimeout (uint32 timeout) {
m_timeout = timeout;
}
void disconnect () {
#if defined(CR_WINDOWS)
if (m_socket != -1) {
closesocket (m_socket);
}
#else
if (m_socket != -1)
close (m_socket);
#endif
}
public:
template <typename U> int32 send (const U *buffer, int32 length) const {
return ::send (m_socket, reinterpret_cast <const char *> (buffer), length, 0);
}
template <typename U> int32 recv (U *buffer, int32 length) {
return ::recv (m_socket, reinterpret_cast <char *> (buffer), length, 0);
}
};
namespace detail {
// simple http uri omitting query-string and port
struct HttpUri {
String path, protocol, host;
public:
static HttpUri parse (const String &uri) {
HttpUri result;
if (uri.empty ()) {
return result;
}
size_t protocol = uri.find ("://");
if (protocol != String::kInvalidIndex) {
result.protocol = uri.substr (0, protocol);
size_t host = uri.find ("/", protocol + 3);
if (host != String::kInvalidIndex) {
result.path = uri.substr (host + 1);
result.host = uri.substr (protocol + 3, host - protocol - 3);
return result;
}
}
return result;
}
};
};
// simple http client for downloading/uploading files only
class HttpClient final : public Singleton <HttpClient> {
private:
static constexpr int32 kMaxRecvErrors = 12;
private:
Socket m_socket;
String m_userAgent = "crlib";
HttpClientResult m_code = HttpClientResult::Undefined;
int32 m_chunkSize = 4096;
public:
HttpClient () = default;
~HttpClient () = default;
private:
HttpClientResult parseResponseHeader (uint8 *buffer) {
bool isFinished = false;
int32 pos = 0, symbols = 0, errors = 0;
// prase response header
while (!isFinished && pos < m_chunkSize) {
if (m_socket.recv (&buffer[pos], 1) < 1) {
if (++errors > kMaxRecvErrors) {
isFinished = true;
}
else {
continue;
}
}
switch (buffer[pos]) {
case '\r':
break;
case '\n':
isFinished = (symbols == 0);
symbols = 0;
break;
default:
symbols++;
break;
}
pos++;
}
String response (reinterpret_cast <const char *> (buffer));
size_t responseCodeStart = response.find ("HTTP/1.1");
if (responseCodeStart != String::kInvalidIndex) {
String respCode = response.substr (responseCodeStart + 9, 3).trim ();
if (respCode == "200") {
return HttpClientResult::OK;
}
else if (respCode == "403") {
return HttpClientResult::Forbidden;
}
else if (respCode == "404") {
return HttpClientResult::NotFound;
}
}
return HttpClientResult::NotFound;
}
public:
// simple blocked download
bool downloadFile (const String &url, const String &localPath) {
if (File::exists (localPath.chars ())) {
m_code = HttpClientResult::LocalFileExists;
return false;
}
auto uri = detail::HttpUri::parse (url);
// no https...
if (uri.protocol == "https") {
m_code = HttpClientResult::HttpOnly;
return false;
}
// unable to connect...
if (!m_socket.connect (uri.host)) {
m_code = HttpClientResult::ConnectError;
m_socket.disconnect ();
return false;
}
String request;
request.appendf ("GET /%s HTTP/1.1\r\n", uri.path.chars ());
request.append ("Accept: */*\r\n");
request.append ("Connection: close\r\n");
request.append ("Keep-Alive: 115\r\n");
request.appendf ("User-Agent: %s\r\n", m_userAgent.chars ());
request.appendf ("Host: %s\r\n\r\n", uri.host.chars ());
if (m_socket.send (request.chars (), static_cast <int32> (request.length ())) < 1) {
m_code = HttpClientResult::SocketError;
m_socket.disconnect ();
return false;
}
Array <uint8, ReservePolicy::PlusOne> buffer (m_chunkSize);
m_code = parseResponseHeader (buffer.data ());
if (m_code != HttpClientResult::OK) {
m_socket.disconnect ();
return false;
}
// receive the file
File file (localPath, "wb");
if (!file) {
m_code = HttpClientResult::Undefined;
m_socket.disconnect ();
return false;
}
int32 length = 0;
int32 errors = 0;
for (;;) {
length = m_socket.recv (buffer.data (), m_chunkSize);
if (length > 0) {
file.write (buffer.data (), length);
}
else if (++errors > 12) {
break;
}
}
file.close ();
m_socket.disconnect ();
m_code = HttpClientResult::OK;
return true;
}
bool uploadFile (const String &url, const String &localPath) {
if (!File::exists (localPath.chars ())) {
m_code = HttpClientResult::NoLocalFile;
return false;
}
auto uri = detail::HttpUri::parse (url);
// no https...
if (uri.protocol == "https") {
m_code = HttpClientResult::HttpOnly;
return false;
}
// unable to connect...
if (!m_socket.connect (uri.host)) {
m_code = HttpClientResult::ConnectError;
m_socket.disconnect ();
return false;
}
// receive the file
File file (localPath, "rb");
if (!file) {
m_code = HttpClientResult::Undefined;
m_socket.disconnect ();
return false;
}
String boundaryName = localPath;
size_t boundarySlash = localPath.findLastOf ("\\/");
if (boundarySlash != String::kInvalidIndex) {
boundaryName = localPath.substr (boundarySlash + 1);
}
const String &kBoundary = "---crlib_upload_boundary_1337";
String request, start, end;
start.appendf ("--%s\r\n", kBoundary.chars ());
start.appendf ("Content-Disposition: form-data; name='file'; filename='%s'\r\n", boundaryName.chars ());
start.append ("Content-Type: application/octet-stream\r\n\r\n");
end.appendf ("\r\n--%s--\r\n\r\n", kBoundary.chars ());
request.appendf ("POST /%s HTTP/1.1\r\n", uri.path.chars ());
request.appendf ("Host: %s\r\n", uri.host.chars ());
request.appendf ("User-Agent: %s\r\n", m_userAgent.chars ());
request.appendf ("Content-Type: multipart/form-data; boundary=%s\r\n", kBoundary.chars ());
request.appendf ("Content-Length: %d\r\n\r\n", file.length () + start.length () + end.length ());
// send the main request
if (m_socket.send (request.chars (), static_cast <int32> (request.length ())) < 1) {
m_code = HttpClientResult::SocketError;
m_socket.disconnect ();
return false;
}
// send boundary start
if (m_socket.send (start.chars (), static_cast <int32> (start.length ())) < 1) {
m_code = HttpClientResult::SocketError;
m_socket.disconnect ();
return false;
}
Array <uint8, ReservePolicy::PlusOne> buffer (m_chunkSize);
int32 length = 0;
for (;;) {
length = static_cast <int32> (file.read (buffer.data (), 1, m_chunkSize));
if (length > 0) {
m_socket.send (buffer.data (), length);
}
else {
break;
}
}
// send boundary end
if (m_socket.send (end.chars (), static_cast <int32> (end.length ())) < 1) {
m_code = HttpClientResult::SocketError;
m_socket.disconnect ();
return false;
}
m_code = parseResponseHeader (buffer.data ());
m_socket.disconnect ();
return m_code == HttpClientResult::OK;
}
public:
void setUserAgent (const String &ua) {
m_userAgent = ua;
}
HttpClientResult getLastStatusCode () {
return m_code;
}
void setChunkSize (int32 chunkSize) {
m_chunkSize = chunkSize;
}
void setTimeout (uint32 timeout) {
m_socket.setTimeout (timeout);
}
};
// expose global http client
static auto &http = HttpClient::get ();
CR_NAMESPACE_END

173
include/crlib/cr-lambda.h Normal file
View file

@ -0,0 +1,173 @@
//
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team.
//
// This software is licensed under the BSD-style license.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://yapb.ru/license
//
#pragma once
#include <crlib/cr-alloc.h>
#include <crlib/cr-uniqueptr.h>
CR_NAMESPACE_BEGIN
static constexpr uint32 kLambdaSmallBufferSize = sizeof (void *) * 16;
template <typename> class Lambda;
template <typename R, typename ...Args> class Lambda <R (Args...)> {
class LambdaFunctorWrapper {
public:
LambdaFunctorWrapper () = default;
virtual ~LambdaFunctorWrapper () = default;
public:
virtual void move (uint8 *to) = 0;
virtual void small (uint8 *to) const = 0;
virtual R invoke (Args &&...) = 0;
virtual UniquePtr <LambdaFunctorWrapper> clone () const = 0;
public:
void operator delete (void *ptr) {
alloc.deallocate (ptr);
}
};
template <typename T> class LambdaFunctor : public LambdaFunctorWrapper {
private:
T m_callable;
public:
LambdaFunctor (const T &callable) : LambdaFunctorWrapper (), m_callable (callable)
{ }
LambdaFunctor (T &&callable) : LambdaFunctorWrapper (), m_callable (cr::move (callable))
{ }
~LambdaFunctor () override = default;
public:
void move (uint8 *to) override {
new (to) LambdaFunctor<T> (cr::move (m_callable));
}
void small (uint8 *to) const override {
new (to) LambdaFunctor<T> (m_callable);
}
R invoke (Args &&... args) override {
return m_callable (cr::forward <Args> (args)...);
}
UniquePtr <LambdaFunctorWrapper> clone () const override {
return createUniqueBase <LambdaFunctor <T>, LambdaFunctorWrapper> (m_callable);
}
};
union {
UniquePtr <LambdaFunctorWrapper> m_functor;
uint8 m_small[kLambdaSmallBufferSize];
};
bool m_smallObject;
private:
void destroy () {
if (m_smallObject) {
reinterpret_cast <LambdaFunctorWrapper *> (m_small)->~LambdaFunctorWrapper ();
}
else {
m_functor.reset ();
}
}
void swap (Lambda &rhs) noexcept {
cr::swap (rhs, *this);
}
public:
Lambda () noexcept : Lambda (nullptr)
{ }
Lambda (decltype (nullptr)) noexcept : m_functor (nullptr), m_smallObject (false)
{ }
Lambda (const Lambda &rhs) {
if (rhs.m_smallObject) {
reinterpret_cast <const LambdaFunctorWrapper *> (rhs.m_small)->small (m_small);
}
else {
new (m_small) UniquePtr <LambdaFunctorWrapper> (rhs.m_functor->clone ());
}
m_smallObject = rhs.m_smallObject;
}
Lambda (Lambda &&rhs) noexcept {
if (rhs.m_smallObject) {
reinterpret_cast <LambdaFunctorWrapper *> (rhs.m_small)->move (m_small);
new (rhs.m_small) UniquePtr <LambdaFunctorWrapper> (nullptr);
}
else {
new (m_small) UniquePtr <LambdaFunctorWrapper> (cr::move (rhs.m_functor));
}
m_smallObject = rhs.m_smallObject;
rhs.m_smallObject = false;
}
template <typename F> Lambda (F function) {
if (cr::fix (sizeof (function) > kLambdaSmallBufferSize)) {
m_smallObject = false;
new (m_small) UniquePtr <LambdaFunctorWrapper> (createUniqueBase <LambdaFunctor <F>, LambdaFunctorWrapper> (cr::move (function)));
}
else {
m_smallObject = true;
new (m_small) LambdaFunctor<F> (cr::move (function));
}
}
~Lambda () {
destroy ();
}
public:
Lambda &operator = (const Lambda &rhs) {
destroy ();
Lambda tmp (rhs);
swap (tmp);
return *this;
}
Lambda &operator = (Lambda &&rhs) noexcept {
destroy ();
if (rhs.m_smallObject) {
reinterpret_cast <LambdaFunctorWrapper *> (rhs.m_small)->move (m_small);
new (rhs.m_small) UniquePtr <LambdaFunctorWrapper> (nullptr);
}
else {
new (m_small) UniquePtr <LambdaFunctorWrapper> (cr::move (rhs.m_functor));
}
m_smallObject = rhs.m_smallObject;
rhs.m_smallObject = false;
return *this;
}
explicit operator bool () const noexcept {
return m_smallObject || !!m_functor;
}
public:
R operator () (Args ...args) {
return m_smallObject ? reinterpret_cast <LambdaFunctorWrapper *> (m_small)->invoke (cr::forward <Args> (args)...) : m_functor->invoke (cr::forward <Args> (args)...);
}
};
CR_NAMESPACE_END

View file

@ -0,0 +1,89 @@
//
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team.
//
// This software is licensed under the BSD-style license.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://yapb.ru/license
//
#pragma once
#include <crlib/cr-basic.h>
#include <crlib/cr-string.h>
#if defined (CR_LINUX) || defined (CR_OSX)
# include <dlfcn.h>
# include <errno.h>
# include <fcntl.h>
# include <sys/stat.h>
# include <unistd.h>
#elif defined (CR_WINDOWS)
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif
CR_NAMESPACE_BEGIN
// handling dynamic library loading
class SharedLibrary final : public DenyCopying {
private:
void *m_handle = nullptr;
public:
explicit SharedLibrary () = default;
SharedLibrary (const String &file) {
if (file.empty ()) {
return;
}
load (file);
}
~SharedLibrary () {
unload ();
}
public:
inline bool load (const String &file) noexcept {
#ifdef CR_WINDOWS
m_handle = LoadLibraryA (file.chars ());
#else
m_handle = dlopen (file.chars (), RTLD_NOW);
#endif
return m_handle != nullptr;
}
void unload () noexcept {
if (!*this) {
return;
}
#ifdef CR_WINDOWS
FreeLibrary (static_cast <HMODULE> (m_handle));
#else
dlclose (m_handle);
#endif
}
template <typename R> R resolve (const char *function) const {
if (!*this) {
return nullptr;
}
return reinterpret_cast <R> (
#ifdef CR_WINDOWS
GetProcAddress (static_cast <HMODULE> (m_handle), function)
#else
dlsym (m_handle, function)
#endif
);
}
public:
explicit operator bool () const {
return m_handle != nullptr;
}
};
CR_NAMESPACE_END

98
include/crlib/cr-logger.h Normal file
View file

@ -0,0 +1,98 @@
//
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team.
//
// This software is licensed under the BSD-style license.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://yapb.ru/license
//
#pragma once
#include <time.h>
#include <crlib/cr-files.h>
#include <crlib/cr-lambda.h>
#if defined (CR_WINDOWS)
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif
CR_NAMESPACE_BEGIN
class SimpleLogger final : public Singleton <SimpleLogger> {
public:
using PrintFunction = Lambda <void (const char *)>;
private:
File m_handle;
PrintFunction m_printer;
public:
SimpleLogger () = default;
~SimpleLogger () {
m_handle.close ();
}
private:
void logToFile (const char *level, const char *msg) {
if (!m_handle) {
return;
}
time_t ticks = time (&ticks);
auto tm = localtime (&ticks);
auto timebuf = strings.chars ();
strftime (timebuf, StringBuffer::StaticBufferSize, "%Y-%m-%d %H:%M:%S", tm);
m_handle.puts ("%s (%s): %s\n", timebuf, level, msg);
}
public:
template <typename ...Args> void fatal (const char *fmt, Args ...args) {
auto msg = strings.format (fmt, cr::forward <Args> (args)...);
logToFile ("FATAL", msg);
if (m_printer) {
m_printer (msg);
}
plat.abort (msg);
}
template <typename ...Args> void error (const char *fmt, Args ...args) {
auto msg = strings.format (fmt, cr::forward <Args> (args)...);
logToFile ("ERROR", msg);
if (m_printer) {
m_printer (msg);
}
}
template <typename ...Args> void message (const char *fmt, Args ...args) {
auto msg = strings.format (fmt, cr::forward <Args> (args)...);
logToFile ("INFO", msg);
if (m_printer) {
m_printer (msg);
}
}
public:
void initialize (const String &filename, PrintFunction printFunction) {
if (m_handle) {
m_handle.close ();
}
m_printer = cr::move (printFunction);
m_handle.open (filename, "at");
}
};
// expose global instance
static auto &logger = SimpleLogger::get ();
CR_NAMESPACE_END

179
include/crlib/cr-math.h Normal file
View file

@ -0,0 +1,179 @@
//
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team.
//
// This software is licensed under the BSD-style license.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://yapb.ru/license
//
#pragma once
#include <crlib/cr-basic.h>
#if defined (CR_HAS_SSE2)
# include <immintrin.h>
#endif
CR_NAMESPACE_BEGIN
constexpr float kFloatEpsilon = 0.01f;
constexpr float kFloatEqualEpsilon = 0.001f;
constexpr float kFloatCmpEpsilon = 1.192092896e-07f;
constexpr float kMathPi = 3.141592653589793115997963468544185161590576171875f;
constexpr float kMathPiReciprocal = 1.0f / kMathPi;
constexpr float kMathPiHalf = kMathPi / 2;
constexpr float kDegreeToRadians = kMathPi / 180.0f;
constexpr float kRadiansToDegree = 180.0f / kMathPi;
constexpr bool fzero (const float e) {
return cr::abs (e) < kFloatEpsilon;
}
constexpr bool fequal (const float a, const float b) {
return cr:: abs (a - b) < kFloatEqualEpsilon;
}
constexpr float radiansToDegrees (const float r) {
return r * kRadiansToDegree;
}
constexpr float degreesToRadians (const float d) {
return d * kDegreeToRadians;
}
constexpr float modAngles (const float a) {
return 360.0f / 65536.0f * (static_cast <int> (a * (65536.0f / 360.0f)) & 65535);
}
constexpr float normalizeAngles (const float a) {
return 360.0f / 65536.0f * (static_cast <int> ((a + 180.0f) * (65536.0f / 360.0f)) & 65535) - 180.0f;
}
constexpr float anglesDifference (const float a, const float b) {
return normalizeAngles (a - b);
}
inline float sinf (const float value) {
const auto sign = static_cast <int32> (value * kMathPiReciprocal);
const float calc = (value - static_cast <float> (sign) * kMathPi);
const float sqr = cr::square (calc);
const float res = 1.00000000000000000000e+00f + sqr * (-1.66666671633720397949e-01f + sqr * (8.33333376795053482056e-03f + sqr * (-1.98412497411482036114e-04f +
sqr * (2.75565571428160183132e-06f + sqr * (-2.50368472620721149724e-08f + sqr * (1.58849267073435385100e-10f + sqr * -6.58925550841432672300e-13f))))));
return (sign & 1) ? -calc * res : value * res;
}
inline float cosf (const float value) {
const auto sign = static_cast <int32> (value * kMathPiReciprocal);
const float calc = (value - static_cast <float> (sign) * kMathPi);
const float sqr = cr::square (calc);
const float res = sqr * (-5.00000000000000000000e-01f + sqr * (4.16666641831398010254e-02f + sqr * (-1.38888671062886714935e-03f + sqr * (2.48006890615215525031e-05f +
sqr * (-2.75369927749125054106e-07f + sqr * (2.06207229069832465029e-09f + sqr * -9.77507137733812925262e-12f))))));
const float f = -1.00000000000000000000e+00f;
return (sign & 1) ? f - res : -f + res;
}
inline float atanf (const float x) {
const float sqr = cr::square (x);
return x * (48.70107004404898384f + sqr * (49.5326263772254345f + sqr * 9.40604244231624f)) / (48.70107004404996166f + sqr * (65.7663163908956299f + sqr * (21.587934067020262f + sqr)));
};
inline float atan2f (const float y, const float x) {
const float ax = cr::abs (x);
const float ay = cr::abs (y);
if (ax < 1e-7f && ay < 1e-7f) {
return 0.0f;
}
if (ax > ay) {
if (x < 0.0f) {
if (y >= 0.0f) {
return atanf (y / x) + kMathPi;
}
return atanf (y / x) - kMathPi;
}
return atanf (y / x);
}
if (y < 0.0f) {
return atanf (-x / y) - kMathPiHalf;
}
return atanf (-x / y) + kMathPiHalf;
}
inline float powf (const float x, const float y) {
union {
float d;
int x;
} res { x };
res.x = static_cast <int> (y * (res.x - 1064866805) + 1064866805);
return res.d;
}
inline float sqrtf (const float value) {
return powf (value, 0.5f);
}
inline float tanf (const float value) {
return sinf (value) / cosf (value);
}
constexpr float ceilf (const float x) {
return static_cast <float> (65536 - static_cast <int> (65536.0f - x));
}
inline void sincosf (const float x, const float y, const float z, float *sines, float *cosines) {
#if defined (CR_HAS_SSE2)
auto set = _mm_set_ps (x, y, z, 0.0f);
auto _mm_sin = [] (__m128 rad) -> __m128 {
static auto pi2 = _mm_set_ps1 (kMathPi * 2);
static auto rp1 = _mm_set_ps1 (4.0f / kMathPi);
static auto rp2 = _mm_set_ps1 (-4.0f / (kMathPi * kMathPi));
static auto val = _mm_cmpnlt_ps (rad, _mm_set_ps1 (kMathPi));
static auto csi = _mm_castsi128_ps (_mm_set1_epi32 (0x80000000));
val = _mm_and_ps (val, pi2);
rad = _mm_sub_ps (rad, val);
val = _mm_cmpngt_ps (rad, _mm_set_ps1 (-kMathPi));
val = _mm_and_ps (val, pi2);
rad = _mm_add_ps (rad, val);
val = _mm_mul_ps (_mm_andnot_ps (csi, rad), rp2);
val = _mm_add_ps (val, rp1);
auto si = _mm_mul_ps (val, rad);
val = _mm_mul_ps (_mm_andnot_ps (csi, si), si);
val = _mm_sub_ps (val, si);
val = _mm_mul_ps (val, _mm_set_ps1 (0.225f));
return _mm_add_ps (val, si);
};
static auto hpi = _mm_set_ps1 (kMathPiHalf);
auto s = _mm_sin (set);
auto c = _mm_sin (_mm_add_ps (set, hpi));
_mm_store_ps (sines, _mm_shuffle_ps (s, s, _MM_SHUFFLE (0, 1, 2, 3)));
_mm_store_ps (cosines, _mm_shuffle_ps (c, c, _MM_SHUFFLE (0, 1, 2, 3)));
#else
sines[0] = cr::sinf (x);
sines[1] = cr::sinf (y);
sines[2] = cr::sinf (z);
cosines[0] = cr::cosf (x);
cosines[1] = cr::cosf (y);
cosines[2] = cr::cosf (z);
#endif
}
CR_NAMESPACE_END

View file

@ -0,0 +1,58 @@
//
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team.
//
// This software is licensed under the BSD-style license.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://yapb.ru/license
//
#pragma once
#include <crlib/cr-basic.h>
CR_NAMESPACE_BEGIN
namespace detail {
template <typename T> struct ClearRef {
using Type = T;
};
template <typename T> struct ClearRef <T &> {
using Type = T;
};
template <typename T> struct ClearRef <T &&> {
using Type = T;
};
};
template <typename T> typename detail::ClearRef <T>::Type constexpr &&move (T &&type) noexcept {
return static_cast <typename detail::ClearRef <T>::Type &&> (type);
}
template <typename T> constexpr T &&forward (typename detail::ClearRef <T>::Type &type) noexcept {
return static_cast <T &&> (type);
}
template <typename T> constexpr T &&forward (typename detail::ClearRef <T>::Type &&type) noexcept {
return static_cast <T &&> (type);
}
template <typename T> inline void swap (T &left, T &right) noexcept {
auto temp = cr::move (left);
left = cr::move (right);
right = cr::move (temp);
}
template <typename T, size_t S> inline void swap (T (&left)[S], T (&right)[S]) noexcept {
if (&left == &right) {
return;
}
auto begin = left;
auto end = begin + S;
for (auto temp = right; begin != end; ++begin, ++temp) {
cr::swap (*begin, *temp);
}
}
CR_NAMESPACE_END

172
include/crlib/cr-platform.h Normal file
View file

@ -0,0 +1,172 @@
//
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team.
//
// This software is licensed under the BSD-style license.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://yapb.ru/license
//
#pragma once
CR_NAMESPACE_BEGIN
// detects the build platform
#if defined(__linux__)
# define CR_LINUX
#elif defined(__APPLE__)
# define CR_OSX
#elif defined(_WIN32)
# define CR_WINDOWS
#endif
#if defined(__ANDROID__)
# define CR_ANDROID
# if defined(LOAD_HARDFP)
# define CR_ANDROID_HARD_FP
# endif
#endif
// detects the compiler
#if defined(_MSC_VER)
# define CR_CXX_MSVC
#elif defined(__clang__)
# define CR_CXX_CLANG
#endif
// configure export macros
#if defined(CR_WINDOWS)
# define CR_EXPORT extern "C" __declspec (dllexport)
#elif defined(CR_LINUX) || defined(CR_OSX)
# define CR_EXPORT extern "C" __attribute__((visibility("default")))
#else
# error "Can't configure export macros. Compiler unrecognized."
#endif
#if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__) || defined(__amd64) || (defined(_MSC_VER) && defined(_M_X64))
# define CR_ARCH_X64
#elif defined(__i686) || defined(__i686__) || defined(__i386) || defined(__i386__) || defined(i386) || (defined(_MSC_VER) && defined(_M_IX86))
# define CR_ARCH_X86
#endif
#if (defined(CR_ARCH_X86) || defined(CR_ARCH_X64)) && !defined(CR_DEBUG)
# define CR_HAS_SSE2
#endif
CR_NAMESPACE_END
#if defined(CR_WINDOWS)
# include <direct.h>
# define stricmp _stricmp
#else
# include <unistd.h>
# include <sys/stat.h>
# define stricmp strcasecmp
#endif
#include <assert.h>
#if defined (CR_ANDROID)
# include <android/log.h>
#endif
CR_NAMESPACE_BEGIN
// helper struct for platform detection
struct Platform : public Singleton <Platform> {
bool isWindows = false;
bool isLinux = false;
bool isOSX = false;
bool isAndroid = false;
bool isAndroidHardFP = false;
bool isX64 = false;
Platform () {
#if defined(CR_WINDOWS)
isWindows = true;
#endif
#if defined(CR_ANDROID)
isAndroid = true;
# if defined (CR_ANDROID_HARD_FP)
isAndroidHardFP = true;
# endif
#endif
#if defined(CR_LINUX)
isLinux = true;
#endif
#if defined (CR_OSX)
isOSX = true;
#endif
#if defined (CR_ARCH_X64)
isX64 = true;
#endif
}
// helper platform-dependant functions
template <typename U> bool checkPointer (U *ptr) {
#if defined(CR_WINDOWS)
if (IsBadCodePtr (reinterpret_cast <FARPROC> (ptr))) {
return false;
}
#else
(void) (ptr);
#endif
return true;
}
bool createDirectory (const char *dir) {
int result = 0;
#if defined(CR_WINDOWS)
result = _mkdir (dir);
#else
result = mkdir (dir, 0777);
#endif
return !!result;
}
bool removeDirectory (const char *dir) {
#if defined(CR_WINDOWS)
_unlink (dir);
#else
unlink (dir);
#endif
return true;
}
bool hasModule (const char *mod) {
#if defined(CR_WINDOWS)
return GetModuleHandleA (mod) != nullptr;
#else
(void) (mod);
return true;
#endif
}
void abort (const char *msg = "OUT OF MEMORY!") noexcept {
fprintf (stderr, "%s\n", msg);
#if defined (CR_ANDROID)
__android_log_write (ANDROID_LOG_ERROR, "crlib.fatal", msg);
#endif
#if defined(CR_WINDOWS)
if (msg) {
DestroyWindow (GetForegroundWindow ());
MessageBoxA (GetActiveWindow (), msg, "crlib.fatal", MB_ICONSTOP);
}
#endif
::abort ();
}
};
// expose platform singleton
static auto &plat = Platform::get ();
CR_NAMESPACE_END

66
include/crlib/cr-random.h Normal file
View file

@ -0,0 +1,66 @@
//
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team.
//
// This software is licensed under the BSD-style license.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://yapb.ru/license
//
#pragma once
#include <time.h>
#include <crlib/cr-basic.h>
CR_NAMESPACE_BEGIN
// random number generator see: https://github.com/preshing/RandomSequence/
class Random final : public Singleton <Random> {
private:
uint32 m_index, m_offset;
uint64 m_divider;
public:
explicit Random () {
const auto base = static_cast <uint32> (time (nullptr));
const auto offset = base + 1;
m_index = premute (premute (base) + 0x682f0161);
m_offset = premute (premute (offset) + 0x46790905);
m_divider = (static_cast <uint64> (1)) << 32;
}
~Random () = default;
private:
uint32 premute (uint32 index) {
static constexpr auto prime = 4294967291u;
if (index >= prime) {
return index;
}
const uint32 residue = (static_cast <uint64> (index) * index) % prime;
return (index <= prime / 2) ? residue : prime - residue;
}
uint32 generate () {
return premute ((premute (m_index++) + m_offset) ^ 0x5bf03635);
}
public:
template <typename U> U int_ (U low, U high) {
return static_cast <U> (generate () * (static_cast <double> (high) - static_cast <double> (low) + 1.0) / m_divider + static_cast <double> (low));
}
float float_ (float low, float high) {
return static_cast <float> (generate () * (static_cast <double> (high) - static_cast <double> (low)) / (m_divider - 1) + static_cast <double> (low));
}
template <typename U> bool chance (const U max, const U maxChance = 100) {
return int_ <U> (0, maxChance) < max;
}
};
// expose global random generator
static auto &rg = Random::get ();
CR_NAMESPACE_END

870
include/crlib/cr-string.h Normal file
View file

@ -0,0 +1,870 @@
//
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team.
//
// This software is licensed under the BSD-style license.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://yapb.ru/license
//
#pragma once
#include <string.h>
#include <ctype.h>
#include <crlib/cr-basic.h>
#include <crlib/cr-alloc.h>
#include <crlib/cr-movable.h>
#include <crlib/cr-twin.h>
#include <crlib/cr-uniqueptr.h>
CR_NAMESPACE_BEGIN
// small-string optimized string class, sso stuff based on: https://github.com/elliotgoodrich/SSO-23/
class String final {
public:
static constexpr size_t kInvalidIndex = static_cast <size_t> (-1);
private:
static constexpr size_t kExcessSpace = 32;
private:
using Length = Twin <size_t, size_t>;
private:
union Data {
struct Big {
char excess[kExcessSpace - sizeof (char *) - 2 * sizeof (size_t)];
char *ptr;
size_t length;
size_t capacity;
} big;
struct Small {
char str[sizeof (Big) / sizeof (char) - 1];
uint8 length;
} small;
} m_data;
private:
static size_t const kSmallCapacity = sizeof (typename Data::Big) / sizeof (char) - 1;
public:
explicit String () {
reset ();
assign ("", 0);
}
String (const char *str, size_t length = 0) {
reset ();
assign (str, length);
}
String (const String &str) {
reset ();
assign (str.data (), str.length ());
}
String (const char ch) {
reset ();
assign (ch);
}
String (String &&rhs) noexcept {
m_data = rhs.m_data;
rhs.setMoved ();
}
~String () {
destroy ();
}
private:
template <typename T> static uint8 &getMostSignificantByte (T &object) {
return *(reinterpret_cast <uint8 *> (&object) + sizeof (object) - 1);
}
template <size_t N> static bool getLeastSignificantBit (uint8 byte) {
return byte & cr::bit (N);
}
template <size_t N> static bool getMostSignificantBit (uint8 byte) {
return byte & cr::bit (CHAR_BIT - N - 1);
}
template <size_t N> static void setLeastSignificantBit (uint8 &byte, bool bit) {
if (bit) {
byte |= cr::bit (N);
}
else {
byte &= ~cr::bit (N);
}
}
template <size_t N> static void setMostSignificantBit (uint8 &byte, bool bit) {
if (bit) {
byte |= cr::bit (CHAR_BIT - N - 1);
}
else {
byte &= ~cr::bit (CHAR_BIT - N - 1);
}
}
void destroy () {
if (!isSmall ()) {
alloc.deallocate (m_data.big.ptr);
}
}
void reset () {
m_data.small.length = 0;
m_data.small.str[0] = '\0';
m_data.big.ptr = nullptr;
m_data.big.length = 0;
}
void endString (const char *str, size_t at) {
const_cast <char *> (str)[at] = '\0';
}
void moveString (const char *dst, const char *src, size_t length) {
if (!dst) {
return;
}
memmove (const_cast <char *> (dst), src, length);
}
const char *data () const {
return isSmall () ? m_data.small.str : m_data.big.ptr;
}
void setMoved () {
setSmallLength (0);
}
void setLength (size_t amount, size_t capacity) {
if (amount <= kSmallCapacity) {
endString (m_data.small.str, amount);
setSmallLength (static_cast <uint8> (amount));
}
else {
endString (m_data.big.ptr, amount);
setDataNonSmall (amount, capacity);
}
}
bool isSmall () const {
return !getLeastSignificantBit <0> (m_data.small.length) && !getLeastSignificantBit <1> (m_data.small.length);
}
void setSmallLength (uint8 length) {
m_data.small.length = static_cast <char> (kSmallCapacity - length) << 2;
}
size_t getSmallLength () const {
return kSmallCapacity - ((m_data.small.length >> 2) & 63u);
}
void setDataNonSmall (size_t length, size_t capacity) {
uint8 &lengthHighByte = getMostSignificantByte (length);
uint8 &capacityHighByte = getMostSignificantByte (capacity);
const bool lengthHasHighBit = getMostSignificantBit <0> (lengthHighByte);
const bool capacityHasHighBit = getMostSignificantBit <0> (capacityHighByte);
const bool capacityHasSecHighBit = getMostSignificantBit <1> (capacityHighByte);
setMostSignificantBit <0> (lengthHighByte, capacityHasSecHighBit);
capacityHighByte <<= 2;
setLeastSignificantBit <0> (capacityHighByte, capacityHasHighBit);
setLeastSignificantBit <1> (capacityHighByte, !lengthHasHighBit);
m_data.big.length = length;
m_data.big.capacity = capacity;
}
Length getDataNonSmall () const {
size_t length = m_data.big.length;
size_t capacity = m_data.big.capacity;
uint8 &lengthHighByte = getMostSignificantByte (length);
uint8 &capacityHighByte = getMostSignificantByte (capacity);
const bool capacityHasHighBit = getLeastSignificantBit <0> (capacityHighByte);
const bool lengthHasHighBit = !getLeastSignificantBit <1> (capacityHighByte);
const bool capacityHasSecHighBit = getMostSignificantBit <0> (lengthHighByte);
setMostSignificantBit <0> (lengthHighByte, lengthHasHighBit);
capacityHighByte >>= 2;
setMostSignificantBit <0> (capacityHighByte, capacityHasHighBit);
setMostSignificantBit <1> (capacityHighByte, capacityHasSecHighBit);
return { length, capacity };
}
public:
String &assign (const char *str, size_t length = 0) {
length = length > 0 ? length : strlen (str);
if (length <= kSmallCapacity) {
moveString (m_data.small.str, str, length);
endString (m_data.small.str, length);
setSmallLength (static_cast <uint8> (length));
}
else {
auto capacity = cr::max (kSmallCapacity * 2, length);
m_data.big.ptr = alloc.allocate <char> (capacity + 1);
if (m_data.big.ptr) {
moveString (m_data.big.ptr, str, length);
endString (m_data.big.ptr, length);
setDataNonSmall (length, capacity);
}
}
return *this;
}
String &assign (const String &str, size_t length = 0) {
return assign (str.chars (), length);
}
String &assign (const char ch) {
const char str[] { ch, '\0' };
return assign (str, strlen (str));
}
String &append (const char *str, size_t length = 0) {
if (empty ()) {
return assign (str, length);
}
length = length > 0 ? length : strlen (str);
size_t oldLength = this->length ();
size_t newLength = oldLength + length;
resize (newLength);
moveString (&data ()[oldLength], str, length);
endString (data (), newLength);
return *this;
}
String &append (const String &str, size_t length = 0) {
return append (str.chars (), length);
}
String &append (const char ch) {
const char str[] { ch, '\0' };
return append (str, strlen (str));
}
template <typename ...Args> String &assignf (const char *fmt, Args ...args) {
const size_t size = snprintf (nullptr, 0, fmt, args...);
Array <char, ReservePolicy::PlusOne> buffer (size + 1);
snprintf (buffer.data (), size + 1, fmt, cr::forward <Args> (args)...);
return assign (buffer.data ());
}
template <typename ...Args> String &appendf (const char *fmt, Args ...args) {
if (empty ()) {
return assignf (fmt, cr::forward <Args> (args)...);
}
const size_t size = snprintf (nullptr, 0, fmt, args...) + length ();
Array <char, ReservePolicy::PlusOne> buffer (size + 1);
snprintf (buffer.data (), size + 1, fmt, cr::forward <Args> (args)...);
return append (buffer.data ());
}
void resize (size_t amount) {
size_t oldLength = length ();
if (amount <= kSmallCapacity) {
if (!isSmall ()) {
auto ptr = m_data.big.ptr;
moveString (m_data.small.str, ptr, cr::min (oldLength, amount));
alloc.deallocate (ptr);
}
setLength (amount, 0);
}
else {
size_t newCapacity = 0;
if (isSmall ()) {
newCapacity = cr::max (amount, kSmallCapacity * 2);
auto ptr = alloc.allocate <char> (newCapacity + 1);
moveString (ptr, m_data.small.str, cr::min (oldLength, amount));
m_data.big.ptr = ptr;
}
else if (amount < capacity ()) {
newCapacity = capacity ();
}
else {
newCapacity = cr::max (amount, capacity () * 3 / 2);
auto ptr = alloc.allocate <char> (newCapacity + 1);
moveString (ptr, m_data.big.ptr, cr::min (oldLength, amount));
alloc.deallocate (m_data.big.ptr);
m_data.big.ptr = ptr;
}
setLength (amount, newCapacity);
}
}
bool insert (size_t index, const String &str) {
if (str.empty ()) {
return false;
}
const size_t strLength = str.length ();
const size_t dataLength = length ();
if (index >= dataLength) {
append (str.chars (), strLength);
}
else {
resize (dataLength + strLength);
for (size_t i = dataLength; i > index; --i) {
at (i + strLength - 1) = at (i - 1);
}
for (size_t i = 0; i < strLength; ++i) {
at (i + index) = str.at (i);
}
}
return true;
}
bool erase (size_t index, size_t count = 1) {
const size_t dataLength = length ();
if (index + count > dataLength) {
return false;
}
const size_t newLength = dataLength - count;
for (size_t i = index; i < newLength; ++i) {
at (i) = at (i + count);
}
resize (newLength);
return true;
}
size_t find (char pattern, size_t start = 0) const {
for (size_t i = start; i < length (); ++i) {
if (at (i) == pattern) {
return i;
}
}
return kInvalidIndex;
}
size_t find (const String &pattern, size_t start = 0) const {
const size_t patternLength = pattern.length ();
const size_t dataLength = length ();
if (patternLength > dataLength || start > dataLength) {
return kInvalidIndex;
}
for (size_t i = start; i <= dataLength - patternLength; ++i) {
size_t index = 0;
for (; at (index) && index < patternLength; ++index) {
if (at (i + index) != pattern.at (index)) {
break;
}
}
if (!pattern.at (index)) {
return i;
}
}
return kInvalidIndex;
}
size_t rfind (char pattern) const {
for (size_t i = length (); i != 0; i--) {
if (at (i) == pattern) {
return i;
}
}
return kInvalidIndex;
}
size_t rfind (const String &pattern) const {
const size_t patternLength = pattern.length ();
const size_t dataLength = length ();
if (patternLength > dataLength) {
return kInvalidIndex;
}
bool match = true;
for (size_t i = dataLength - 1; i >= patternLength; i--) {
match = true;
for (size_t j = patternLength - 1; j > 0; j--) {
if (at (i + j) != pattern.at (j)) {
match = false;
break;
}
}
if (match) {
return i;
}
}
return kInvalidIndex;
}
size_t findFirstOf (const String &pattern, size_t start = 0) const {
const size_t patternLength = pattern.length ();
const size_t dataLength = length ();
for (size_t i = start; i < dataLength; ++i) {
for (size_t j = 0; j < patternLength; ++j) {
if (at (i) == pattern.at (j)) {
return i;
}
}
}
return kInvalidIndex;
}
size_t findLastOf (const String &pattern) const {
const size_t patternLength = pattern.length ();
const size_t dataLength = length ();
for (size_t i = dataLength - 1; i > 0; i--) {
for (size_t j = 0; j < patternLength; ++j) {
if (at (i) == pattern.at (j)) {
return i;
}
}
}
return kInvalidIndex;
}
size_t findFirstNotOf (const String &pattern, size_t start = 0) const {
const size_t patternLength = pattern.length ();
const size_t dataLength = length ();
bool different = true;
for (size_t i = start; i < dataLength; ++i) {
different = true;
for (size_t j = 0; j < patternLength; ++j) {
if (at (i) == pattern.at (j)) {
different = false;
break;
}
}
if (different) {
return i;
}
}
return kInvalidIndex;
}
size_t findLastNotOf (const String &pattern) const {
const size_t patternLength = pattern.length ();
const size_t dataLength = length ();
bool different = true;
for (size_t i = dataLength - 1; i > 0; i--) {
different = true;
for (size_t j = 0; j < patternLength; ++j) {
if (at (i) == pattern.at (j)) {
different = false;
break;
}
}
if (different) {
return i;
}
}
return kInvalidIndex;
}
size_t countChar (char ch) const {
size_t count = 0;
for (size_t i = 0, e = length (); i != e; ++i) {
if (at (i) == ch) {
++count;
}
}
return count;
}
size_t countStr (const String &pattern) const {
const size_t patternLen = pattern.length ();
const size_t dataLength = length ();
if (patternLen > dataLength) {
return 0;
}
size_t count = 0;
for (size_t i = 0, e = dataLength - patternLen + 1; i != e; ++i) {
if (substr (i, patternLen).compare (pattern)) {
++count;
}
}
return count;
}
String substr (size_t start, size_t count = kInvalidIndex) const {
start = cr::min (start, length ());
if (count == kInvalidIndex) {
count = length ();
}
return String (data () + start, cr::min (count, length () - start));
}
size_t replace (const String &needle, const String &to) {
if (needle.empty () || to.empty ()) {
return 0;
}
size_t replaced = 0, pos = 0;
while (pos < length ()) {
pos = find (needle, pos);
if (pos == kInvalidIndex) {
break;
}
erase (pos, needle.length ());
insert (pos, to);
pos += to.length ();
replaced++;
}
return replaced;
}
bool startsWith (const String &prefix) const {
const size_t prefixLength = prefix.length ();
const size_t dataLength = length ();
return prefixLength <= dataLength && strncmp (data (), prefix.data (), prefixLength) == 0;
}
bool endsWith (const String &suffix) const {
const size_t suffixLength = suffix.length ();
const size_t dataLength = length ();
return suffixLength <= dataLength && strncmp (data () + dataLength - suffixLength, suffix.data (), suffixLength) == 0;
}
Array <String> split (const String &delim) const {
Array <String> tokens;
size_t prev = 0, pos = 0;
while ((pos = find (delim, pos)) != kInvalidIndex) {
tokens.push (substr (prev, pos - prev));
prev = ++pos;
}
tokens.push (substr (prev, pos - prev));
return tokens;
}
size_t length () const {
if (isSmall ()) {
return getSmallLength ();
}
else {
return getDataNonSmall ().first;
}
}
size_t capacity () const {
if (isSmall ()) {
return sizeof (m_data) - 1;
}
else {
return getDataNonSmall ().second;
}
}
bool small () const {
return isSmall ();
}
bool empty () const {
return length () == 0;
}
void clear () {
assign ("");
}
const char *chars () const {
return data ();
}
const char &at (size_t index) const {
return begin ()[index];
}
char &at (size_t index) {
return begin ()[index];
}
int32 compare (const String &rhs) const {
return strcmp (rhs.data (), data ());
}
int32 compare (const char *rhs) const {
return strcmp (rhs, data ());
}
bool contains (const String &rhs) const {
return find (rhs) != kInvalidIndex;
}
String &lowercase () {
for (auto &ch : *this) {
ch = static_cast <char> (::tolower (ch));
}
return *this;
}
String &uppercase () {
for (auto &ch : *this) {
ch = static_cast <char> (::toupper (ch));
}
return *this;
}
int32 int_ () const {
return atoi (data ());
}
float float_ () const {
return static_cast <float> (atof (data ()));
}
String &ltrim (const String &characters = "\r\n\t ") {
size_t begin = length ();
for (size_t i = 0; i < begin; ++i) {
if (characters.find (at (i)) == kInvalidIndex) {
begin = i;
break;
}
}
return *this = substr (begin, length () - begin);
}
String &rtrim (const String &characters = "\r\n\t ") {
size_t end = 0;
for (size_t i = length (); i > 0; --i) {
if (characters.find (at (i - 1)) == kInvalidIndex) {
end = i;
break;
}
}
return *this = substr (0, end);
}
String &trim (const String &characters = "\r\n\t ") {
return ltrim (characters).rtrim (characters);
}
public:
String &operator = (String &&rhs) noexcept {
destroy ();
m_data = rhs.m_data;
rhs.setMoved ();
return *this;
}
String &operator = (const String &rhs) {
return assign (rhs);
}
String &operator = (const char *rhs) {
return assign (rhs);
}
String &operator = (char rhs) {
return assign (rhs);
}
String &operator += (const String &rhs) {
return append (rhs);
}
String &operator += (const char *rhs) {
return append (rhs);
}
const char &operator [] (size_t index) const {
return at (index);
}
char &operator [] (size_t index) {
return at (index);
}
friend String operator + (const String &lhs, char rhs) {
return String (lhs).append (rhs);
}
friend String operator + (char lhs, const String &rhs) {
return String (lhs).append (rhs);
}
friend String operator + (const String &lhs, const char *rhs) {
return String (lhs).append (rhs);
}
friend String operator + (const char *lhs, const String &rhs) {
return String (lhs).append (rhs);
}
friend String operator + (const String &lhs, const String &rhs) {
return String (lhs).append (rhs);
}
friend bool operator == (const String &lhs, const String &rhs) {
return lhs.compare (rhs) == 0;
}
friend bool operator < (const String &lhs, const String &rhs) {
return lhs.compare (rhs) < 0;
}
friend bool operator > (const String &lhs, const String &rhs) {
return lhs.compare (rhs) > 0;
}
friend bool operator == (const char *lhs, const String &rhs) {
return rhs.compare (lhs) == 0;
}
friend bool operator == (const String &lhs, const char *rhs) {
return lhs.compare (rhs) == 0;
}
friend bool operator != (const String &lhs, const String &rhs) {
return lhs.compare (rhs) != 0;
}
friend bool operator != (const char *lhs, const String &rhs) {
return rhs.compare (lhs) != 0;
}
friend bool operator != (const String &lhs, const char *rhs) {
return lhs.compare (rhs) != 0;
}
public:
static String join (const Array <String> &sequence, const String &delim, size_t start = 0) {
if (sequence.empty ()) {
return "";
}
if (sequence.length () == 1) {
return sequence.at (0);
}
String result;
for (size_t index = start; index < sequence.length (); ++index) {
if (index != start) {
result += delim + sequence[index];
}
else {
result += sequence[index];
}
}
return result;
}
// for range-based loops
public:
char *begin () {
return const_cast <char *> (data ());
}
char *begin () const {
return const_cast <char *> (data ());
}
char *end () {
return begin () + length ();
}
char *end () const {
return begin () + length ();
}
};
// simple rotation-string pool for holding temporary data passed to different modules and for formatting
class StringBuffer final : public Singleton <StringBuffer> {
public:
enum : size_t {
StaticBufferSize = static_cast <size_t> (768),
RotationCount = static_cast <size_t> (32)
};
private:
char m_data[RotationCount + 1][StaticBufferSize] {};
size_t m_rotate = 0;
public:
StringBuffer () = default;
~StringBuffer () = default;
public:
char *chars () {
if (++m_rotate >= RotationCount) {
m_rotate = 0;
}
return m_data[cr::clamp <size_t> (m_rotate, 0, RotationCount)];
}
template <typename U, typename ...Args> U *format (const U *fmt, Args ...args) {
auto buffer = Singleton <StringBuffer>::get ().chars ();
snprintf (buffer, StaticBufferSize, fmt, args...);
return buffer;
}
template <typename U> U *format (const U *fmt) {
auto buffer = Singleton <StringBuffer>::get ().chars ();
strncpy (buffer, fmt, StaticBufferSize);
return buffer;
}
};
// expose global string pool
static auto &strings = StringBuffer::get ();
CR_NAMESPACE_END

75
include/crlib/cr-twin.h Normal file
View file

@ -0,0 +1,75 @@
//
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team.
//
// This software is licensed under the BSD-style license.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://yapb.ru/license
//
#pragma once
#include <crlib/cr-basic.h>
#include <crlib/cr-movable.h>
CR_NAMESPACE_BEGIN
// simple pair (twin)
template <typename A, typename B> class Twin {
public:
A first;
B second;
public:
template <typename T, typename U> Twin (T &&a, U &&b) : first (cr::forward <T> (a)), second (cr::forward <U> (b)) { }
template <typename T, typename U> Twin (const Twin <T, U> &rhs) : first (rhs.first), second (rhs.second) { }
template <typename T, typename U> Twin (Twin <T, U> &&rhs) noexcept : first (cr::move (rhs.first)), second (cr::move (rhs.second)) { }
public:
explicit Twin () = default;
~Twin () = default;
public:
template <typename T, typename U> Twin &operator = (const Twin <T, U> &rhs) {
first = rhs.first;
second = rhs.second;
return *this;
}
template <typename T, typename U> Twin &operator = (Twin <T, U> &&rhs) {
first = cr::move (rhs.first);
second = cr::move (rhs.second);
return *this;
}
// specialized operators for binary heap, do not use as it's test only second element
public:
friend bool operator < (const Twin &a, const Twin &b) {
return a.second < b.second;
}
friend bool operator <= (const Twin &a, const Twin &b) {
return a.second <= b.second;
}
friend bool operator > (const Twin &a, const Twin &b) {
return b.second < a.second;
}
friend bool operator >= (const Twin &a, const Twin &b) {
return b.second <= a.second;
}
};
// creating pairs
template <typename A, typename B> constexpr Twin <A, B> makeTwin (A &&a, B &&b) {
return Twin <A, B> (cr::forward <A> (a), cr::forward <B> (b));
}
template <typename A, typename B> constexpr Twin <A, B> makeTwin (const A &a, const B &b) {
return Twin <A, B> (a, b);
}
CR_NAMESPACE_END

314
include/crlib/cr-ulz.h Normal file
View file

@ -0,0 +1,314 @@
//
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team.
//
// This software is licensed under the BSD-style license.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://yapb.ru/license
//
#pragma once
#include <crlib/cr-array.h>
CR_NAMESPACE_BEGIN
// see https://github.com/encode84/ulz/
class ULZ final : DenyCopying {
public:
enum : int32 {
Excess = 16,
UncompressFailure = -1
};
private:
enum : int32 {
WindowBits = 17,
WindowSize = cr::bit (WindowBits),
WindowMask = WindowSize - 1,
MinMatch = 4,
MaxChain = cr::bit (5),
HashBits = 19,
HashLength = cr::bit (HashBits),
EmptyHash = -1,
};
private:
Array <int32, ReservePolicy::PlusOne> m_hashTable;
Array <int32, ReservePolicy::PlusOne> m_prevTable;
public:
ULZ () {
m_hashTable.resize (HashLength);
m_prevTable.resize (WindowSize);
}
~ULZ () = default;
public:
int32 compress (uint8 *in, int32 inputLength, uint8 *out) {
for (auto &htb : m_hashTable) {
htb = EmptyHash;
}
uint8 *op = out;
int32 anchor = 0;
int32 cur = 0;
while (cur < inputLength) {
const int32 maxMatch = inputLength - cur;
int32 bestLength = 0;
int32 dist = 0;
if (maxMatch >= MinMatch) {
const int32 limit = cr::max <int32> (cur - WindowSize, EmptyHash);
int32 chainLength = MaxChain;
int32 lookup = m_hashTable[hash32 (&in[cur])];
while (lookup > limit) {
if (in[lookup + bestLength] == in[cur + bestLength] && load32 (&in[lookup]) == load32 (&in[cur])) {
int32 length = MinMatch;
while (length < maxMatch && in[lookup + length] == in[cur + length]) {
length++;
}
if (length > bestLength) {
bestLength = length;
dist = cur - lookup;
if (length == maxMatch) {
break;
}
}
}
if (--chainLength == 0) {
break;
}
lookup = m_prevTable[lookup & WindowMask];
}
}
if (bestLength == MinMatch && (cur - anchor) >= (7 + 128)) {
bestLength = 0;
}
if (bestLength >= MinMatch && bestLength < maxMatch && (cur - anchor) != 6) {
const int32 next = cur + 1;
const int32 target = bestLength + 1;
const int32 limit = cr::max <int32> (next - WindowSize, EmptyHash);
int32 chainLength = MaxChain;
int32 lookup = m_hashTable[hash32 (&in[next])];
while (lookup > limit) {
if (in[lookup + bestLength] == in[next + bestLength] && load32 (&in[lookup]) == load32 (&in[next])) {
int32 length = MinMatch;
while (length < target && in[lookup + length] == in[next + length]) {
length++;
}
if (length == target) {
bestLength = 0;
break;
}
}
if (--chainLength == 0) {
break;
}
lookup = m_prevTable[lookup & WindowMask];
}
}
if (bestLength >= MinMatch) {
const int32 length = bestLength - MinMatch;
const int32 token = ((dist >> 12) & 16) + cr::min <int32> (length, 15);
if (anchor != cur) {
const int32 run = cur - anchor;
if (run >= 7) {
add (op, (7 << 5) + token);
encode (op, run - 7);
}
else {
add (op, (run << 5) + token);
}
copy (op, &in[anchor], run);
op += run;
}
else {
add (op, token);
}
if (length >= 15) {
encode (op, length - 15);
}
store16 (op, dist);
op += 2;
while (bestLength-- != 0) {
const uint32 hash = hash32 (&in[cur]);
m_prevTable[cur & WindowMask] = m_hashTable[hash];
m_hashTable[hash] = cur++;
}
anchor = cur;
}
else {
const uint32 hash = hash32 (&in[cur]);
m_prevTable[cur & WindowMask] = m_hashTable[hash];
m_hashTable[hash] = cur++;
}
}
if (anchor != cur) {
const int32 run = cur - anchor;
if (run >= 7) {
add (op, 7 << 5);
encode (op, run - 7);
}
else {
add (op, run << 5);
}
copy (op, &in[anchor], run);
op += run;
}
return op - out;
}
int32 uncompress (uint8 *in, int32 inputLength, uint8 *out, int32 outLength) {
uint8 *op = out;
uint8 *ip = in;
const uint8 *opEnd = op + outLength;
const uint8 *ipEnd = ip + inputLength;
while (ip < ipEnd) {
const int32 token = *ip++;
if (token >= 32) {
int32 run = token >> 5;
if (run == 7) {
run += decode (ip);
}
if ((opEnd - op) < run || (ipEnd - ip) < run) {
return UncompressFailure;
}
copy (op, ip, run);
op += run;
ip += run;
if (ip >= ipEnd) {
break;
}
}
int32 length = (token & 15) + MinMatch;
if (length == (15 + MinMatch)) {
length += decode (ip);
}
if ((opEnd - op) < length) {
return UncompressFailure;
}
const int32 dist = ((token & 16) << 12) + load16 (ip);
ip += 2;
uint8 *cp = op - dist;
if ((op - out) < dist) {
return UncompressFailure;
}
if (dist >= 8) {
copy (op, cp, length);
op += length;
}
else
{
for (int32 i = 0; i < 4; ++i) {
*op++ = *cp++;
}
while (length-- != 4) {
*op++ = *cp++;
}
}
}
return static_cast <int32> (ip == ipEnd) ? static_cast <int32> (op - out) : UncompressFailure;
}
private:
inline uint16 load16 (void *ptr) {
return *reinterpret_cast <const uint16 *> (ptr);
}
inline uint32 load32 (void *ptr) {
return *reinterpret_cast <const uint32 *> (ptr);
}
inline void store16 (void *ptr, int32 val) {
*reinterpret_cast <uint16 *> (ptr) = static_cast <uint16> (val);
}
inline void copy64 (void *dst, void *src) {
*reinterpret_cast <uint64 *> (dst) = *reinterpret_cast <const uint64 *> (src);
}
inline uint32 hash32 (void *ptr) {
return (load32 (ptr) * 0x9E3779B9) >> (32 - HashBits);
}
inline void copy (uint8 *dst, uint8 *src, int32 count) {
copy64 (dst, src);
for (int32 i = 8; i < count; i += 8) {
copy64 (dst + i, src + i);
}
}
inline void add (uint8 *&dst, int32 val) {
*dst++ = static_cast <uint8> (val);
}
inline void encode (uint8 *&ptr, uint32 val) {
while (val >= 128) {
val -= 128;
*ptr++ = 128 + (val & 127);
val >>= 7;
}
*ptr++ = static_cast <uint8> (val);
}
inline uint32 decode (uint8 *&ptr) {
uint32 val = 0;
for (int32 i = 0; i <= 21; i += 7) {
const uint32 cur = *ptr++;
val += cur << i;
if (cur < 128) {
break;
}
}
return val;
}
};
CR_NAMESPACE_END

View file

@ -0,0 +1,99 @@
//
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team.
//
// This software is licensed under the BSD-style license.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://yapb.ru/license
//
#pragma once
#include <crlib/cr-basic.h>
#include <crlib/cr-movable.h>
CR_NAMESPACE_BEGIN
// simple unique ptr
template <typename T> class UniquePtr final : public DenyCopying {
private:
T *m_ptr = nullptr;
public:
UniquePtr () = default;
explicit UniquePtr (T *ptr) : m_ptr (ptr)
{ }
UniquePtr (UniquePtr &&rhs) noexcept : m_ptr (rhs.m_ptr) {
rhs.m_ptr = nullptr;
}
~UniquePtr () {
destroy ();
}
public:
T *get () const {
return m_ptr;
}
T *release () {
auto ret = m_ptr;
m_ptr = nullptr;
return ret;
}
void reset (T *ptr = nullptr) {
destroy ();
m_ptr = ptr;
}
private:
void destroy () {
if (m_ptr) {
alloc.destroy (m_ptr);
m_ptr = nullptr;
}
}
public:
UniquePtr &operator = (UniquePtr &&rhs) noexcept {
if (this != &rhs) {
destroy ();
m_ptr = rhs.m_ptr;
rhs.m_ptr = nullptr;
}
return *this;
}
UniquePtr &operator = (decltype (nullptr)) {
destroy ();
return *this;
}
T &operator * () const {
return *m_ptr;
}
T *operator -> () const {
return m_ptr;
}
explicit operator bool () const {
return m_ptr != nullptr;
}
};
// create unique
template <typename T, typename... Args> UniquePtr <T> createUnique (Args &&... args) {
return UniquePtr <T> (alloc.create <T> (cr::forward <Args> (args)...));
}
// create unique (base class)
template <typename D, typename B, typename... Args> UniquePtr <B> createUniqueBase (Args &&... args) {
return UniquePtr <B> (alloc.create <D> (cr::forward <Args> (args)...));
}
CR_NAMESPACE_END

232
include/crlib/cr-vector.h Normal file
View file

@ -0,0 +1,232 @@
//
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) YaPB Development Team.
//
// This software is licensed under the BSD-style license.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://yapb.ru/license
//
#pragma once
#include <crlib/cr-math.h>
CR_NAMESPACE_BEGIN
// 3dmath vector
class Vector final {
public:
float x = 0.0f, y = 0.0f, z = 0.0f;
public:
Vector (const float scaler = 0.0f) : x (scaler), y (scaler), z (scaler)
{ }
explicit Vector (const float _x, const float _y, const float _z) : x (_x), y (_y), z (_z)
{ }
Vector (float *rhs) : x (rhs[0]), y (rhs[1]), z (rhs[2])
{ }
Vector (const Vector &) = default;
public:
operator float *() {
return &x;
}
operator const float * () const {
return &x;
}
Vector operator + (const Vector &rhs) const {
return Vector (x + rhs.x, y + rhs.y, z + rhs.z);
}
Vector operator - (const Vector &rhs) const {
return Vector (x - rhs.x, y - rhs.y, z - rhs.z);
}
Vector operator - () const {
return Vector (-x, -y, -z);
}
friend Vector operator * (const float scale, const Vector &rhs) {
return Vector (rhs.x * scale, rhs.y * scale, rhs.z * scale);
}
Vector operator * (const float scale) const {
return Vector (scale * x, scale * y, scale * z);
}
Vector operator / (const float div) const {
const float inv = 1 / div;
return Vector (inv * x, inv * y, inv * z);
}
// cross product
Vector operator ^ (const Vector &rhs) const {
return Vector (y * rhs.z - z * rhs.y, z * rhs.x - x * rhs.z, x * rhs.y - y * rhs.x);
}
// dot product
float operator | (const Vector &rhs) const {
return x * rhs.x + y * rhs.y + z * rhs.z;
}
const Vector &operator += (const Vector &rhs) {
x += rhs.x;
y += rhs.y;
z += rhs.z;
return *this;
}
const Vector &operator -= (const Vector &right) {
x -= right.x;
y -= right.y;
z -= right.z;
return *this;
}
const Vector &operator *= (float scale) {
x *= scale;
y *= scale;
z *= scale;
return *this;
}
const Vector &operator /= (float div) {
const float inv = 1 / div;
x *= inv;
y *= inv;
z *= inv;
return *this;
}
bool operator == (const Vector &rhs) const {
return cr::fequal (x, rhs.x) && cr::fequal (y, rhs.y) && cr::fequal (z, rhs.z);
}
bool operator != (const Vector &rhs) const {
return !cr::fequal (x, rhs.x) && !cr::fequal (y, rhs.y) && !cr::fequal (z, rhs.z);
}
Vector &operator = (const Vector &) = default;
public:
float length () const {
return cr::sqrtf (lengthSq ());
}
float length2d () const {
return cr::sqrtf (x * x + y * y);
}
float lengthSq () const {
return x * x + y * y + z * z;
}
Vector get2d () const {
return Vector (x, y, 0.0f);
}
Vector normalize () const {
float len = length () + cr::kFloatCmpEpsilon;
if (cr::fzero (len)) {
return Vector (0.0f, 0.0f, 1.0f);
}
len = 1.0f / len;
return Vector (x * len, y * len, z * len);
}
Vector normalize2d () const {
float len = length2d () + cr::kFloatCmpEpsilon;
if (cr::fzero (len)) {
return Vector (0.0f, 1.0f, 0.0f);
}
len = 1.0f / len;
return Vector (x * len, y * len, 0.0f);
}
bool empty () const {
return cr::fzero (x) && cr::fzero (y) && cr::fzero (z);
}
static const Vector &null () {
static const auto s_zero = Vector (0.0f, 0.0f, 0.0f);
return s_zero;
}
void clear () {
x = y = z = 0.0f;
}
Vector clampAngles () {
x = cr::normalizeAngles (x);
y = cr::normalizeAngles (y);
z = 0.0f;
return *this;
}
float pitch () const {
if (cr::fzero (x) && cr::fzero (y)) {
return 0.0f;
}
return cr::degreesToRadians (cr::atan2f (z, length2d ()));
}
float yaw () const {
if (cr::fzero (x) && cr::fzero (y)) {
return 0.0f;
}
return cr::radiansToDegrees (cr:: atan2f (y, x));
}
Vector angles () const {
if (cr::fzero (x) && cr::fzero (y)) {
return Vector (z > 0.0f ? 90.0f : 270.0f, 0.0, 0.0f);
}
return Vector (cr::radiansToDegrees (cr::atan2f (z, length2d ())), cr::radiansToDegrees (cr::atan2f (y, x)), 0.0f);
}
void buildVectors (Vector *forward, Vector *right, Vector *upward) const {
enum { pitch, yaw, roll, unused, max };
float sines[max] = { 0.0f, 0.0f, 0.0f, 0.0f };
float cosines[max] = { 0.0f, 0.0f, 0.0f, 0.0f };
// compute the sine and cosine compontents
cr::sincosf (cr::degreesToRadians (x), cr::degreesToRadians (y), cr::degreesToRadians (z), sines, cosines);
if (forward) {
forward->x = cosines[pitch] * cosines[yaw];
forward->y = cosines[pitch] * sines[yaw];
forward->z = -sines[pitch];
}
if (right) {
right->x = -sines[roll] * sines[pitch] * cosines[yaw] + cosines[roll] * sines[yaw];
right->y = -sines[roll] * sines[pitch] * sines[yaw] - cosines[roll] * cosines[yaw];
right->z = -sines[roll] * cosines[pitch];
}
if (upward) {
upward->x = cosines[roll] * sines[pitch] * cosines[yaw] + sines[roll] * sines[yaw];
upward->y = cosines[roll] * sines[pitch] * sines[yaw] - sines[roll] * cosines[yaw];
upward->z = cosines[roll] * cosines[pitch];
}
}
};
// expose global null vector
static auto &nullvec = Vector::null ();
CR_NAMESPACE_END