From 6e358e75d35034032493d73dbc0a69a773853bf4 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 27 Mar 2021 15:32:28 +0100 Subject: [PATCH] IntVector, Vector. --- .vscode/settings.json | 6 +- 01_alapok.txt | 11 ++- 01_alapok/compile.sh | 13 ++- 01_alapok/int_vector.cpp | 140 +++++++++++++++++++++++++++ 01_alapok/int_vector.h | 41 ++++++++ 01_alapok/main.cpp | 30 +++++- 01_alapok/vector.h | 203 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 434 insertions(+), 10 deletions(-) create mode 100644 01_alapok/int_vector.cpp create mode 100644 01_alapok/int_vector.h create mode 100644 01_alapok/vector.h diff --git a/.vscode/settings.json b/.vscode/settings.json index 9e03165..727fc58 100755 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -51,6 +51,10 @@ "stdexcept": "cpp", "streambuf": "cpp", "typeinfo": "cpp", - "variant": "cpp" + "variant": "cpp", + "forward_list": "cpp", + "unordered_set": "cpp", + "bitset": "cpp", + "valarray": "cpp" } } \ No newline at end of file diff --git a/01_alapok.txt b/01_alapok.txt index c370ff9..fd7de91 100755 --- a/01_alapok.txt +++ b/01_alapok.txt @@ -3,10 +3,11 @@ rev 2: abs() képlet hozzáadva. rev 3: Hozzáadtam vector és float-os közötti szorzás operátorokat mindkét vektorhoz. rev 4: Vector length képlet hozzáadva. rev 5: Vector dot() képletben hiányzott egy .y -rev 6: lenght_squared -> length_squared +rev 6: lenght_squared -> length_squared. rev 7: Rect2 is_equal_approx paraméter típus kijavítva. -rev 8: Rect2 grow, shrink visszatérési érték javítva (Már nincs) -rev 9: intersects_include_borders typo javítva +rev 8: Rect2 grow, shrink visszatérési érték javítva (Már nincs). +rev 9: intersects_include_borders typo javítva. +rev 10: IntVectorba pop_back metódus hozzáadva. 1. Implementáld az alábbi 2ds matematikai vektor osztályt: @@ -169,6 +170,7 @@ return (b.x >= x) && (b.y >= y) && | class IntVector | |-----------------------------------------------------| | + push_back(element : int) | +| + pop_back() | -> utolsó elem törlése | + remove(index : int) | | + erase(element : int) | | + clear() | @@ -199,6 +201,9 @@ return (b.x >= x) && (b.y >= y) && 5. A 4. feladatban elékszített IntVektor osztályból csinálj egy templatekkel általánosított Vector osztályt. +Fontos! A templatelt tagfüggvényeket implementációját vector.h-ba kell rakni! Vagy bele az osztályba, vagy a vector.h aljára! +Enélkül undefined reference hibákat fog dobmi a fordító! + Eddíg pl: class IntVector { diff --git a/01_alapok/compile.sh b/01_alapok/compile.sh index 3e7f63f..62b087e 100755 --- a/01_alapok/compile.sh +++ b/01_alapok/compile.sh @@ -7,9 +7,12 @@ if [ ! -d "bin" ]; then mkdir bin fi -g++ -Wall -g -Iinclude -c main.cpp -o obj/main.o -g++ -Wall -g -Iinclude -c vector2.cpp -o obj/vector2.o -g++ -Wall -g -Iinclude -c vector3.cpp -o obj/vector3.o -g++ -Wall -g -Iinclude -c rect2.cpp -o obj/rect2.o -g++ -o bin/program obj/main.o obj/vector2.o obj/vector3.o obj/rect2.o +#-Iinclude + +g++ -Wall -g -c vector2.cpp -o obj/vector2.o +g++ -Wall -g -c vector3.cpp -o obj/vector3.o +g++ -Wall -g -c rect2.cpp -o obj/rect2.o +g++ -Wall -g -c int_vector.cpp -o obj/int_vector.o +g++ -Wall -g -c main.cpp -o obj/main.o +g++ -o bin/program obj/vector2.o obj/vector3.o obj/rect2.o obj/int_vector.o obj/main.o diff --git a/01_alapok/int_vector.cpp b/01_alapok/int_vector.cpp new file mode 100644 index 0000000..e609ef6 --- /dev/null +++ b/01_alapok/int_vector.cpp @@ -0,0 +1,140 @@ +#include "int_vector.h" + +void IntVector::push_back(const int element) { + ensure_capacity(_size + 1); + + _data[_size++] = element; +} + +void IntVector::pop_back() { + if (_size == 0) { + return; + } + + --_size; +} + +void IntVector::remove(const int index) { + _data[index] = _data[_size - 1]; + + --_size; +} + +void IntVector::erase(const int element) { + int index = find(element); + + if (index != -1) { + remove(index); + } +} + +void IntVector::clear() { + _size = 0; +} + +bool IntVector::empty() const { + return _size == 0; +} + +int IntVector::get(const int index) { + return _data[index]; +} + +const int &IntVector::get(const int index) const { + return _data[index]; +} + +void IntVector::set(const int index, const int value) { + _data[index] = value; +} + +int IntVector::size() const { + return _size; +} + +int IntVector::capacity() const { + return _actual_size; +} + +void IntVector::ensure_capacity(const int capacity) { + if (capacity <= _actual_size) { + return; + } + + int tsize = capacity + _grow_by; + + int *nd = new int[tsize]; + + for (int i = 0; i < _size; ++i) { + nd[i] = _data[i]; + } + + delete[] _data; + + _data = nd; +} + +void IntVector::resize(const int s) { + ensure_capacity(s); + + _size = s; +} + +void IntVector::append_array(const IntVector &other) { + ensure_capacity(_size + other._size); + + for (int i = 0; i < other._size; ++i) { + _data[_size++] = other._data[i]; + } +} + +int IntVector::find(const int val) const { + for (int i = 0; i < _size; ++i) { + if (_data[i] == val) { + return i; + } + } + + return -1; +} + +int *IntVector::dataw() { + return _data; +} + +const int *IntVector::data() const { + return _data; +} + +const int &IntVector::operator[](const int index) const { + return _data[index]; +} + +int &IntVector::operator[](const int index) { + return _data[index]; +} + +IntVector::IntVector() { + _data = nullptr; + _actual_size = 0; + _size = 0; + _grow_by = 100; +} + +IntVector::IntVector(int prealloc) { + _data = nullptr; + _actual_size = 0; + _size = 0; + _grow_by = 100; + + ensure_capacity(prealloc); +} + +IntVector::IntVector(int prealloc, int grow_by) { + _data = nullptr; + _actual_size = 0; + _size = 0; + _grow_by = grow_by; + + ensure_capacity(prealloc); +} diff --git a/01_alapok/int_vector.h b/01_alapok/int_vector.h new file mode 100644 index 0000000..246e818 --- /dev/null +++ b/01_alapok/int_vector.h @@ -0,0 +1,41 @@ +#ifndef IntVector_H +#define IntVector_H + +class IntVector { + +public: + void push_back(const int element); + void pop_back(); + void remove(const int index); + void erase(const int element); + void clear(); + bool empty() const; + int get(const int index); + const int &get(const int index) const; + void set(const int index, const int value); + + int size() const; + int capacity() const; + void ensure_capacity(const int capacity); + void resize(const int s); + void append_array(const IntVector &other); + int find(const int val) const; + + int *dataw(); + const int *data() const; + + const int &operator[](const int index) const; + int &operator[](const int index); + + IntVector(); + IntVector(int prealloc); + IntVector(int prealloc, int grow_by); + +private: + int *_data; + int _actual_size; + int _size; + int _grow_by; +}; + +#endif \ No newline at end of file diff --git a/01_alapok/main.cpp b/01_alapok/main.cpp index bd4c116..560bbee 100755 --- a/01_alapok/main.cpp +++ b/01_alapok/main.cpp @@ -1,7 +1,35 @@ -#include +#include + +#include "int_vector.h" +#include "vector.h" int main() { + + //iv + IntVector iv; + + iv.push_back(123); + iv.push_back(232); + + for (int i = 0; i < iv.size(); ++i) { + std::cout << iv[i] << std::endl; + } + + std::cout << std::endl; + + //v + Vector v; + + v.push_back(123); + v.push_back(232); + + for (int i = 0; i < v.size(); ++i) { + std::cout << v[i] << std::endl; + } + + std::cout << std::endl; + std::cout << "asd" << std::endl; return 0; } \ No newline at end of file diff --git a/01_alapok/vector.h b/01_alapok/vector.h new file mode 100644 index 0000000..d8371dc --- /dev/null +++ b/01_alapok/vector.h @@ -0,0 +1,203 @@ +#ifndef VECTOR_H +#define VECTOR_H + +template +class Vector { + +public: + void push_back(const T &element); + void pop_back(); + void remove(const int index); + void erase(const T &element); + void clear(); + bool empty() const; + T get(const int index); + const T &get(const int index) const; + void set(const int index, const T &value); + + int size() const; + int capacity() const; + void ensure_capacity(const int capacity); + void resize(const int s); + void append_array(const Vector &other); + int find(const T &val) const; + + int *dataw(); + const int *data() const; + + const T &operator[](const int index) const; + T &operator[](const int index); + + Vector(); + Vector(int prealloc); + Vector(int prealloc, int grow_by); + +private: + T *_data; + int _actual_size; + int _size; + int _grow_by; +}; + +template +void Vector::push_back(const T &element) { + ensure_capacity(_size + 1); + + _data[_size++] = element; +} + +template +void Vector::pop_back() { + if (_size == 0) { + return; + } + + --_size; +} + +template +void Vector::remove(const int index) { + _data[index] = _data[_size - 1]; + + --_size; +} + +template +void Vector::erase(const T &element) { + int index = find(element); + + if (index != -1) { + remove(index); + } +} + +template +void Vector::clear() { + _size = 0; +} + +template +bool Vector::empty() const { + return _size == 0; +} + +template +T Vector::get(const int index) { + return _data[index]; +} + +template +const T &Vector::get(const int index) const { + return _data[index]; +} + +template +void Vector::set(const int index, const T &value) { + _data[index] = value; +} + +template +int Vector::size() const { + return _size; +} + +template +int Vector::capacity() const { + return _actual_size; +} + +template +void Vector::ensure_capacity(const int capacity) { + if (capacity <= _actual_size) { + return; + } + + int tsize = capacity + _grow_by; + + T *nd = new T[tsize]; + + for (int i = 0; i < _size; ++i) { + nd[i] = _data[i]; + } + + delete[] _data; + + _data = nd; +} + +template +void Vector::resize(const int s) { + ensure_capacity(s); + + _size = s; +} + +template +void Vector::append_array(const Vector &other) { + ensure_capacity(_size + other._size); + + for (int i = 0; i < other._size; ++i) { + _data[_size++] = other._data[i]; + } +} + +template +int Vector::find(const T &val) const { + for (int i = 0; i < _size; ++i) { + if (_data[i] == val) { + return i; + } + } + + return -1; +} + +template +int *Vector::dataw() { + return _data; +} + +template +const int *Vector::data() const { + return _data; +} + +template +const T &Vector::operator[](const int index) const { + return _data[index]; +} + +template +T &Vector::operator[](const int index) { + return _data[index]; +} + +template +Vector::Vector() { + _data = nullptr; + _actual_size = 0; + _size = 0; + _grow_by = 100; +} + +template +Vector::Vector(int prealloc) { + _data = nullptr; + _actual_size = 0; + _size = 0; + _grow_by = 100; + + ensure_capacity(prealloc); +} + +template +Vector::Vector(int prealloc, int grow_by) { + _data = nullptr; + _actual_size = 0; + _size = 0; + _grow_by = grow_by; + + ensure_capacity(prealloc); +} + +#endif \ No newline at end of file