From 435d6b7e7f16ed8f0bd7f4cf64d8612d1e6ac1ed Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 27 Mar 2021 15:37:54 +0100 Subject: [PATCH] Started work on String. --- 01_alapok/compile.sh | 3 +- 01_alapok/string.cpp | 140 +++++++++++++++++++++++++++++++++++++++++++ 01_alapok/string.h | 41 +++++++++++++ 3 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 01_alapok/string.cpp create mode 100644 01_alapok/string.h diff --git a/01_alapok/compile.sh b/01_alapok/compile.sh index 62b087e..f010055 100755 --- a/01_alapok/compile.sh +++ b/01_alapok/compile.sh @@ -13,6 +13,7 @@ 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 string.cpp -o obj/string.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 +g++ -o bin/program obj/vector2.o obj/vector3.o obj/rect2.o obj/int_vector.o obj/string.o obj/main.o diff --git a/01_alapok/string.cpp b/01_alapok/string.cpp new file mode 100644 index 0000000..c703df6 --- /dev/null +++ b/01_alapok/string.cpp @@ -0,0 +1,140 @@ +#include "string.h" + +void String::push_back(const int element) { + ensure_capacity(_size + 1); + + _data[_size++] = element; +} + +void String::pop_back() { + if (_size == 0) { + return; + } + + --_size; +} + +void String::remove(const int index) { + _data[index] = _data[_size - 1]; + + --_size; +} + +void String::erase(const int element) { + int index = find(element); + + if (index != -1) { + remove(index); + } +} + +void String::clear() { + _size = 0; +} + +bool String::empty() const { + return _size == 0; +} + +int String::get(const int index) { + return _data[index]; +} + +const int &String::get(const int index) const { + return _data[index]; +} + +void String::set(const int index, const int value) { + _data[index] = value; +} + +int String::size() const { + return _size; +} + +int String::capacity() const { + return _actual_size; +} + +void String::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 String::resize(const int s) { + ensure_capacity(s); + + _size = s; +} + +void String::append_array(const String &other) { + ensure_capacity(_size + other._size); + + for (int i = 0; i < other._size; ++i) { + _data[_size++] = other._data[i]; + } +} + +int String::find(const int val) const { + for (int i = 0; i < _size; ++i) { + if (_data[i] == val) { + return i; + } + } + + return -1; +} + +int *String::dataw() { + return _data; +} + +const int *String::data() const { + return _data; +} + +const int &String::operator[](const int index) const { + return _data[index]; +} + +int &String::operator[](const int index) { + return _data[index]; +} + +String::String() { + _data = nullptr; + _actual_size = 0; + _size = 0; + _grow_by = 100; +} + +String::String(int prealloc) { + _data = nullptr; + _actual_size = 0; + _size = 0; + _grow_by = 100; + + ensure_capacity(prealloc); +} + +String::String(int prealloc, int grow_by) { + _data = nullptr; + _actual_size = 0; + _size = 0; + _grow_by = grow_by; + + ensure_capacity(prealloc); +} diff --git a/01_alapok/string.h b/01_alapok/string.h new file mode 100644 index 0000000..b05fd37 --- /dev/null +++ b/01_alapok/string.h @@ -0,0 +1,41 @@ +#ifndef STRING_H +#define STRING_H + +class String { + +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 String &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); + + String(); + String(int prealloc); + String(int prealloc, int grow_by); + +private: + int *_data; + int _actual_size; + int _size; + int _grow_by; +}; + +#endif \ No newline at end of file