Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions practice_12/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.0)
project(practice_12)

add_compile_options(-g -Wall -std=c++17)

include_directories(include)

add_executable(main main.cpp)

10 changes: 10 additions & 0 deletions practice_12/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
if ! [ -d build ]; then
echo "Creating build directory..."
mkdir build
fi

cd build
cmake ..
make
exit
37 changes: 37 additions & 0 deletions practice_12/include/FancySafeArray.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef FANCY_HPP
#define FANCY_HPP

#include <iostream>
#include <SafeArray.hpp>

template<class T>
class FancySafeArray: public SafeArray {
public:
FancySafeArray(size_t size): SafeArray(size) { };
FancySafeArray(const FancySafeArray<T>& ar): SafeArray(ar) { };

FancySafeArray operator+(const FancySafeArray& rhs) const {
size_t new_size = size_ + rhs.size;
FancySafeArray new_array(new_size);
for (size_t i = 0; i < size_; ++i) {
new_array[i] = data[i];
}
for (size_t i = size_; i < rhs.size; ++i) {
new_array[i] = rhs[i];
}
return new_array;
}
};

template<class T>
std::ostream& operator<<(std::ostream& os, const FancySafeArray& fsa) {
os << "[";
for (size_t i = 0; i < fsa.getSize(); ++i) {
os << fsa[i];
if(!i%5) os << std::endl;
}
os << "]" << std::endl;
return os;
}

#endif
77 changes: 77 additions & 0 deletions practice_12/include/SafeArray.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#ifndef SAFE_ARRAY_HPP
#define SAFE_ARRAY_HPP

#include <memory>
#include <iostream>
#include <exception>

template<class T>
class SafeArray {
public:
SafeArray(): size_(0) { }

SafeArray(unsigned int size):
size_(size)
{
data = std::make_unique<T[]>(size_);
}

SafeArray(const SafeArray<T>& ar):
size_(0)
{
size_ = ar.size();
data.reset(new T[size_]);
for(unsigned int i = 0; i < size_; ++i) {
data[i] = ar[i];
}
}

SafeArray<T>& operator=(const SafeArray<T>& ar) {
size_ = ar.size();
std::unique_ptr<T[]> temp = std::move(data);
data.reset(new T[size_]);
for(unsigned int i = 0; i < size_; ++i) {
data[i] = ar[i];
}
return *this;
}

void insert(const T& value, size_t index) {
if(size_ < index) throw
data[index] = value;
}

void resize(size_t new_size) {
std::unique_ptr<T[]> temp(new T[new_size]{});
for(size_t i = 0; i < new_size; ++i) {
if(i >= size_) {
break;
}
temp[i] = data[i];
}
std::swap(temp, data);
size_ = new_size;
}

T* begin(void) { return data.get(); }
T* end(void) { return &data[size_]; };

size_t size(void) const { return size_; }

T& operator[](size_t index){
if (index > size_) throw std::out_of_range;
return data[index];
}

const T& operator[](size_t index) const {
if (index > size_) throw std::out_of_range;
return data[index];
}

private:
unsigned int size_;
std::unique_ptr<T[]> data;
};

#endif

44 changes: 44 additions & 0 deletions practice_12/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <SafeArray.hpp>
#include <iostream>
#include <string>

int main(void) {
SafeArray<int> array(10);
for(size_t i = 0; i < 10; ++i) {
array[i] = i * i;
}

std::cout << "Array 1:" << std::endl;
for(int x: array) {
std::cout << x << std::endl;
}

SafeArray<int> array2(array);
array2.resize(5);

std::cout << array2[7];
std::cout << "Array 2:" << std::endl;
for(int x: array2) {
std::cout << x << std::endl;
}

SafeArray<int> array3 = array2;
array3.resize(10);
std::cout << "Array 3:" << std::endl;
for(int x: array3) {
std::cout << x << std::endl;
}

SafeArray<std::string> string_array(10);
for(size_t i = 0; i < 10; ++i) {
string_array[i] = "The number: " + std::to_string(i);
}

std::cout << "String array:" << std::endl;
for(std::string s: string_array) {
std::cout << s << std::endl;
}

SafeArray<std::string> new_string_array(string_array);
return 0;
}