-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
305 additions
and
11 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# 42 | ||
*.DS_Store | ||
*.vscode | ||
*Run | ||
|
||
# Prerequisites | ||
*.d | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#include "Array.hpp" | ||
|
||
|
||
// Constructors - Destructor | ||
|
||
Array::Array() | ||
{ | ||
std::cout << "[ Called def. constr. for an ARRAY instance ]\n"; | ||
|
||
this->size = 0; | ||
this->arr = new int[ this->size ]; | ||
|
||
} | ||
Array::Array( int s ) | ||
{ | ||
std::cout << "[ Called param. cons. for an ARRAY instance ]\n"; | ||
|
||
this->size = s; | ||
this->arr = new int[ this->size ]; | ||
|
||
for ( int i = 0; i < this->size; i++ ) | ||
this->arr[ i ] = 0; | ||
} | ||
Array::Array( const Array &other ) | ||
{ | ||
std::cout << "[ Called copy constr. for an ARRAY instance ]\n"; | ||
|
||
this->size = other.getSize(); | ||
this->arr = new int[ this->size ]; | ||
|
||
for ( int i = 0; i < this->size; i++ ) | ||
this->arr[ i ] = other.getValue( i ); | ||
} | ||
Array::~Array() | ||
{ | ||
std::cout << "[ Destroying an ARRAY instance ]\n"; | ||
|
||
delete [] this->arr; | ||
} | ||
|
||
|
||
// Operator Overloads | ||
|
||
Array &Array::operator= ( const Array &other ) | ||
{ | ||
std::cout << "[ Called assign. op. for an ARRAY instance ]\n"; | ||
|
||
delete [] this->arr; | ||
|
||
this->size = other.getSize(); | ||
this->arr = new int[ this->size ]; | ||
|
||
for ( int i = 0; i < this->size; i++ ) | ||
this->arr[ i ] = other.getValue( i ); | ||
|
||
return *this; | ||
} | ||
int &Array::operator[] ( int i ) | ||
{ | ||
//std::cout << "[ Called subscript. op for an ARRAY instance ]\n"; | ||
|
||
return this->arr[ this->getIndex( i )]; | ||
} | ||
|
||
|
||
// Setters - Getters | ||
|
||
int Array::getIndex( int i ) const | ||
{ | ||
if ( i < -this->size || i >= this->size ) | ||
throw InvalidIndex(); | ||
|
||
else if ( i < 0 ) // allows reading the array backwards via negative indexes | ||
return this->size + i; | ||
|
||
else | ||
return i; | ||
} | ||
|
||
void Array::setSize( int s ) | ||
{ | ||
if ( s < 0 ) | ||
throw InvalidSize(); | ||
|
||
int *tmp = new int[ s ]; | ||
|
||
for ( int i = 0; i < this->size; i++ ) | ||
tmp[ this->getIndex( i ) ] = this->arr[ this->getIndex( i )]; | ||
|
||
delete [] this->arr; | ||
|
||
this->size = s; | ||
this->arr = tmp; | ||
} | ||
void Array::setValue( int i, int v ) { this->arr[ this->getIndex( i )] = v; } | ||
|
||
int Array::getSize( void ) const { return this->size; } | ||
int Array::getValue( int i ) const { return this->arr[ this->getIndex( i )]; } | ||
|
||
|
||
|
||
std::ostream &operator<< (std::ostream &out, const Array &rhs) | ||
{ | ||
out << "[ "; | ||
for ( int i = 0; i < rhs.getSize(); i++ ) | ||
out << rhs.getValue( i ) << " "; | ||
out << "]"; | ||
return (out); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#ifndef ARRAY_HPP | ||
# define ARRAY_HPP | ||
|
||
# include <exception> | ||
# include <iostream> | ||
# include <sstream> | ||
# include <string> | ||
|
||
class Array | ||
{ | ||
private: | ||
// Attributes | ||
int *arr; | ||
int size; | ||
|
||
protected: | ||
int getIndex( int i ) const; | ||
|
||
// Nested Classes | ||
class InvalidSize : public std::exception | ||
{ | ||
public: | ||
virtual const char *what() const throw() { return "array error : size cannot be negative"; } | ||
}; | ||
class InvalidIndex : public std::exception | ||
{ | ||
public: | ||
virtual const char *what() const throw() { return "array error : index must be within bounds"; } | ||
}; | ||
|
||
public: | ||
// Constructors - Destructor | ||
Array(); | ||
Array( const int s ); | ||
Array( const Array &other ); | ||
~Array(); | ||
|
||
// Operator Overloads | ||
Array &operator= ( const Array &other ); | ||
int &operator[] ( int i ); | ||
|
||
// Setters - Getters | ||
void setSize( int s ); | ||
void setValue( int i, int v ); | ||
|
||
int getSize( void ) const; | ||
int getValue( int i ) const; | ||
|
||
|
||
}; | ||
|
||
std::ostream &operator<< (std::ostream &out, const Array &rhs); | ||
|
||
#endif // ARRAY_HPP |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,159 @@ | ||
#include <iostream> | ||
#include "Array.hpp" | ||
|
||
template < typename T > void print( T const &i ) { std::cout << i << std::endl; } | ||
#define MAX_VAL 10 | ||
/* | ||
int intTester() | ||
{ | ||
Array<int> numbers(MAX_VAL); | ||
int* mirror = new int[MAX_VAL]; | ||
srand(time(NULL)); | ||
for (int i = 0; i < MAX_VAL; i++) | ||
{ | ||
const int value = rand(); | ||
numbers[i] = value; | ||
mirror[i] = value; | ||
} | ||
//SCOPE | ||
{ | ||
Array<int> tmp = numbers; | ||
Array<int> test(tmp); | ||
} | ||
for (int i = 0; i < MAX_VAL; i++) | ||
{ | ||
if (mirror[i] != numbers[i]) | ||
{ | ||
std::cerr << "didn't save the same value!!" << std::endl; | ||
return 1; | ||
} | ||
} | ||
try | ||
{ | ||
numbers[-2] = 0; | ||
} | ||
catch(const std::exception& e) | ||
{ | ||
std::cerr << e.what() << '\n'; | ||
} | ||
try | ||
{ | ||
numbers[MAX_VAL] = 0; | ||
} | ||
catch(const std::exception& e) | ||
{ | ||
std::cerr << e.what() << '\n'; | ||
} | ||
template < typename T > void iter( T *ptr, size_t len, void ( *f )( T const & ) ) | ||
for (int i = 0; i < MAX_VAL; i++) | ||
{ | ||
numbers[i] = rand(); | ||
} | ||
delete [] mirror;// | ||
return 0; | ||
} */ | ||
void intTester() | ||
{ | ||
T *arr = reinterpret_cast< T * >( ptr ); | ||
for ( size_t i = 0; i < len; i++ ) | ||
f( arr[ i ]); | ||
Array numbers(MAX_VAL); | ||
int* mirror = new int[MAX_VAL]; | ||
srand(time(NULL)); | ||
std::cout << "\nArray : " << numbers << std::endl; | ||
|
||
for (int i = 0; i < MAX_VAL; i++) | ||
{ | ||
const int value = rand() % 256; | ||
numbers[i] = value; | ||
mirror[i] = value; | ||
} | ||
std::cout << "Array : " << numbers << std::endl << std::endl; | ||
|
||
//SCOPE | ||
{ | ||
Array tmp = numbers; | ||
Array test(tmp); | ||
} | ||
|
||
for (int i = 0; i < MAX_VAL; i++) | ||
{ | ||
if (mirror[i] != numbers[i]) | ||
{ | ||
std::cerr << "\ndidn't save the same value!!" << std::endl; | ||
return; | ||
} | ||
} | ||
|
||
try { | ||
std::cout << "\nTrying to access index MIN_VAL - 1" << std::endl; | ||
int value = numbers[-MAX_VAL - 1]; | ||
std::cout << "Value : " << value << std::endl; | ||
numbers[-MAX_VAL - 1] = value; | ||
} catch(const std::exception& e) { std::cerr << e.what() << '\n'; } | ||
|
||
try { | ||
std::cout << "\nTrying to access index MIN_VAL" << std::endl; | ||
int value = numbers[-MAX_VAL]; | ||
std::cout << "Value : " << value << std::endl; | ||
numbers[-MAX_VAL] = value; | ||
} catch(const std::exception& e) { std::cerr << e.what() << '\n'; } | ||
|
||
try { | ||
std::cout << "\nTrying to access index -1" << std::endl; | ||
int value = numbers[-1]; | ||
std::cout << "Value : " << value << std::endl; | ||
numbers[-1] = value; | ||
} catch(const std::exception& e) { std::cerr << e.what() << '\n'; } | ||
|
||
try { | ||
std::cout << "\nTrying to access index 0" << std::endl; | ||
int value = numbers[0]; | ||
std::cout << "Value : " << value << std::endl; | ||
numbers[0] = value; | ||
} catch(const std::exception& e) { std::cerr << e.what() << '\n'; } | ||
|
||
try { | ||
std::cout << "\nTrying to access index MAX_VAL - 1" << std::endl; | ||
int value = numbers[MAX_VAL - 1]; | ||
std::cout << "Value : " << value << std::endl; | ||
numbers[MAX_VAL - 1] = value; | ||
} catch(const std::exception& e) { std::cerr << e.what() << '\n'; } | ||
|
||
try { | ||
std::cout << "\nTrying to access index MAX_VAL" << std::endl; | ||
int value = numbers[MAX_VAL]; | ||
std::cout << "Value : " << value << std::endl; | ||
numbers[MAX_VAL] = value; | ||
} catch(const std::exception& e) { std::cerr << e.what() << '\n'; } | ||
|
||
std::cout << "\nTrying to reassign values forwards" << std::endl; | ||
for (int i = 0; i < MAX_VAL; i++) { numbers[i] = rand() % 256; } | ||
std::cout << "Array : " << numbers << std::endl; | ||
|
||
|
||
std::cout << "\nTrying to reassign values backwards" << std::endl; | ||
for (int i = 0; i > -MAX_VAL; --i) { numbers[i] = rand() % 256; } | ||
std::cout << "Array : " << numbers << std::endl; | ||
|
||
delete [] mirror; | ||
|
||
std::cout << std::endl; | ||
} | ||
|
||
void runTests( void ) | ||
{ | ||
std::cout << "\nO================================ TEST 1 ================================O\n" << std::endl; | ||
{ | ||
int arr[] = { 1, 2, 3, 4, 5 }; | ||
iter( arr, 5, print); | ||
|
||
} | ||
std::cout << "\nO================================ TEST 2 ================================O\n" << std::endl; | ||
{ | ||
float arr[] = { 1.1f, 2.2f, 3.3f, 4.4f, 5.5f }; | ||
iter( arr, 5, print); | ||
|
||
} | ||
std::cout << "\nO================================= END ==================================O\n\n" << std::endl; | ||
} | ||
|
||
int main( void ) | ||
{ | ||
runTests(); | ||
//runTests(); | ||
intTester(); | ||
//TemplateTester(); | ||
} |