-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8260255
commit a7586a5
Showing
3 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
Copyright (c) 2022, Philip Deegan. | ||
All rights reserved. | ||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are | ||
met: | ||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above | ||
copyright notice, this list of conditions and the following disclaimer | ||
in the documentation and/or other materials provided with the | ||
distribution. | ||
* Neither the name of Philip Deegan nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
#ifndef _MKN_KUL_VECTOR_HPP_ | ||
#define _MKN_KUL_VECTOR_HPP_ | ||
|
||
#include "mkn/kul/alloc.hpp" | ||
|
||
#include <vector> | ||
|
||
namespace mkn::kul { | ||
|
||
template <typename T, typename A = void> // A ignored but there for std::vector interop | ||
using NonConstructingVector = std::vector<T, NonConstructingAllocator<T>>; | ||
|
||
template <typename T> | ||
std::vector<T, Allocator<T>>& as_super(std::vector<T, NonConstructingAllocator<T>>& v) { | ||
return *reinterpret_cast<std::vector<T, Allocator<T>>*>(&v); | ||
} | ||
|
||
} // namespace mkn::kul | ||
|
||
template <typename T, typename A0, typename A1 = void> | ||
bool operator==(std::vector<T, A0> const& v0, mkn::kul::NonConstructingVector<T, A1> const& v1) { | ||
if (v0.size() != v1.size()) return false; | ||
for (std::size_t i = 0; i < v0.size(); i++) | ||
if (v0[i] != v1[i]) return false; | ||
return true; | ||
} | ||
|
||
template <typename T, typename A0, typename A1 = void> | ||
bool operator==(mkn::kul::NonConstructingVector<T, A1> const& v0, std::vector<T, A0> const& v1) { | ||
return v1 == v0; | ||
} | ||
|
||
template <typename T, typename A0, typename A1 = void> | ||
bool operator!=(std::vector<T, A0> const& v0, mkn::kul::NonConstructingVector<T, A1> const& v1) { | ||
return !(v0 == v1); | ||
} | ||
|
||
template <typename T, typename A0, typename A1 = void> | ||
bool operator!=(mkn::kul::NonConstructingVector<T, A1> const& v0, std::vector<T, A0> const& v1) { | ||
return !(v0 == v1); | ||
} | ||
|
||
#endif /*_MKN_KUL_VECTOR_HPP_*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
|
||
|
||
#include "test_common.hpp" | ||
|
||
#include "mkn/kul/dbg.hpp" | ||
#include "mkn/kul/alloc.hpp" | ||
#include "mkn/kul/vector.hpp" | ||
|
||
#include <array> | ||
#include <cstddef> | ||
#include <stdexcept> | ||
|
||
template <std::size_t size = 8> | ||
struct S { | ||
S() { | ||
for (std::size_t i = 0; i < size; i++) vars[i] = i; | ||
} | ||
|
||
bool operator==(S const& that) const { | ||
for (std::size_t i = 0; i < size; i++) | ||
if (vars[i] != that.vars[i]) return false; | ||
|
||
return true; | ||
} | ||
|
||
std::array<std::size_t, size> vars; | ||
}; | ||
|
||
template <typename V> | ||
auto copy_construct(V const& v) { | ||
KUL_DBG_FUNC_ENTER; | ||
V out{v}; | ||
return out; | ||
} | ||
|
||
template <typename V> | ||
auto copy_operator_equal(V const& v) { | ||
KUL_DBG_FUNC_ENTER; | ||
V out; | ||
out.reserve(v.capacity()); | ||
out = v; | ||
return out; | ||
} | ||
|
||
template <typename V> | ||
auto copy_manual(V const& v) { | ||
KUL_DBG_FUNC_ENTER; | ||
V out; | ||
out.reserve(v.capacity()); | ||
out.resize(v.size()); | ||
std::copy(v.begin(), v.end(), out.begin()); | ||
return out; | ||
} | ||
|
||
template <typename V> | ||
auto make_vector(std::size_t const& size) { | ||
KUL_DBG_FUNC_ENTER; | ||
return V{size}; | ||
} | ||
template <typename V0, typename V1> | ||
auto make_vector_from(V1 const& v1) { | ||
KUL_DBG_FUNC_ENTER; | ||
V0 v(v1.size()); | ||
std::copy(v1.begin(), v1.end(), v.begin()); | ||
return v; | ||
} | ||
|
||
template <typename T> | ||
void do_compare() { | ||
constexpr static std::size_t N = 2e6; | ||
auto const std_vec = make_vector<std::vector<T>>(N); | ||
auto const no_construct_vec = make_vector_from<mkn::kul::NonConstructingVector<T>>(std_vec); | ||
if (std_vec != no_construct_vec) throw std::runtime_error("FAIL"); | ||
auto const v0 = copy_construct(std_vec); | ||
auto const v1 = copy_construct(no_construct_vec); | ||
auto const v2 = copy_manual(std_vec); | ||
auto const v3 = copy_manual(no_construct_vec); | ||
|
||
if (v0 != std_vec) throw std::runtime_error("FAIL 0"); | ||
if (v0 == v1) throw std::runtime_error("FAIL 1"); // :( | ||
if (v0 != v2) throw std::runtime_error("FAIL 2"); | ||
if (v0 != v3) throw std::runtime_error("FAIL 3"); | ||
} | ||
|
||
TEST(NoConstructAllocator, copies) { do_compare<S<8>>(); } |