This commit is contained in:
Isuru Samarathunga
2025-07-18 22:37:47 +05:30
parent f2e6cee915
commit a324de4111
10 changed files with 428 additions and 234 deletions

View File

@ -31,15 +31,15 @@ namespace ia
define_member_function(, FixedContainer, IN size_type count) define_member_function(, FixedContainer, IN size_type count)
{ {
m_count = count; Base::m_count = count;
m_data = alloc<_alignment>(m_allocator, m_count); Base::m_data = alloc<_alignment>(m_allocator, Base::m_count);
m_allocator.construct(m_data, m_count); m_allocator.construct(Base::m_data, Base::m_count);
} }
define_member_function(, FixedContainer, IN initializer_list<value_type> v) define_member_function(, FixedContainer, IN initializer_list<value_type> v)
{ {
m_data = alloc<_alignment>(m_allocator, v.size()); Base::m_data = alloc<_alignment>(m_allocator, v.size());
for(const auto& value: v) new (&m_data[m_count++]) value_type(value); for(const auto& value: v) new (&Base::m_data[Base::m_count++]) value_type(value);
} }
define_member_function(, ~FixedContainer,) define_member_function(, ~FixedContainer,)
@ -49,19 +49,19 @@ namespace ia
define_member_function(VOID, reset,) define_member_function(VOID, reset,)
{ {
if(m_data) if(Base::m_data)
{ {
m_allocator.destruct(m_data, m_count); m_allocator.destruct(Base::m_data, Base::m_count);
dealloc<_alignment>(m_allocator, m_data); dealloc<_alignment>(m_allocator, Base::m_data);
m_data = nullptr; Base::m_data = nullptr;
} }
m_count = 0; Base::m_count = 0;
} }
define_member_function(__ia__identifier::pointer_type, take,) define_member_function(__ia__identifier::pointer_type, take,)
{ {
auto res = m_data; auto res = Base::m_data;
m_data = nullptr; Base::m_data = nullptr;
reset(); reset();
return res; return res;
} }
@ -69,15 +69,15 @@ namespace ia
define_member_function(VOID, copy_from, IN CONST __ia__identifier& r) define_member_function(VOID, copy_from, IN CONST __ia__identifier& r)
{ {
m_allocator = r.m_allocator.selectOnContainerCopy(); m_allocator = r.m_allocator.selectOnContainerCopy();
m_data = alloc<_alignment>(m_allocator, (m_count = r.m_count)); Base::m_data = alloc<_alignment>(m_allocator, (Base::m_count = r.Base::m_count));
for(size_type i = 0; i < m_count; i++) for(size_type i = 0; i < Base::m_count; i++)
new (&m_data[i]) value_type(r.m_data[i]); new (&Base::m_data[i]) value_type(r.Base::m_data[i]);
} }
define_member_function(VOID, take_from, IN __ia__identifier&& r) define_member_function(VOID, take_from, IN __ia__identifier&& r)
{ {
ia_swap(m_data, r.m_data); ia_swap(Base::m_data, r.Base::m_data);
ia_swap(m_count, r.m_count); ia_swap(Base::m_count, r.Base::m_count);
ia_swap(m_allocator, r.m_allocator); ia_swap(m_allocator, r.m_allocator);
} }
} }
@ -106,121 +106,58 @@ namespace ia
define_const_member_function(__ia__identifier::const_iterator, cfind, IN comparator_type c, IN size_type offset) define_const_member_function(__ia__identifier::const_iterator, cfind, IN comparator_type c, IN size_type offset)
{ {
for(size_type i = offset; i < m_count; i++) for(size_type i = offset; i < Base::m_count; i++)
if(!c(m_data[i])) return { &m_data[i] }; if(!c(Base::m_data[i])) return { &Base::m_data[i] };
return end(); return end();
} }
define_const_member_function(__ia__identifier::const_iterator, crfind, IN comparator_type c, IN size_type offset) define_const_member_function(__ia__identifier::const_iterator, crfind, IN comparator_type c, IN size_type offset)
{ {
for(ssize_type i = m_count - 1; i >= offset; i--) for(ssize_type i = Base::m_count - 1; i >= offset; i--)
if(!c(m_data[i])) return { &m_data[i] }; if(!c(Base::m_data[i])) return { &Base::m_data[i] };
return end(); return end();
} }
define_const_member_function(template<typename compare_type> __ia__identifier::const_iterator, cfind, IN CONST compare_type& v, IN size_type offset) define_const_member_function(template<typename compare_type> __ia__identifier::const_iterator, cfind, IN CONST compare_type& v, IN size_type offset)
{ {
for(size_type i = offset; i < m_count; i++) for(size_type i = offset; i < Base::m_count; i++)
if(v == m_data[i]) return { &m_data[i] }; if(v == Base::m_data[i]) return { &Base::m_data[i] };
return end(); return end();
} }
define_const_member_function(template<typename compare_type> __ia__identifier::const_iterator, crfind, IN CONST compare_type& v, IN size_type offset) define_const_member_function(template<typename compare_type> __ia__identifier::const_iterator, crfind, IN CONST compare_type& v, IN size_type offset)
{ {
for(ssize_type i = m_count - 1; i >= offset; i--) for(ssize_type i = Base::m_count - 1; i >= offset; i--)
if(v == m_data[i]) return { &m_data[i] }; if(v == Base::m_data[i]) return { &Base::m_data[i] };
return end(); return end();
} }
define_const_member_function(BOOL, contains, IN comparator_type v) define_const_member_function(BOOL, contains, IN comparator_type v)
{ {
for(size_type i = 0; i < m_count; i++) for(size_type i = 0; i < Base::m_count; i++)
if(!c(m_data[i])) return true; if(!c(Base::m_data[i])) return true;
return false; return false;
} }
define_const_member_function(template<typename compare_type> BOOL, contains, IN CONST compare_type& v) define_const_member_function(template<typename compare_type> BOOL, contains, IN CONST compare_type& v)
{ {
for(size_type i = 0; i < m_count; i++) for(size_type i = 0; i < Base::m_count; i++)
if(v == m_data[i]) return true; if(v == Base::m_data[i]) return true;
return false; return false;
} }
define_const_member_function(BOOL, operator==, IN CONST FixedContainer& o) define_const_member_function(BOOL, operator==, IN CONST FixedContainer& o)
{ {
if(m_count != o.size()) return false; if(Base::m_count != o.size()) return false;
const auto data = o.data(); const auto data = o.data();
for(size_type i = 0; i < o.size(); i++) for(size_type i = 0; i < o.size(); i++)
if(m_data[i] != data[i]) return false; if(Base::m_data[i] != data[i]) return false;
return true; return true;
} }
define_const_member_function(UINT64, hash,) define_const_member_function(UINT64, hash,)
{ {
return IIHashable::fnv_1a((PCUINT8)m_data, m_count * sizeof(value_type)); return IIHashable::fnv_1a((PCUINT8)Base::m_data, Base::m_count * sizeof(value_type));
}
}
namespace ia
{
define_member_function(__ia__identifier::iterator, begin,)
{
return cbegin();
}
define_member_function(__ia__identifier::iterator, end,)
{
return cend();
}
define_member_function(__ia__identifier::reverse_iterator, rbegin,)
{
return crbegin();
}
define_member_function(__ia__identifier::reverse_iterator, rend,)
{
return crend();
}
define_const_member_function(__ia__identifier::const_iterator, begin,)
{
return cbegin();
}
define_const_member_function(__ia__identifier::const_iterator, end,)
{
return cend();
}
define_const_member_function(__ia__identifier::const_iterator, cbegin,)
{
return { m_data };
}
define_const_member_function(__ia__identifier::const_iterator, cend,)
{
return { m_data + m_count };
}
define_const_member_function(__ia__identifier::reverse_const_iterator, rbegin,)
{
return crbegin();
}
define_const_member_function(__ia__identifier::reverse_const_iterator, rend,)
{
return crend();
}
define_const_member_function(__ia__identifier::reverse_const_iterator, crbegin,)
{
return { m_data + m_count - 1 };
}
define_const_member_function(__ia__identifier::reverse_const_iterator, crend,)
{
return { m_data - 1 };
} }
} }

View File

@ -16,124 +16,160 @@
#pragma once #pragma once
#include <iacore/hashable.hpp>
#include <iacore/allocator.hpp> #include <iacore/allocator.hpp>
#include <iacore/memory.hpp> #include <iacore/memory.hpp>
#include "iterator.interface.inl" #include "span.interface.inl"
namespace ia namespace ia
{ {
template<typename _value_type, typename _allocator_type = GeneralAllocator<_value_type>, CONST SIZE_T _alignment = 1> template<typename _value_type, typename _allocator_type = GeneralAllocator<_value_type>,
CONST SIZE_T _alignment = 1>
requires valid_allocator_type<_allocator_type, _value_type> requires valid_allocator_type<_allocator_type, _value_type>
class FixedContainer: public IIHashable class FixedContainer : public IIHashable, public Span<_value_type>
{ {
using Base = Span<_value_type>;
public: public:
using size_type = SIZE_T; using typename Base::size_type;
using ssize_type = SSIZE_T; using typename Base::ssize_type;
using value_type = _value_type; using typename Base::value_type;
using buffer_type = value_type*; using typename Base::pointer_type;
using pointer_type = value_type*; using typename Base::const_pointer_type;
using reference_type = value_type&; using typename Base::buffer_type;
using typename Base::reference_type;
using typename Base::const_reference_type;
using typename Base::iterator;
using typename Base::const_iterator;
using typename Base::reverse_iterator;
using typename Base::reverse_const_iterator;
using typename Base::comparator_type;
using allocator_type = _allocator_type; using allocator_type = _allocator_type;
using const_pointer_type = CONST value_type*;
using const_reference_type = CONST value_type&;
using iterator = ContainerIterator<value_type>;
using const_iterator = ContainerConstIterator<value_type>;
using reverse_iterator = ContainerIterator<value_type, true>;
using reverse_const_iterator = ContainerConstIterator<value_type, true>;
using comparator_type = std::function<INT32(IN CONST value_type&)>;
STATIC CONSTEXPR size_type alignment = _alignment; STATIC CONSTEXPR size_type alignment = _alignment;
public: public:
FixedContainer(); FixedContainer();
FixedContainer(IN size_type count); FixedContainer(IN size_type count);
FixedContainer(IN initializer_list<value_type> v); FixedContainer(IN initializer_list<value_type> v);
FixedContainer(IN FixedContainer&& r) { take_from(IA_MOVE(r)); }
FixedContainer(IN CONST FixedContainer& r) { copy_from(r); } FixedContainer(IN FixedContainer &&r)
{
take_from(IA_MOVE(r));
}
FixedContainer(IN CONST FixedContainer &r)
{
copy_from(r);
}
~FixedContainer(); ~FixedContainer();
VOID operator=(IN FixedContainer&& r) { take_from(IA_MOVE(r)); } VOID operator=(IN FixedContainer &&r)
VOID operator=(IN CONST FixedContainer& r) { copy_from(r); } {
take_from(IA_MOVE(r));
}
VOID operator=(IN CONST FixedContainer &r)
{
copy_from(r);
}
public: public:
iterator begin(); value_type &front()
iterator end(); {
const_iterator begin() CONST; IA_ASSERT(Base::m_count > 0);
const_iterator end() CONST; return Base::m_data[0];
const_iterator cbegin() CONST; }
const_iterator cend() CONST;
reverse_iterator rbegin(); CONST value_type &front() CONST
reverse_iterator rend(); {
reverse_const_iterator rbegin() CONST; IA_ASSERT(Base::m_count > 0);
reverse_const_iterator rend() CONST; return Base::m_data[0];
reverse_const_iterator crbegin() CONST; }
reverse_const_iterator crend() CONST;
value_type &back()
{
IA_ASSERT(Base::m_count > 0);
return Base::m_data[Base::m_count - 1];
}
CONST value_type &back() CONST
{
IA_ASSERT(Base::m_count > 0);
return Base::m_data[Base::m_count - 1];
}
value_type &operator[](IN size_type index)
{
IA_ASSERT(index < Base::m_count);
return Base::m_data[index];
}
CONST value_type &operator[](IN size_type index) CONST
{
IA_ASSERT(index < Base::m_count);
return Base::m_data[index];
}
public: public:
value_type& front() { IA_ASSERT(m_count > 0); return m_data[0]; } template<typename compare_type> iterator find(IN CONST compare_type &v, IN size_type offset = 0);
CONST value_type& front() CONST { IA_ASSERT(m_count > 0); return m_data[0]; }
value_type& back() { IA_ASSERT(m_count > 0); return m_data[m_count - 1]; }
CONST value_type& back() CONST { IA_ASSERT(m_count > 0); return m_data[m_count - 1]; }
value_type& operator[](IN size_type index) { IA_ASSERT(index < m_count); return m_data[index]; }
CONST value_type& operator[](IN size_type index) CONST { IA_ASSERT(index < m_count); return m_data[index]; }
public:
template<typename compare_type>
iterator find(IN CONST compare_type& v, IN size_type offset = 0);
iterator find(IN comparator_type c, IN size_type offset = 0); iterator find(IN comparator_type c, IN size_type offset = 0);
template<typename compare_type> template<typename compare_type> iterator rfind(IN CONST compare_type &v, IN size_type offset = 0);
iterator rfind(IN CONST compare_type& v, IN size_type offset = 0);
iterator rfind(IN comparator_type c, IN size_type offset = 0); iterator rfind(IN comparator_type c, IN size_type offset = 0);
template<typename compare_type> template<typename compare_type> const_iterator find(IN CONST compare_type &v, IN size_type offset = 0) CONST
const_iterator find(IN CONST compare_type& v, IN size_type offset = 0) CONST { return cfind(v, offset); } {
const_iterator find(IN comparator_type c, IN size_type offset = 0) CONST { return cfind(c, offset); } return cfind(v, offset);
template<typename compare_type> }
const_iterator rfind(IN CONST compare_type& v, IN size_type offset = 0) CONST { return crfind(v, offset); }
const_iterator rfind(IN comparator_type c, IN size_type offset = 0) CONST { return crfind(c, offset); }
template<typename compare_type> const_iterator find(IN comparator_type c, IN size_type offset = 0) CONST
const_iterator cfind(IN CONST compare_type& v, IN size_type offset = 0) CONST; {
return cfind(c, offset);
}
template<typename compare_type> const_iterator rfind(IN CONST compare_type &v, IN size_type offset = 0) CONST
{
return crfind(v, offset);
}
const_iterator rfind(IN comparator_type c, IN size_type offset = 0) CONST
{
return crfind(c, offset);
}
template<typename compare_type> const_iterator cfind(IN CONST compare_type &v, IN size_type offset = 0) CONST;
const_iterator cfind(IN comparator_type c, IN size_type offset = 0) CONST; const_iterator cfind(IN comparator_type c, IN size_type offset = 0) CONST;
template<typename compare_type> template<typename compare_type> const_iterator crfind(IN CONST compare_type &v, IN size_type offset = 0) CONST;
const_iterator crfind(IN CONST compare_type& v, IN size_type offset = 0) CONST;
const_iterator crfind(IN comparator_type c, IN size_type offset = 0) CONST; const_iterator crfind(IN comparator_type c, IN size_type offset = 0) CONST;
template<typename compare_type> template<typename compare_type> BOOL contains(IN CONST compare_type &v) CONST;
BOOL contains(IN CONST compare_type& v) CONST;
BOOL contains(IN CONST comparator_type c) CONST; BOOL contains(IN CONST comparator_type c) CONST;
public: public:
UINT64 hash() CONST; UINT64 hash() CONST;
public: public:
BOOL operator==(IN CONST FixedContainer& o) CONST; BOOL operator==(IN CONST FixedContainer &o) CONST;
public: public:
pointer_type data() { return m_data; } allocator_type &allocator()
const_pointer_type CONST data() CONST { return m_data; } {
VIRTUAL size_type size() CONST { return m_count; } return m_allocator;
VIRTUAL BOOL empty() CONST { return !m_count; } }
allocator_type& allocator() { return m_allocator; } VOID allocator(IN CONST allocator_type &v)
VOID allocator(IN CONST allocator_type& v) { m_allocator = v; } {
m_allocator = v;
}
public: public:
VIRTUAL VOID reset(); VIRTUAL VOID reset();
VIRTUAL pointer_type take(); VIRTUAL pointer_type take();
VIRTUAL VOID take_from(IN FixedContainer&& r); VIRTUAL VOID take_from(IN FixedContainer &&r);
VIRTUAL VOID copy_from(IN CONST FixedContainer& r); VIRTUAL VOID copy_from(IN CONST FixedContainer &r);
protected: protected:
size_type m_count { 0 }; allocator_type m_allocator{};
buffer_type m_data { nullptr };
protected:
allocator_type m_allocator {};
}; };
} } // namespace ia

