vector 3
parent
8571a8034b
commit
392c32751d
|
@ -17,11 +17,13 @@
|
||||||
|
|
||||||
namespace blt
|
namespace blt
|
||||||
{
|
{
|
||||||
template<typename TYPE_ITR>
|
namespace itr
|
||||||
class enumerator
|
|
||||||
{
|
{
|
||||||
public:
|
template<typename TYPE_ITR, bool is_ptr = std::is_pointer_v<TYPE_ITR>>
|
||||||
class iterator
|
class iterator;
|
||||||
|
|
||||||
|
template<typename TYPE_ITR>
|
||||||
|
class iterator<TYPE_ITR, false>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using iterator_category = std::input_iterator_tag;
|
using iterator_category = std::input_iterator_tag;
|
||||||
|
@ -29,6 +31,7 @@ namespace blt
|
||||||
using difference_type = typename TYPE_ITR::difference_type;
|
using difference_type = typename TYPE_ITR::difference_type;
|
||||||
using pointer = typename TYPE_ITR::pointer;
|
using pointer = typename TYPE_ITR::pointer;
|
||||||
using reference = typename TYPE_ITR::reference;
|
using reference = typename TYPE_ITR::reference;
|
||||||
|
using const_reference = const typename TYPE_ITR::reference;
|
||||||
private:
|
private:
|
||||||
blt::size_t index = 0;
|
blt::size_t index = 0;
|
||||||
TYPE_ITR current;
|
TYPE_ITR current;
|
||||||
|
@ -53,7 +56,7 @@ namespace blt
|
||||||
return current != other.current;
|
return current != other.current;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<blt::size_t, const reference> operator*() const
|
std::pair<blt::size_t, const_reference> operator*() const
|
||||||
{
|
{
|
||||||
return {index, *current};
|
return {index, *current};
|
||||||
};
|
};
|
||||||
|
@ -64,22 +67,72 @@ namespace blt
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename TYPE_ITR>
|
||||||
|
class iterator<TYPE_ITR, true>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using iterator_category = std::input_iterator_tag;
|
||||||
|
using value_type = std::remove_pointer_t<TYPE_ITR>;
|
||||||
|
using difference_type = std::ptrdiff_t;
|
||||||
|
using pointer = TYPE_ITR;
|
||||||
|
using reference = std::remove_pointer_t<TYPE_ITR>&;
|
||||||
|
using const_reference = const std::remove_pointer_t<TYPE_ITR>&;
|
||||||
|
private:
|
||||||
|
blt::size_t index = 0;
|
||||||
|
TYPE_ITR current;
|
||||||
|
public:
|
||||||
|
explicit iterator(TYPE_ITR current): current(std::move(current))
|
||||||
|
{}
|
||||||
|
|
||||||
|
iterator& operator++()
|
||||||
|
{
|
||||||
|
++index;
|
||||||
|
++current;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==(iterator other) const
|
||||||
|
{
|
||||||
|
return current == other.current;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator!=(iterator other) const
|
||||||
|
{
|
||||||
|
return current != other.current;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::pair<blt::size_t, const_reference> operator*() const
|
||||||
|
{
|
||||||
|
return {index, *current};
|
||||||
|
};
|
||||||
|
|
||||||
|
std::pair<blt::size_t, reference> operator*()
|
||||||
|
{
|
||||||
|
return {index, *current};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TYPE_ITR>
|
||||||
|
class enumerator
|
||||||
|
{
|
||||||
|
public:
|
||||||
explicit enumerator(TYPE_ITR begin, TYPE_ITR end): begin_(std::move(begin)), end_(std::move(end))
|
explicit enumerator(TYPE_ITR begin, TYPE_ITR end): begin_(std::move(begin)), end_(std::move(end))
|
||||||
{}
|
{}
|
||||||
|
|
||||||
iterator begin()
|
itr::iterator<TYPE_ITR> begin()
|
||||||
{
|
{
|
||||||
return begin_;
|
return begin_;
|
||||||
}
|
}
|
||||||
|
|
||||||
iterator end()
|
itr::iterator<TYPE_ITR> end()
|
||||||
{
|
{
|
||||||
return end_;
|
return end_;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
iterator begin_;
|
itr::iterator<TYPE_ITR> begin_;
|
||||||
iterator end_;
|
itr::iterator<TYPE_ITR> end_;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
@ -337,11 +390,13 @@ namespace blt
|
||||||
constexpr span(R&& range): size_(std::size(range)), data_(std::data(range))
|
constexpr span(R&& range): size_(std::size(range)), data_(std::data(range))
|
||||||
{}
|
{}
|
||||||
|
|
||||||
template<size_type SIZE, typename std::enable_if_t<extent != dynamic_extent && SIZE == extent && std::is_const_v<element_type>, bool> = true>
|
template<size_type SIZE, typename std::enable_if_t<
|
||||||
|
extent != dynamic_extent && SIZE == extent && std::is_const_v<element_type>, bool> = true>
|
||||||
explicit constexpr span(std::initializer_list<value_type> il) noexcept: size_(il.size()), data_(&il.begin())
|
explicit constexpr span(std::initializer_list<value_type> il) noexcept: size_(il.size()), data_(&il.begin())
|
||||||
{}
|
{}
|
||||||
|
|
||||||
template<size_type SIZE, typename std::enable_if_t<extent == dynamic_extent && SIZE == extent && std::is_const_v<element_type>, bool> = true>
|
template<size_type SIZE, typename std::enable_if_t<
|
||||||
|
extent == dynamic_extent && SIZE == extent && std::is_const_v<element_type>, bool> = true>
|
||||||
explicit span(std::initializer_list<value_type> il) noexcept: size_(il.size()), data_(&il.begin())
|
explicit span(std::initializer_list<value_type> il) noexcept: size_(il.size()), data_(&il.begin())
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
|
|
@ -324,53 +324,55 @@ namespace blt
|
||||||
template<typename G, std::enable_if_t<std::is_convertible_v<G, T>, bool> = true>
|
template<typename G, std::enable_if_t<std::is_convertible_v<G, T>, bool> = true>
|
||||||
constexpr iterator insert(const_iterator pos, G&& ref)
|
constexpr iterator insert(const_iterator pos, G&& ref)
|
||||||
{
|
{
|
||||||
difference_type loc = *pos - buffer_;
|
difference_type loc = pos - buffer_;
|
||||||
if (size_ + 1 >= capacity_)
|
if (size_ + 1 >= capacity_)
|
||||||
expand();
|
expand();
|
||||||
for (auto insert = end() - 1; (insert - buffer_) != loc; insert--)
|
for (auto insert = end() - 1; (insert - buffer_) != loc - 1; insert--)
|
||||||
{
|
{
|
||||||
auto new_pos = insert + 1;
|
auto new_pos = insert + 1;
|
||||||
*new_pos = *insert;
|
*new_pos = *insert;
|
||||||
}
|
}
|
||||||
buffer_[loc] = std::forward(ref);
|
buffer_[loc] = ref;
|
||||||
size_++;
|
size_++;
|
||||||
return &buffer_[loc];
|
return buffer_ + loc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
constexpr iterator erase(const_iterator pos)
|
constexpr iterator erase(const_iterator pos)
|
||||||
{
|
{
|
||||||
difference_type loc = *pos - buffer_;
|
difference_type loc = pos - buffer_;
|
||||||
|
|
||||||
for (auto fetch = pos + 1; fetch != cend(); fetch++)
|
for (auto fetch = begin() + loc + 1; fetch != end(); fetch++)
|
||||||
{
|
{
|
||||||
auto insert = fetch - 1;
|
auto insert = fetch - 1;
|
||||||
*insert = *fetch;
|
*insert = *fetch;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_--;
|
size_--;
|
||||||
return &buffer_[loc + 1];
|
return buffer_ + loc + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr iterator erase(const_iterator first, const_iterator last)
|
constexpr iterator erase(const_iterator first, const_iterator last)
|
||||||
{
|
{
|
||||||
difference_type loc = *first - buffer_;
|
difference_type first_pos = first - buffer_;
|
||||||
|
difference_type last_pos = last - buffer_;
|
||||||
|
difference_type remove_amount = last_pos - first_pos;
|
||||||
|
|
||||||
for (auto fetch = last, insert = first; fetch != cend(); fetch++, insert++)
|
for (auto fetch = begin() + last_pos, insert = begin() + first_pos; fetch != end(); fetch++, insert++)
|
||||||
{
|
{
|
||||||
*insert = fetch;
|
*insert = *fetch;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_ = loc;
|
size_ -= remove_amount;
|
||||||
return &buffer_[loc + 1];
|
return buffer_ + first_pos + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr inline iterator begin() noexcept
|
constexpr inline iterator begin() const noexcept
|
||||||
{
|
{
|
||||||
return data();
|
return data();
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr inline iterator end() noexcept
|
constexpr inline iterator end() const noexcept
|
||||||
{
|
{
|
||||||
return data() + size();
|
return data() + size();
|
||||||
}
|
}
|
||||||
|
@ -385,12 +387,12 @@ namespace blt
|
||||||
return data() + size();
|
return data() + size();
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr inline reverse_iterator rbegin() noexcept
|
constexpr inline reverse_iterator rbegin() const noexcept
|
||||||
{
|
{
|
||||||
return reverse_iterator{end()};
|
return reverse_iterator{end()};
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr inline reverse_iterator rend() noexcept
|
constexpr inline reverse_iterator rend() const noexcept
|
||||||
{
|
{
|
||||||
return reverse_iterator{begin()};
|
return reverse_iterator{begin()};
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,6 +35,8 @@ namespace blt::test
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void vector_run();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //BLT_BLT_TESTS_H
|
#endif //BLT_BLT_TESTS_H
|
||||||
|
|
|
@ -0,0 +1,79 @@
|
||||||
|
/*
|
||||||
|
* <Short Description>
|
||||||
|
* Copyright (C) 2024 Brett Terpstra
|
||||||
|
*
|
||||||
|
* 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <blt_tests.h>
|
||||||
|
#include <blt/std/vector.h>
|
||||||
|
#include <blt/std/logging.h>
|
||||||
|
#include "blt/std/assert.h"
|
||||||
|
|
||||||
|
namespace blt::test
|
||||||
|
{
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void print(const T& ref)
|
||||||
|
{
|
||||||
|
BLT_TRACE_STREAM << "(" << ref.size() << ") [";
|
||||||
|
for (auto v : blt::enumerate(ref))
|
||||||
|
BLT_TRACE_STREAM << v.second << ((v.first != ref.size()-1) ? ", " : "]\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void vector_run()
|
||||||
|
{
|
||||||
|
|
||||||
|
blt::vector<int> vec;
|
||||||
|
|
||||||
|
vec.push_back(10);
|
||||||
|
vec.push_back(20);
|
||||||
|
vec.push_back(30);
|
||||||
|
vec.push_back(40);
|
||||||
|
vec.push_back(50);
|
||||||
|
vec.push_back(60);
|
||||||
|
vec.push_back(70);
|
||||||
|
vec.push_back(80);
|
||||||
|
vec.push_back(90);
|
||||||
|
|
||||||
|
print(vec);
|
||||||
|
BLT_ASSERT(vec.size() == 9 && "Failed at push_back");
|
||||||
|
|
||||||
|
vec.insert(vec.cbegin() + 2, 25);
|
||||||
|
BLT_ASSERT(vec.size() == 10 && "Failed at insert single");
|
||||||
|
|
||||||
|
print(vec);
|
||||||
|
|
||||||
|
for (int i = 0; i < 128; i++)
|
||||||
|
vec.insert(vec.begin() + 2, i);
|
||||||
|
BLT_ASSERT(vec.size() == 138 && "Failed at insert 128");
|
||||||
|
|
||||||
|
print(vec);
|
||||||
|
|
||||||
|
vec.erase(vec.begin() + 3, vec.begin() + 8);
|
||||||
|
BLT_ASSERT(vec.size() == 133 && "Failed at erase range (non end)");
|
||||||
|
|
||||||
|
print(vec);
|
||||||
|
|
||||||
|
vec.erase(vec.begin() + 5);
|
||||||
|
|
||||||
|
print(vec);
|
||||||
|
BLT_ASSERT(vec.size() == 132 && "Failed at erase single");
|
||||||
|
|
||||||
|
vec.erase(vec.begin() + 10, vec.end());
|
||||||
|
|
||||||
|
print(vec);
|
||||||
|
BLT_ASSERT(vec.size() == 10 && "Failed at erase range (end)");
|
||||||
|
}
|
||||||
|
}
|
|
@ -84,6 +84,7 @@ int main(int argc, const char** argv)
|
||||||
.setNArgs('?').build());
|
.setNArgs('?').build());
|
||||||
parser.addArgument(blt::arg_builder("--utility").setHelp("Run tests on utility functions").setAction(blt::arg_action_t::STORE_TRUE).build());
|
parser.addArgument(blt::arg_builder("--utility").setHelp("Run tests on utility functions").setAction(blt::arg_action_t::STORE_TRUE).build());
|
||||||
parser.addArgument(blt::arg_builder("--data").setHelp("Run tests on data functions").setAction(blt::arg_action_t::STORE_TRUE).build());
|
parser.addArgument(blt::arg_builder("--data").setHelp("Run tests on data functions").setAction(blt::arg_action_t::STORE_TRUE).build());
|
||||||
|
parser.addArgument(blt::arg_builder("--vector").setHelp("Run tests for the vectors").setAction(blt::arg_action_t::STORE_TRUE).build());
|
||||||
|
|
||||||
auto args = parser.parse_args(argc, argv);
|
auto args = parser.parse_args(argc, argv);
|
||||||
|
|
||||||
|
@ -103,6 +104,9 @@ int main(int argc, const char** argv)
|
||||||
if (args.contains("--data"))
|
if (args.contains("--data"))
|
||||||
blt::test::data::run();
|
blt::test::data::run();
|
||||||
|
|
||||||
|
if (args.contains("--vector"))
|
||||||
|
blt::test::vector_run();
|
||||||
|
|
||||||
if (args.contains("--nbt"))
|
if (args.contains("--nbt"))
|
||||||
{
|
{
|
||||||
auto v = blt::arg_parse::get<std::string>(args["nbt"]);
|
auto v = blt::arg_parse::get<std::string>(args["nbt"]);
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
#include <blt/std/random.h>
|
#include <blt/std/random.h>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include "blt/std/utility.h"
|
#include "blt/std/utility.h"
|
||||||
|
#include "blt/std/vector.h"
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include <blt/compatibility.h>
|
#include <blt/compatibility.h>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue