Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added fixed_vector #34

Merged
merged 41 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
ad7c507
Added fixed_vector and first tests
Daddelhai Sep 10, 2020
2d45dde
Merge branch 'master' into Issue31
Daddelhai Sep 11, 2020
76c0a91
Capacity as argument, not as template
Daddelhai Sep 11, 2020
45edff9
Bug Fix
Daddelhai Sep 11, 2020
f2f5216
Buf fix
Daddelhai Sep 14, 2020
8e50565
Final Fix
Daddelhai Sep 14, 2020
3750282
Update include/nitro/lang/fixed_vector.hpp
Daddelhai Sep 28, 2020
c390b09
Update include/nitro/lang/fixed_vector.hpp
Daddelhai Sep 28, 2020
6b2d765
Update include/nitro/lang/fixed_vector.hpp
Daddelhai Sep 28, 2020
8e8106f
Update include/nitro/lang/fixed_vector.hpp
Daddelhai Sep 28, 2020
57b8ad5
Merge branch 'Issue31' of https://github.com/Daddelhai/nitro into Iss…
Daddelhai Sep 28, 2020
42e8d3d
Update include/nitro/lang/fixed_vector.hpp
Daddelhai Sep 28, 2020
5d2b502
Update
Daddelhai Sep 28, 2020
ca791bf
Merge branch 'Issue31' of https://github.com/Daddelhai/nitro into Iss…
Daddelhai Sep 28, 2020
37ba9ed
Added Move Constructor
Daddelhai Sep 28, 2020
8678d70
Update
Daddelhai Sep 28, 2020
465e7e3
Final Fix
Daddelhai Sep 28, 2020
b9fc334
Fix
Daddelhai Oct 1, 2020
7fba477
Fix
Daddelhai Oct 1, 2020
0b83064
Bug Fix
Daddelhai Oct 1, 2020
bcb4249
Update
Daddelhai Oct 1, 2020
ea8b146
Fix
Daddelhai Oct 1, 2020
0d2fed8
Fix
Daddelhai Oct 1, 2020
5572cd5
Added operator= and initializer_list
Daddelhai Jan 4, 2021
e49334d
Build fix for macOS and Clang
Daddelhai Jan 4, 2021
a48d64a
Code Formatting
Daddelhai Jan 4, 2021
3efd5bf
constexpr (requires Cppstd 20)
Daddelhai Jan 5, 2021
af50e9a
Formatted
Daddelhai Jan 5, 2021
ebb0b6d
More constexpr
Daddelhai Jan 5, 2021
41ef99e
Added types
Daddelhai Jan 25, 2021
a494721
Code Formatting
Daddelhai Jan 25, 2021
ad35606
Added usings
Daddelhai Jan 26, 2021
9220b1c
Added test for init list
Daddelhai Jan 26, 2021
9356b7d
Added more constexpr
Daddelhai Jan 26, 2021
41ae632
Formatted test file
Daddelhai May 19, 2021
2d2304f
Added Tests for copy and move
Daddelhai May 19, 2021
6638af5
Replaced raw types with typedefs
Daddelhai May 19, 2021
229da45
Fix
Daddelhai May 19, 2021
70eabc3
removed move
Daddelhai May 21, 2021
68d7d97
deleted CATCH_CONFIG_MAIN
Daddelhai May 21, 2021
baeb8f9
Merge branch 'master' of https://github.com/tud-zih-energy/nitro into…
Daddelhai May 21, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@

build
.clang_complete
.vscode
cmake_build_debug
Testing
Testing
378 changes: 378 additions & 0 deletions include/nitro/lang/fixed_vector.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,378 @@
/*
* Copyright (c) 2015-2020, Technische Universität Dresden, Germany
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions
* and the following disclaimer.
*
* 2. 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.
*
* 3. Neither the name of the copyright holder 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 HOLDER 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.
*/

#pragma once

#include <nitro/except/raise.hpp>

#include <array>
#include <type_traits>