View File

@ -0,0 +1,84 @@
// IACore-OSS; The Core Library for All IA Open Source Projects
// Copyright (C) 2024 IAS (ias@iasoft.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include <iacore/allocator.hpp>
#include <iacore/hashable.hpp>
#include <iacore/memory.hpp>
#include "iterator.interface.inl"
namespace ia
{
template<typename _value_type> class Span
{
public:
using size_type = SIZE_T;
using ssize_type = SSIZE_T;
using value_type = _value_type;
using pointer_type = value_type *;
using buffer_type = value_type *;
using reference_type = value_type &;
using const_pointer_type = CONST value_type *;
using const_reference_type = CONST value_type &;
using iterator = ContainerIterator<value_type>;
using const_iterator = ContainerConstIterator<value_type>;
using reverse_iterator = ContainerIterator<value_type, true>;
using reverse_const_iterator = ContainerConstIterator<value_type, true>;
using comparator_type = std::function<INT32(IN CONST value_type &)>;
public:
iterator begin();
iterator end();
const_iterator begin() CONST;
const_iterator end() CONST;
const_iterator cbegin() CONST;
const_iterator cend() CONST;
reverse_iterator rbegin();
reverse_iterator rend();
reverse_const_iterator rbegin() CONST;
reverse_const_iterator rend() CONST;
reverse_const_iterator crbegin() CONST;
reverse_const_iterator crend() CONST;
public:
pointer_type data()
{
return m_data;
}
const_pointer_type CONST data() CONST
{
return m_data;
}
VIRTUAL size_type size() CONST
{
return m_count;
}
VIRTUAL BOOL empty() CONST
{
return !m_count;
}
protected:
size_type m_count{0};
pointer_type m_data{nullptr};
};
} // namespace ia

