<commit_before>/*! \file ArrayContainer */

#ifndef ARRAY_CONTAINER_H
#define ARRAY_CONTAINER_H

#include "numlib-config.h"
#include "debug_tools.h"

namespace numlib{

//! A simple, generic, implementation of a resizable array
/*!
 *	This implements a very simple array container with the usual
 *	element access operators and iterators. This container is
 *	a compliment to the standard STL containers in that it 
 *	assumes the array size remains fixed once initialized. This
 *	assumption allows this container to be very lightweight
 *	and implement fast iterators and element lookup.
 *
 *	If you need dynamic resizing (e.g. element insertion, deletion, etc)
 *	then you should use one of the STL containers. STL containers are
 *	very well designed, and some compilers can better optimize STL containers
 *	since they can safely make assumptions regarding their usage.
 *
 *	This class does not provide any arithmetic operators or related member
 *	functions. The purpose of this class is limited to memory management and
 *	element access. However, this class may be used as a foundation for
 *	defining numerical arrays and other field types.
 *
 */
template<class T>
class ArrayContainer
{
public:

	typedef T ValueType;

	/*------------------------------------------------------------------------*/
	/*                                         INITIALIZATION AND DUPLICATION */

	//! Initializes an array for n elements (default 0)
	ArrayContainer(const Size size=0):
		n(size),
		data(NULL)
	{
		data = new T[n];
	}

	//! Deep copy initialization
	ArrayContainer(const ArrayContainer& other):data(NULL)
	{
		ASSERT( other.data );
		n = other.n;
		data = new T[n];
		for(Index i=0; i<n; ++i)
			data[i] = other.data[i];
	}

	virtual
	~ArrayContainer()
	{
		delete[] data;
	}

	//! Deep copy assignment
	ArrayContainer& operator=(const ArrayContainer& other)
	{
		if(&other==this) return *this;
		// Free old data...
		n = other.n;
		delete[] data;
		// Allocate memory for new data...
		n = other.n;
		data = new T[n];
		// Copy data over from other...
		for(Index i=0; i<n; ++i)
			data[i] = other.data[i];
		// Return reference to this object...
		return *this;
	}

	/*------------------------------------------------------------------------*/
	/*                                                         ELEMENT ACCESS */

	//! Returns number of elements in the array
	const Size size() const {return n;}

	//! Resizes the array to hold n elements
	/*!
	 *	This does not preserve contents!
	 */
	void resize(const Size size)
	{
		delete[] data;
		n = size;
		data = new T[n];
	}

	//! Mutable element access
	T& operator[](const Index i) 
	{
		ASSERT( i < n );
		return data[i];
	}

	//! Immutable element access
	const T& operator[](const Index i) const 
	{<commit_msg>Changed ArrayContainer to use () for subscripting instead of []<commit_after>/*! \file ArrayContainer */

#ifndef ARRAY_CONTAINER_H
#define ARRAY_CONTAINER_H

#include "numlib-config.h"
#include "debug_tools.h"

namespace numlib{

//! A simple, generic, implementation of a resizable array
/*!
 *	This implements a very simple array container with the usual
 *	element access operators and iterators. This container is
 *	a compliment to the standard STL containers in that it 
 *	assumes the array size remains fixed once initialized. This
 *	assumption allows this container to be very lightweight
 *	and implement fast iterators and element lookup.
 *
 *	If you need dynamic resizing (e.g. element insertion, deletion, etc)
 *	then you should use one of the STL containers. STL containers are
 *	very well designed, and some compilers can better optimize STL containers
 *	since they can safely make assumptions regarding their usage.
 *
 *	This class does not provide any arithmetic operators or related member
 *	functions. The purpose of this class is limited to memory management and
 *	element access. However, this class may be used as a foundation for
 *	defining numerical arrays and other field types.
 *
 */
template<class T>
class ArrayContainer
{
public:

	typedef T ValueType;

	/*------------------------------------------------------------------------*/
	/*                                         INITIALIZATION AND DUPLICATION */

	//! Initializes an array for n elements (default 0)
	ArrayContainer(const Size size=0):
		n(size),
		data(NULL)
	{
		data = new T[n];
	}

	//! Deep copy initialization
	ArrayContainer(const ArrayContainer& other):data(NULL)
	{
		ASSERT( other.data );
		n = other.n;
		data = new T[n];
		for(Index i=0; i<n; ++i)
			data[i] = other.data[i];
	}

	virtual
	~ArrayContainer()
	{
		delete[] data;
	}

	//! Deep copy assignment
	ArrayContainer& operator=(const ArrayContainer& other)
	{
		if(&other==this) return *this;
		// Free old data...
		n = other.n;
		delete[] data;
		// Allocate memory for new data...
		n = other.n;
		data = new T[n];
		// Copy data over from other...
		for(Index i=0; i<n; ++i)
			data[i] = other.data[i];
		// Return reference to this object...
		return *this;
	}

	/*------------------------------------------------------------------------*/
	/*                                                         ELEMENT ACCESS */

	//! Returns number of elements in the array
	const Size size() const {return n;}

	//! Resizes the array to hold n elements
	/*!
	 *	This does not preserve contents!
	 */
	void resize(const Size size)
	{
		delete[] data;
		n = size;
		data = new T[n];
	}

	//! Mutable element access
	T& operator()(const Index i) 
	{
		ASSERT( i < n );
		return data[i];
	}

	//! Immutable element access
	const T& operator()(const Index i) const 
	{
{'fraction_non_alphanumeric': 0.14962593516209477, 'fraction_numerical': 0.0014250089063056644, 'mean_word_length': 2.415450121654501, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7216786', 'n_tokens_mistral': 1713, 'n_tokens_neox': 1580, 'n_words': 630}