From a9213b33e9ad25a1151cd7742f0dad75fcd9a013 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20=C3=98stby?= Date: Fri, 13 Apr 2018 02:16:34 +0200 Subject: [PATCH 1/4] Outline of practice 12 --- practice_12/CMakeLists.txt | 9 ++++ practice_12/build.sh | 10 +++++ practice_12/include/SafeArray.hpp | 73 +++++++++++++++++++++++++++++++ practice_12/main.cpp | 26 +++++++++++ 4 files changed, 118 insertions(+) create mode 100644 practice_12/CMakeLists.txt create mode 100755 practice_12/build.sh create mode 100644 practice_12/include/SafeArray.hpp create mode 100644 practice_12/main.cpp 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/SafeArray.hpp b/practice_12/include/SafeArray.hpp new file mode 100644 index 0000000..eab7f60 --- /dev/null +++ b/practice_12/include/SafeArray.hpp @@ -0,0 +1,73 @@ +#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.reset(new T[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(); + data.reset(new T[size_]); + for(unsigned int i = 0; i < size_; ++i) { + data[i] = ar[i]; + } + //TODO Find out why this will not cast to a reference. + 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 = std::move(data); + data.reset(new T[new_size]); + for (unsigned int i = 0; i < size_; i++) { + data[i] = temp[i]; + if (i >= new_size) break; + } + 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){ + return data[index]; + } + + const T& operator[](size_t index) const { + 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..ef7d431 --- /dev/null +++ b/practice_12/main.cpp @@ -0,0 +1,26 @@ +#include +#include + +int main(void) { + SafeArray array(10); + for(size_t i = 0; i < 10; ++i) { + array[i] = 2 * i; + } + for(int x: array) { + std::cout << x << std::endl; + } + array.resize(5); + for(int x: array) { + std::cout << x << std::endl; + } + array.resize(100); + for(int x: array) { + std::cout << x << std::endl; + } + + SafeArray array2(array); + SafeArray array3; + array3 = array2; + + return 0; +} From 1245f85a000b371201a7cb9a0e3b1ebce58c30a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20=C3=98stby?= Date: Mon, 16 Apr 2018 22:19:39 +0200 Subject: [PATCH 2/4] Fixes standard initialization of the safe arrays --- practice_12/include/FancySafeArray.hpp | 15 ++++++++++++ practice_12/include/SafeArray.hpp | 16 ++++++------ practice_12/main.cpp | 34 ++++++++++++++++++++------ 3 files changed, 50 insertions(+), 15 deletions(-) create mode 100644 practice_12/include/FancySafeArray.hpp diff --git a/practice_12/include/FancySafeArray.hpp b/practice_12/include/FancySafeArray.hpp new file mode 100644 index 0000000..1a3e782 --- /dev/null +++ b/practice_12/include/FancySafeArray.hpp @@ -0,0 +1,15 @@ +#ifndef FANCY_HPP +#define FANCY_HPP + +#include + +template +class FancySafeArray { + public: + + private: + +}; + +#endif + diff --git a/practice_12/include/SafeArray.hpp b/practice_12/include/SafeArray.hpp index eab7f60..9b42afe 100644 --- a/practice_12/include/SafeArray.hpp +++ b/practice_12/include/SafeArray.hpp @@ -28,12 +28,12 @@ class SafeArray { 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]; } - //TODO Find out why this will not cast to a reference. - return this; + return *this; } void insert(const T& value, size_t index) { @@ -42,12 +42,14 @@ class SafeArray { } void resize(size_t new_size) { - std::unique_ptr temp = std::move(data); - data.reset(new T[new_size]); - for (unsigned int i = 0; i < size_; i++) { - data[i] = temp[i]; - if (i >= new_size) break; + 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; } diff --git a/practice_12/main.cpp b/practice_12/main.cpp index ef7d431..053d3cc 100644 --- a/practice_12/main.cpp +++ b/practice_12/main.cpp @@ -1,26 +1,44 @@ #include #include +#include int main(void) { SafeArray array(10); for(size_t i = 0; i < 10; ++i) { - array[i] = 2 * i; + array[i] = i * i; } + + std::cout << "Array 1:" << std::endl; for(int x: array) { std::cout << x << std::endl; } - array.resize(5); - for(int x: array) { + + 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; } - array.resize(100); - for(int x: array) { + + SafeArray array3 = array2; + array3.resize(10); + std::cout << "Array 3:" << std::endl; + for(int x: array3) { std::cout << x << std::endl; } - SafeArray array2(array); - SafeArray array3; - array3 = array2; + 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; } From 958a30fb00811925b2a8f1de5d5abf010221cc80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20=C3=98stby?= Date: Mon, 16 Apr 2018 22:21:10 +0200 Subject: [PATCH 3/4] stuff --- practice_12/include/FancySafeArray.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/practice_12/include/FancySafeArray.hpp b/practice_12/include/FancySafeArray.hpp index 1a3e782..d822713 100644 --- a/practice_12/include/FancySafeArray.hpp +++ b/practice_12/include/FancySafeArray.hpp @@ -4,9 +4,9 @@ #include template -class FancySafeArray { +class FancySafeArray: public SafeArray { public: - + FancySafeArray(size_t size): SafeArray(size) { }; private: }; From f12e260a58f766ba68ddd25f544c776d269ec771 Mon Sep 17 00:00:00 2001 From: Oskiboy Date: Mon, 16 Apr 2018 22:44:39 +0200 Subject: [PATCH 4/4] Adds exceptions and FancySafeArrays --- practice_12/include/FancySafeArray.hpp | 26 ++++++++++++++++++++++++-- practice_12/include/SafeArray.hpp | 4 +++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/practice_12/include/FancySafeArray.hpp b/practice_12/include/FancySafeArray.hpp index d822713..7c7c222 100644 --- a/practice_12/include/FancySafeArray.hpp +++ b/practice_12/include/FancySafeArray.hpp @@ -1,15 +1,37 @@ #ifndef FANCY_HPP #define FANCY_HPP +#include #include template class FancySafeArray: public SafeArray { public: FancySafeArray(size_t size): SafeArray(size) { }; - private: + 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; + } }; -#endif +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 index 9b42afe..76ae3f7 100644 --- a/practice_12/include/SafeArray.hpp +++ b/practice_12/include/SafeArray.hpp @@ -13,7 +13,7 @@ class SafeArray { SafeArray(unsigned int size): size_(size) { - data.reset(new T[size_]); + data = std::make_unique(size_); } SafeArray(const SafeArray& ar): @@ -59,10 +59,12 @@ class SafeArray { 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]; }