View File

@ -0,0 +1,92 @@
// IACore-OSS; The Core Library for All IA Open Source Projects
// Copyright (C) 2024 IAS (ias@iasoft.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include "interface/span.interface.inl"
#define __template template<typename _value_type>
#define __ia__identifier Span<_value_type>
#define define_member_function(return_type, name, ...) __template return_type __ia__identifier::name(__VA_ARGS__)
#define define_const_member_function(return_type, name, ...) __template return_type __ia__identifier::name(__VA_ARGS__) CONST
namespace ia
{
define_member_function(__ia__identifier::iterator, begin,)
{
return cbegin();
}
define_member_function(__ia__identifier::iterator, end,)
{
return cend();
}
define_member_function(__ia__identifier::reverse_iterator, rbegin,)
{
return crbegin();
}
define_member_function(__ia__identifier::reverse_iterator, rend,)
{
return crend();
}
define_const_member_function(__ia__identifier::const_iterator, begin,)
{
return cbegin();
}
define_const_member_function(__ia__identifier::const_iterator, end,)
{
return cend();
}
define_const_member_function(__ia__identifier::const_iterator, cbegin,)
{
return { m_data };
}
define_const_member_function(__ia__identifier::const_iterator, cend,)
{
return { m_data + m_count };
}
define_const_member_function(__ia__identifier::reverse_const_iterator, rbegin,)
{
return crbegin();
}
define_const_member_function(__ia__identifier::reverse_const_iterator, rend,)
{
return crend();
}
define_const_member_function(__ia__identifier::reverse_const_iterator, crbegin,)
{
return { m_data + m_count - 1 };
}
define_const_member_function(__ia__identifier::reverse_const_iterator, crend,)
{
return { m_data - 1 };
}
}
#undef __template
#undef __ia__identifier
#undef define_member_function
#undef define_const_member_function

