From 967be9e750221ab2ab783f95df79bb26d290a45e Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:07:58 +0200 Subject: add: added projects --- tiger-compiler/lib/misc/vector.hxx | 193 +++++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 tiger-compiler/lib/misc/vector.hxx (limited to 'tiger-compiler/lib/misc/vector.hxx') diff --git a/tiger-compiler/lib/misc/vector.hxx b/tiger-compiler/lib/misc/vector.hxx new file mode 100644 index 0000000..5c516cb --- /dev/null +++ b/tiger-compiler/lib/misc/vector.hxx @@ -0,0 +1,193 @@ +/** -*- C++ -*- + ** \file misc/vector.hxx + ** \brief Implementation of misc::vector. + */ + +#pragma once + +#include + +#include + +namespace misc +{ + template vector::vector() {} + + template vector::vector(const T& t) { this->push_back(t); } + + template vector::vector(const T& t1, const T& t2) + { + this->push_back(t1); + this->push_back(t2); + } + + template vector::vector(const T& t, const vector& v) + { + this->push_back(t); + this->insert(this->end(), v.begin(), v.end()); + } + + template vector::vector(std::initializer_list l) + { + this->insert(this->begin(), l.begin(), l.end()); + } + + template + template + vector::vector(InputIterator b, InputIterator e) + : super_type(b, e) + {} + + template + template + vector::vector(const C& c) + : super_type(c.begin(), c.end()) + {} + + template bool vector::has(const T& data) const + { + return std::ranges::find(*this, data) != this->end(); + } + + template + inline typename std::vector::iterator + vector::prepend(const vector& v) + { + auto it = this->insert(this->begin(), v.begin(), v.end()); + return it; + } + + template + inline typename std::vector::iterator + vector::prepend(const vector&& v) + { + auto it = this->insert(this->begin(), v.begin(), v.end()); + return it; + } + + template + inline typename std::vector::iterator vector::append(const vector& v) + { + auto it = this->insert(this->end(), v.begin(), v.end()); + return it; + } + + template + inline typename std::vector::iterator + vector::append(const vector&& v) + { + auto it = this->insert(this->end(), v.begin(), v.end()); + return it; + } + + template + inline vector vector::operator+(const vector& v) const + { + vector res = *this; + return res += v; + } + + template inline vector& vector::operator+=(const vector& v) + { + this->insert(this->end(), v.begin(), v.end()); + return *this; + } + + template + template + inline typename std::vector::iterator + vector::position_append(const vector& v, Iterator it) + { + auto new_it = this->insert(it, v.begin(), v.end()); + return new_it; + } + + template + template + inline typename std::vector::iterator + vector::position_append(const vector&& v, Iterator it) + { + auto new_it = this->insert(it, v.begin(), v.end()); + return new_it; + } + + template inline vector& vector::remove(const T& data) + { + precondition(has(data)); + this->erase(std::ranges::find(*this, data)); + return *this; + } + + template std::ostream& vector::dump(std::ostream& ostr) const + { + for (typename super_type::const_iterator i = this->begin(); + i != this->end(); ++i) + ostr << (i == this->begin() ? "" : ", ") << *i; + return ostr; + } + + template + std::ostream& operator<<(std::ostream& ostr, const vector& v) + { + return v.dump(ostr); + } + + template + auto append_and_move(std::vector& v1, std::vector&& v2) + { + auto it = v1.insert(v1.end(), make_move_iterator(v2.begin()), + make_move_iterator(v2.end())); + v2.clear(); + return it; + } + + template + auto append_and_move(std::vector& v1, std::vector& v2) + { + auto it = v1.insert(v1.end(), make_move_iterator(v2.begin()), + make_move_iterator(v2.end())); + v2.clear(); + return it; + } + + template + auto prepend_and_move(std::vector& v1, std::vector&& v2) + { + auto it = v1.insert(v1.begin(), make_move_iterator(v2.begin()), + make_move_iterator(v2.end())); + v2.clear(); + return it; + } + + template + auto prepend_and_move(std::vector& v1, std::vector& v2) + { + auto it = v1.insert(v1.begin(), make_move_iterator(v2.begin()), + make_move_iterator(v2.end())); + v2.clear(); + return it; + } + + template + auto position_append_and_move(std::vector& v1, + Iterator v1_iterator, + std::vector&& v2) + { + auto it = v1.insert(v1_iterator, make_move_iterator(v2.begin()), + make_move_iterator(v2.end())); + v2.clear(); + return it; + } + + template + auto position_append_and_move(std::vector& v1, + Iterator v1_iterator, + std::vector& v2) + { + auto it = v1.insert(v1_iterator, make_move_iterator(v2.begin()), + make_move_iterator(v2.end())); + v2.clear(); + return it; + } + +} // namespace misc -- cgit v1.2.3