namespace nitro
{
namespace lang
{
template <typename T>
class fixed_vector
{
public:
bmario marked this conversation as resolved.
Show resolved Hide resolved
using value_type = T;
using pointer = value_type*;
using const_pointer = const value_type*;
using reference = value_type&;
using const_reference = const value_type&;
using size_type = std::size_t;
using iterator = value_type*;
using const_iterator = const value_type*;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
Daddelhai marked this conversation as resolved.
Show resolved Hide resolved

constexpr fixed_vector(std::size_t capacity)
: capacity_(capacity), data_(std::make_unique<value_type[]>(capacity))
{
}

template <typename Iterabel>
constexpr fixed_vector(std::size_t capacity, const Iterabel& array)
: capacity_(capacity), data_(std::make_unique<value_type[]>(capacity))
{
insert(this->begin(), array.begin(), array.end());
}

constexpr fixed_vector(const std::initializer_list<value_type>& list)
: capacity_(list.size()), data_(std::make_unique<value_type[]>(list.size()))
{
Daddelhai marked this conversation as resolved.
Show resolved Hide resolved
insert(this->begin(), list.begin(), list.end());
}

constexpr fixed_vector(const fixed_vector<value_type>& v) : fixed_vector(v.capacity_, v)
{
}

constexpr fixed_vector(fixed_vector<value_type>&& v)
: capacity_(v.capacity_), data_(std::move(v.data_))
{
}

constexpr fixed_vector operator=(const fixed_vector& v)
{
return fixed_vector(v);
}

constexpr fixed_vector operator=(fixed_vector&& v)
{
return fixed_vector(std::move(v));
}

Daddelhai marked this conversation as resolved.
Show resolved Hide resolved
constexpr fixed_vector operator=(const std::initializer_list<value_type>& l)
{
return fixed_vector(l.size(), l);
}

~fixed_vector() = default;

constexpr bool empty() const
{
return size_ == 0;
}

constexpr std::size_t size() const
{
return size_;
}

constexpr std::size_t capacity() const
{
return capacity_;
}

constexpr value_type& operator[](const std::size_t& key)
{
return data_[key];
}

constexpr value_type& at(const std::size_t& key)
{
if (key >= capacity_)
raise("Key is larger than capacity");

if (key > size_)
raise("Key is larger than size, use append() instead");

return data_[key];
}

constexpr const value_type& operator[](const std::size_t& key) const
{
return data_[key];
}

constexpr const value_type& at(const std::size_t& key) const
{
if (key >= capacity_)
raise("Key is larger than capacity!");

if (key > size_)
raise("Key is larger than size, use emplace_back() instead!");

return data_[key];
}

constexpr value_type& front() noexcept
{
return data_[0];
}

constexpr const value_type& front() const noexcept
{
return data_[0];
}

constexpr value_type& back() noexcept
{
return data_[size_ - 1];
}

constexpr const value_type& back() const noexcept
{
return data_[size_ - 1];
}

template <class... Args>
constexpr void emplace(value_type* const pos, Args&&... args)
{
auto key = std::distance(begin(), pos);

if (size_ >= capacity_)
raise("No capacity left!");

replace(data_[key], value_type(args...));
++size_;
}

template <class... Args>
constexpr std::size_t emplace_back(Args&&... args)
{
if (size_ >= capacity_)
raise("No capacity left!");

replace(data_[size_], value_type(std::forward<Args>(args)...));
++size_;

return size_ - 1;
}

constexpr std::size_t insert(const value_type& value)
{
if (size_ >= capacity_)
raise("No capacity left!");

replace(data_[size_], value);
++size_;

return size_ - 1;
}

constexpr std::size_t insert(value_type&& value)
{
if (size_ >= capacity_)
raise("No capacity left!");

replace(data_[size_], std::move(value));
++size_;

return size_ - 1;
}

template <typename Iter>
constexpr void insert(value_type* const pos, Iter start, Iter end)
{
std::size_t key = std::distance(begin(), pos);
if (key > size_)
raise("Key larger than size!");

while (start != end)
{
if (key >= capacity_)
raise("No capacity left!");

data_[key] = *start;

if (key == size_)
++size_;

++key;
++start;
}
}

constexpr void insert(value_type* const pos, std::initializer_list<value_type>& list)
{
insert(pos, list.begin(), list.end());
}

constexpr std::size_t push_back(const value_type& value)
{
if (size_ >= capacity_)
raise("No capacity left!");

data_[size_] = value;
++size_;

return size_ - 1;
}

template <typename Iter>
constexpr void push_back(Iter start, Iter end)
{
insert(this->end(), start, end);
}

constexpr void pop_back()
{
if (size_ == 0)
raise("Container is empty!");

--size_;
}

constexpr value_type* begin() noexcept
{
return &data_[0];
}

constexpr value_type* rbegin() noexcept
{
return &data_[size_ - 1];
}

constexpr value_type* end() noexcept
{
return &data_[size_];
}

constexpr value_type* rend() noexcept
{
return &data_[-1];
Daddelhai marked this conversation as resolved.
Show resolved Hide resolved
}

constexpr const value_type* begin() const noexcept
{
return &data_[0];
}

constexpr const value_type* rbegin() const noexcept
{
return &data_[size_ - 1];
}

constexpr const value_type* end() const noexcept
{
return &data_[size_];
}

constexpr const value_type* rend() const noexcept
{
return &data_[-1];
}

constexpr const value_type* cbegin() const noexcept
{
return &data_[0];
}

constexpr const value_type* crbegin() const noexcept
{
return &data_[size_ - 1];
}

constexpr const value_type* cend() const noexcept
{
return &data_[size_];
}

constexpr const value_type* crend() const noexcept
{
return &data_[-1];
}

constexpr void erase(value_type* pos)
{
std::size_t key = std::distance(begin(), pos);

if (key >= size_)
raise("Key does not exsist!");

while (key + 1 < size_ && key + 1 < capacity_)
{
replace(data_[key], std::forward<value_type>(data_[key + 1]));
++key;
}
--size_;
}

Daddelhai marked this conversation as resolved.
Show resolved Hide resolved
constexpr value_type* data() noexcept
{
return data_.get();
}

constexpr const value_type* data() const noexcept
{
return data_.get();
}

private:
std::size_t size_ = 0;
std::size_t capacity_ = 0;

std::unique_ptr<value_type[]> data_;

template <typename A>
constexpr std::enable_if_t<std::is_move_assignable<A>::value> replace(A& a, A&& b)
{
a = std::move(b);
}

template <typename A>
constexpr std::enable_if_t<!std::is_move_assignable<A>::value> replace(A& a, const A& b)
{
a = b;
}
};

} // namespace lang
} // namespace nitro

namespace std
{
template <size_t I, typename T>
T& get(nitro::lang::fixed_vector<T>& c) noexcept
{
return c.at(I);
}
} // namespace std
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@ NitroTest(is_callable_test.cpp)
NitroTest(format_test.cpp)

NitroTest(string_test.cpp)

NitroTest(fixed_vector_test.cpp)
Loading