View File

@ -16,6 +16,7 @@
#pragma once #pragma once
#include "iacore/container/span.inl"
#include "iacore/container/fixed.inl" #include "iacore/container/fixed.inl"
#include "iacore/container/dynamic.inl" #include "iacore/container/dynamic.inl"

View File

@ -0,0 +1,56 @@
// IACore-OSS; The Core Library for All IA Open Source Projects
// Copyright (C) 2024 IAS (ias@iasoft.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include "iacore/string.hpp"
namespace ia
{
class Logger
{
public:
template<typename... Args> STATIC VOID Info(Args... args)
{
StringStream ss;
UNUSED((ss << ... << args));
printf("\033[32m[INFO]: %s\033[39m\n", ss.str().c_str());
}
template<typename... Args> STATIC VOID Warn(Args... args)
{
StringStream ss;
UNUSED((ss << ... << args));
printf("\033[33m[WARN]: %s\033[39m\n", ss.str().c_str());
}
template<typename... Args> STATIC VOID Error(Args... args)
{
StringStream ss;
UNUSED((ss << ... << args));
printf("\033[31m[ERROR]: %s\033[39m\n", ss.str().c_str());
}
private:
Logger()
{
}
~Logger()
{
}
};
} // namespace ia

View File

@ -16,38 +16,8 @@
#pragma once #pragma once
#include "file.hpp" #include "iacore/container/span.inl"
namespace ia namespace ia
{ {
class IAML
{
public:
public:
IAML(IN CONST String& path) { ParseFromFile(path); }
~IAML() {}
private:
VOID ParseFromFile(IN CONST String& path);
VOID ParseFromMemory(IN CONST String& iaml);
};
}
namespace ia
{
VOID IAML::ParseFromFile(IN CONST String& path)
{
File f(path, File::OPEN_FLAG_READ);
String data;
data.resize(f.Size() + 1);
f.Read(data.data(), f.Size(), f.Size());
ParseFromMemory(data);
}
VOID IAML::ParseFromMemory(IN CONST String& iaml)
{
}
} }

View File

@ -1,7 +1,21 @@
#include <stdio.h> #include <iacore/log.hpp>
#include <iacore/file.hpp>
#include <iacore/vector.hpp>
using namespace ia;
VOID print(IN CONST Span<int>& s)
{
for(const auto& v: s)
Logger::Info(v);
}
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
Vector<int> v1{10, 20, 32};
print(v1);
return 0; return 0;
} }

View File

@ -0,0 +1,4 @@
Hello
W
o
rld