diff --git a/practice_12/CMakeLists.txt b/practice_12/CMakeLists.txt new file mode 100644 index 0000000..76fa8cf --- /dev/null +++ b/practice_12/CMakeLists.txt @@ -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) + diff --git a/practice_12/build.sh b/practice_12/build.sh new file mode 100755 index 0000000..10c5860 --- /dev/null +++ b/practice_12/build.sh @@ -0,0 +1,10 @@ +#!/bin/bash +if ! [ -d build ]; then + echo "Creating build directory..." + mkdir build +fi + +cd build +cmake .. +make +exit diff --git a/practice_12/include/FancySafeArray.hpp b/practice_12/include/FancySafeArray.hpp new file mode 100644 index 0000000..7c7c222 --- /dev/null +++ b/practice_12/include/FancySafeArray.hpp @@ -0,0 +1,37 @@ +#ifndef FANCY_HPP +#define FANCY_HPP + +#include +#include + +template +class FancySafeArray: public SafeArray { + public: + FancySafeArray(size_t size): SafeArray(size) { }; + FancySafeArray(const FancySafeArray& 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 +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 diff --git a/practice_12/include/SafeArray.hpp b/practice_12/include/SafeArray.hpp new file mode 100644 index 0000000..76ae3f7 --- /dev/null +++ b/practice_12/include/SafeArray.hpp @@ -0,0 +1,77 @@ +#ifndef SAFE_ARRAY_HPP +#define SAFE_ARRAY_HPP + +#include +#include +#include + +template +class SafeArray { + public: + SafeArray(): size_(0) { } + + SafeArray(unsigned int size): + size_(size) + { + data = std::make_unique(size_); + } + + SafeArray(const SafeArray& ar): + size_(0) + { + size_ = ar.size(); + data.reset(new T[size_]); + for(unsigned int i = 0; i < size_; ++i) { + data[i] = ar[i]; + } + } + + SafeArray& operator=(const SafeArray& ar) { + size_ = ar.size(); + std::unique_ptr 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 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 data; +}; + +#endif + diff --git a/practice_12/main.cpp b/practice_12/main.cpp new file mode 100644 index 0000000..053d3cc --- /dev/null +++ b/practice_12/main.cpp @@ -0,0 +1,44 @@ +#include +#include +#include + +int main(void) { + SafeArray 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 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 array3 = array2; + array3.resize(10); + std::cout << "Array 3:" << std::endl; + for(int x: array3) { + std::cout << x << std::endl; + } + + SafeArray 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 new_string_array(string_array); + return 0; +}