#ifndef ARRAY_H
#define ARRAY_H

#include <iostream>

#include "Exception.h"

class Array {
	private:
		int *array;
		int array_size;
		int count;

	public:
		Array( int = 10 );
		~Array();

		bool empty() const;
		int size() const;
		int get( int ) const;
		bool member( int ) const;
		void print() const;

		void insert( int );
		bool remove( int );
};

Array::Array( int n ):
array_size( n ),
count( 0 ) {
	array = new int[array_size];
}

Array::~Array() {
	delete[] array;
}

bool Array::empty() const {
	return count == 0;
}

int Array::size() const {
	return count;
}

int Array::get( int i ) const {
	if ( i < 0 || i >= count ) {
		throw illegal_argument();
	}

	return array[i];
}

bool Array::member( int n ) const {
	// yes, very inefficient for a sorted list...
	for ( int i = 0; i < count; ++i ) {
		if ( array[i] == n ) {
			return true;
		}
	}

	return false;
}

void Array::print() const {
	if ( count == 0 ) {
		return;
	}

	std::cout << array[0];

	for ( int i = 1; i < count; ++i ) {
		std::cout << "-" << array[i];
	}
}

void Array::insert( int n ) {
	if ( count == array_size ) {
		throw overflow();
	}

	++count;

	for ( int i = count - 2; i >= 0; --i ) {
		if ( array[i] > n ) {
			array[i + 1] = array[i];
		} else {
			array[i + 1] = n;
			return;
		}
	}

	array[0] = n;
}

bool Array::remove( int n ) {
	bool found = false;

	for ( int i = 0; i < count; ++i ) {
		if ( found ) {
			array[i] = array[i + 1];
		} else {
			if ( array[i] == n ) {
				array[i] = array[i + 1];
				--count;
				found = true;
			}
		}
	}

	return found;
}

#endif