From c6e0940d96d667f0a58c0c8ee692008840840067 Mon Sep 17 00:00:00 2001 From: Jankatay <162386918+Jankatay@users.noreply.github.com> Date: Sat, 6 Jul 2024 22:05:07 -0700 Subject: [PATCH] Create arrayDimensional.h A really cool header file to help with 2d and 3d standard arrays. --- Cpp/arrayDimensional.h | 86 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 Cpp/arrayDimensional.h diff --git a/Cpp/arrayDimensional.h b/Cpp/arrayDimensional.h new file mode 100644 index 0000000..65fc05e --- /dev/null +++ b/Cpp/arrayDimensional.h @@ -0,0 +1,86 @@ +// Simple implementation of 2d and 3d arrays +// Using standard array for 3d is unreadable and c-like arrays don't let you use algorithms libraries. This is here to fix that problem +// See below for example usage +// Mostly experimental. I will update this over time. +// While std::sort should be able to work it would have to be inside a loop for example. We can make our own std::sort for 2d and 3d arrays. +// I highly encourage you to add stuff too. +// +// TODO: +// Remove the annoying requirement for having to specify std::array etc when initializing. Maybe a make_array2d and a make_array3d function etc people can use? +// Add some sorting function. +// Operator<< overload is for visibility only for now. Make an alternative function that returns std::ostream& instead so people can choose their own implementation of <<. +// Add 4d arrays. +// Whatever you find cool and relevant. Vectors, maps, algorithms, operators etc. +// + +// EXAMPLE USAGE. +/* +int main() { + ARRD::array2d foo{ + std::array //Don't put ',' + {1, 2, 3, 4}, + {2, 4, 6, 8}, + {3, 1, 4, 1} + }; + + ARRD::array3d goo{ + ARRD::array2d + {}, + {} + }; + + ARRD::array3d hoo{ + foo, + goo[0], + {} + }; + + std::cout << "Equivelent:\tint foo [3][4]:\n" << foo << '\n'; + std::cout << "Equivelent:\tint goo [2][3][4]:\n" << goo << '\n'; + std::cout << "Equivelent:\tint hoo [3][3][4]:\n" << hoo << '\n'; + + return 0; +} +*/ + +#ifndef ARRAY_D +#define ARRAY_D + +#include +#include + +namespace ARRD { + template + using array2d = std::array< std::array, row_count >; + + template + using array3d = std::array< array2d, depth>; +} + + +template +std::ostream& operator<<(std::ostream& os + , const ARRD::array2d& arr) +{ + for (auto row : arr) { + for (T col : row) { + os << col << ' '; + } + os << '\n'; + } + return os << '\n'; +} + +template +std::ostream& operator<<(std::ostream& os + , const ARRD::array3d& arr) +{ + for (const auto& arr_2d : arr) { + os << arr_2d; + } + return os; +} + +#endif