Skip to content

Commit

Permalink
[free_list] reserve(count) and assignment/more ctors
Browse files Browse the repository at this point in the history
  • Loading branch information
harrand committed Jan 15, 2024
1 parent f02aa2c commit e4658bc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/tz/core/data/free_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,15 @@ namespace tz
using iterator = free_list_iterator<T, C>;
friend class free_list_iterator<T, C>;
free_list() = default;
free_list(C&& container);
free_list& operator=(const free_list<T, C>& rhs);
free_list& operator=(free_list<T, C>&& rhs);
free_list& operator=(C&& container);

std::size_t size() const;
bool empty() const;
void clear();
void reserve(std::size_t count);

iterator begin();
iterator end();
Expand Down
35 changes: 35 additions & 0 deletions src/tz/core/data/free_list.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,35 @@

namespace tz
{
template<tz::nullable T, tz::container C>
free_list<T, C>::free_list(C&& container):
elements(container),
frees(){}

template<tz::nullable T, tz::container C>
free_list<T, C>& free_list<T, C>::operator=(const free_list<T, C>& rhs)
{
this->elements = rhs.container;
this->frees = rhs.frees;
return *this;
}

template<tz::nullable T, tz::container C>
free_list<T, C>& free_list<T, C>::operator=(free_list<T, C>&& rhs)
{
std::swap(this->elements, rhs.elements);
std::swap(this->frees, rhs.frees);
return *this;
}

template<tz::nullable T, tz::container C>
free_list<T, C>& free_list<T, C>::operator=(C&& container)
{
this->elements = container;
this->frees = {};
return *this;
}

template<tz::nullable T, tz::container C>
std::size_t free_list<T, C>::size() const
{
Expand All @@ -21,6 +50,12 @@ namespace tz
this->frees.clear();
}

template<tz::nullable T, tz::container C>
void free_list<T, C>::reserve(std::size_t count)
{
this->elements.reserve(count + this->frees.size());
}

template<tz::nullable T, tz::container C>
free_list<T, C>::iterator free_list<T, C>::begin()
{
Expand Down
2 changes: 1 addition & 1 deletion test/core/tz_container_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void free_list()
tz::assert(classes.size() == 2);

// iterators
ints = {};
ints = tz::free_list<nullable_int>{};
// fibonacci
// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610
int a = 0;
Expand Down

0 comments on commit e4658bc

Please sign in to comment.