Started work on String.

This commit is contained in:
Relintai 2021-03-27 15:37:54 +01:00
parent c5794147b4
commit 435d6b7e7f
3 changed files with 183 additions and 1 deletions

View File

@ -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

140
01_alapok/string.cpp Normal file
View File

@ -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);
}

41
01_alapok/string.h Normal file
View File

@ -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