Skip to content

Commit

Permalink
Simplify namespace vec, define all tools except main class Vector in …
Browse files Browse the repository at this point in the history
…amc::vec
  • Loading branch information
sjanel committed Sep 2, 2021
1 parent ba91804 commit d83b7cb
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 90 deletions.
8 changes: 5 additions & 3 deletions src/include/amc_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
#include "amc_type_traits.hpp"

namespace amc {
namespace alloc {

/**
* Adaptor that wraps a singleton class Alloc into a "basic" allocator.
* Alloc is expected to model the "basic" allocator concept and to provide
* a static instance() method to access singleton.
*
* A basic allocator is a class that provides methods:
* void * allocate(size_t n)
* void * reallocate(void *p, size_t oldSz, size_t newSz)
* void *allocate(size_t n)
* void *reallocate(void *p, size_t oldSz, size_t newSz)
* void deallocate(void *p, size_t n)
*/
template <typename Alloc>
Expand Down Expand Up @@ -161,6 +162,7 @@ struct SimpleAllocator {

void deallocate(void *p, size_t) { free(p); }
};
} // namespace alloc

/**
* Standard (STL conformant) allocator using std::allocator providing an extra 'reallocate' method.
Expand All @@ -169,5 +171,5 @@ struct SimpleAllocator {
* otherwise it will simply allocate the new block and relocate all elements into it.
*/
template <class T>
using allocator = BasicAllocatorWrapper<T, SimpleAllocator>;
using allocator = alloc::BasicAllocatorWrapper<T, alloc::SimpleAllocator>;
} // namespace amc
8 changes: 4 additions & 4 deletions src/include/amc_smallvector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,27 @@ inline T *Reallocate(Alloc &alloc, T *p, SizeType oldCapa, SizeType newCapa, Siz
alloc.deallocate(p, oldCapa);
return newPtr;
}
} // namespace vec

template <class T, class Alloc, class SizeType>
void SmallVectorBase<T, Alloc, SizeType>::grow(uintmax_t minSize, bool exact) {
SizeType newCapa;
if (isSmall()) {
SizeType oldCapa = _size == std::numeric_limits<SizeType>::max() ? _capa : _size;
newCapa = vec::SafeNextCapacity(oldCapa, minSize, exact);
newCapa = SafeNextCapacity(oldCapa, minSize, exact);
T *dynStorage = this->allocate(newCapa);
(void)amc::uninitialized_relocate_n(_storage.ptr(), _capa, dynStorage);
_storage.setDyn(dynStorage);
_size = _capa;
} else {
newCapa = vec::SafeNextCapacity(_capa, minSize, exact);
newCapa = SafeNextCapacity(_capa, minSize, exact);
_storage.setDyn(vec::Reallocate(static_cast<Alloc &>(*this), _storage.dyn(), _capa, newCapa, _size));
}
_capa = newCapa;
}

template <class T, class Alloc, class SizeType>
void StdVectorBase<T, Alloc, SizeType>::grow(uintmax_t minSize, bool exact) {
SizeType newCapa = vec::SafeNextCapacity(_capa, minSize, exact);
SizeType newCapa = SafeNextCapacity(_capa, minSize, exact);
_storage = vec::Reallocate(static_cast<Alloc &>(*this), _storage, _capa, newCapa, _size);
_capa = newCapa;
}
Expand Down Expand Up @@ -116,6 +115,7 @@ template <class T, class Alloc, class SizeType>
void SmallVectorBase<T, Alloc, SizeType>::freeStorage() {
this->deallocate(_storage.dyn(), _capa);
}
} // namespace vec

/**
* Vector optimized for *trivially relocatable types* and in 'Small' state (when its capacity is <= N).
Expand Down
Loading

0 comments on commit d83b7cb

Please sign in to comment.