// file:          OwnerOfArray.hpp
// description:   Container of an array with random initialization.
//                Usable as Mixin.
//                Sak A16(4), PÜ 31.5(13)(d).
// author:        Karlheinz Hug
// creation date: 2012-06-14
// last update:   2012-07-02

#ifndef OWNER_OF_ARRAY_HPP_
#define OWNER_OF_ARRAY_HPP_

#include <cassert>
#include <vector>

#include "ToolsetRandomC.hpp"

	template <typename T>
	class OwnerOfArray
		// Instantiation condition: T is a comparable type.
	{
		protected:
			std::vector<T> array;

			int	low,
				high;

			OwnerOfArray ();

			virtual void init_array (unsigned int size, T min_item, T max_item);
	}; // OwnerOfArray

	template <typename T>
	OwnerOfArray<T>::OwnerOfArray () :
		low (0),		// Necessary, no default initialization!
		high (-1)
	{
	}

	template <typename T>
	void OwnerOfArray<T>::init_array (unsigned int size, T min_item, T max_item)
	{
		assert(min_item <= max_item); // precondition
		array.resize(size);
		high = size - 1;

		// Index variant:
		//	for (unsigned int index = 0; index < array.size(); ++index)
		//	{
		//		array[index] = ToolsetRandom<T>::uniform (min_item, max_item);
		//	}

		// Iterator variant:
		typename std::vector<T>::iterator pos;
		for (pos = array.begin(); pos != array.end(); ++pos)
		{
			*pos = ToolsetRandom<T>::uniform (min_item, max_item);
		}

		assert(low == 0);                        // invariant
		assert(high + 1 == (int)array.size());   // invariant
	}

#endif // OWNER_OF_ARRAY_HPP_
