Marcus Lee / LinearArray

linearArray.hpp

Committer:
UHSLMarcus
Date:
2017-03-08
Revision:
8:18c9d62b8f7c
Parent:
7:6e24d8ccecd4
Child:
11:1e27a6f0b0cf

File content as of revision 8:18c9d62b8f7c:

template<class type>
LinearArray<type>::LinearArray(int size, bool forced) :
		_elem_count(0), _array_size(size), _front(0), _rear(-1), _forced(forced) {
	_array = new type[size];
}

template<class type>
LinearArray<type>::~LinearArray() {
	delete[] _array;
}

template<class type>
int LinearArray<type>::push(type item) {

	int ret = -1;
	bool room = _elem_count < _array_size;
	if (!room && _forced) {
		if (++_front == _array_size) _front = 0;
		_elem_count--;
		room = true;
	}
	if (room) {
		if (_rear == _array_size - 1) _rear = -1;
		_array[++_rear] = item;
		ret = _elem_count++;
	}

	return ret;
}

template<class type>
type& LinearArray<type>::pop() {

	type item = NULL;

	if (_elem_count > 0) {
		item = _array[_front++];
		if (_front == _array_size) _front = 0;
		_elem_count--;
	}

	return item;

}

template<class type>
type& LinearArray<type>::peek() {

	type item = NULL;

	if (_front <= _rear) {
		item = _array[_front];
	}

	return item;

}

template<class type>
int LinearArray<type>::size() {
	return _array_size;
}

template<class type>
int LinearArray<type>::count() {
	return _elem_count;
}

template<class type>
bool LinearArray<type>::full() {
	return _elem_count == _array_size;
}