Last active
January 29, 2024 09:49
-
-
Save DmitrySoshnikov/9a1a8b0462753a199bb0f885cd7f2b8e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Basic Array class, similar to std::array. | |
* | |
* by Dmitry Soshnikov <[email protected]> | |
* MIT Style License (C) 2020 | |
*/ | |
#ifndef __Array_h | |
#define __Array_h | |
#include <iterator> | |
#include <stddef.h> | |
#include <stdlib.h> | |
template <typename _Tp, size_t _Size> class Array { | |
/** | |
* Type aliases. | |
*/ | |
using __self = Array; | |
using value_type = _Tp; | |
using reference = value_type &; | |
using const_reference = const value_type &; | |
using iterator = value_type *; | |
using const_iterator = const value_type *; | |
using pointer = value_type *; | |
using const_pointer = const value_type *; | |
using size_type = size_t; | |
using difference_type = ptrdiff_t; | |
using reverse_iterator = std::reverse_iterator<iterator>; | |
using const_reverse_iterator = std::reverse_iterator<const_iterator>; | |
public: | |
/** | |
* Iterators. | |
*/ | |
iterator begin() { return iterator(data()); } | |
const_iterator begin() const { return const_iterator(data()); } | |
iterator end() { return iterator(data() + _Size); } | |
const_iterator end() const { return const_iterator(data() + _Size); } | |
reverse_iterator rbegin() { return reverse_iterator(end()); } | |
const_reverse_iterator rbegin() const { | |
return const_reverse_iterator(end()); | |
} | |
reverse_iterator rend() { return reverse_iterator(begin()); } | |
const_reverse_iterator rend() const { | |
return const_reverse_iterator(begin()); | |
} | |
const_iterator cbegin() const { return begin(); } | |
const_iterator cend() const { return end(); } | |
const_reverse_iterator crbegin() const { return rbegin(); } | |
const_reverse_iterator crend() const { return rend(); } | |
/** | |
* Capacity. | |
*/ | |
constexpr size_type size() const { return _Size; } | |
constexpr size_type max_size() const { return _Size; } | |
constexpr bool empty() const { return _Size == 0; } | |
/** | |
* Element access. | |
*/ | |
reference operator[](size_type index) { return m_Data[index]; } | |
const_reference operator[](size_type index) const { return m_Data[index]; } | |
reference front() { return m_Data[0]; } | |
const_reference front() const { return m_Data[0]; } | |
reference back() { return m_Data[_Size - 1]; } | |
const_reference back() const { return m_Data[_Size - 1]; } | |
value_type *data() { return m_Data; } | |
const value_type *data() const { return m_Data; } | |
private: | |
/** | |
* Storage. | |
*/ | |
int m_Data[_Size]; | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Array.h" | |
#include <iostream> | |
int main(int argc, char const *argv[]) { | |
Array<int, 5> values; | |
values[0] = 100; | |
values[1] = 200; | |
values[2] = 300; | |
for (const auto &v : values) { | |
std::cout << "Value: " << v << "\n"; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment