<commit_before>/*
 *   CrissCross
 *   A multi-purpose cross-platform library.
 *
 *   A product of Uplink Laboratories.
 *
 *   (c) 2006-2008 <NAME>.
 *   Licensed under the New BSD License.
 *
 */

#ifndef __included_cc_darray_h
#error "This file shouldn't be compiled directly."
#endif

#include <crisscross/debug.h>

#include <crisscross/darray.h>
#include <crisscross/dstack.h>

namespace CrissCross
{
	namespace Data
	{
		template <class T>
		DArray <T>::DArray()
		{
			m_stepSize = -1;
			m_numUsed = m_arraySize = 0;
			m_shadow = NULL;
			m_array = NULL;
			m_emptyNodes = new DStack<size_t>;
			m_emptyNodes->push((size_t)-1);
			m_encapsulated = true;
		}

		template <class T>
		DArray <T>::DArray(T *_array, size_t _indices, bool _encapsulate)
		{
			m_encapsulated = _encapsulate;
			if ( _encapsulate )
			{
				m_array = new T[_indices];
				memcpy(m_array, _array, _indices * sizeof(T));
			} else {
				m_array = _array;
			}
			m_shadow = new char[_indices];
			memset(m_shadow, 0, _indices * sizeof(char));
			m_numUsed = 0;
			for (size_t i = 0; i < _indices; i++)
			{
				m_shadow[i] = m_array[i] != NULL ? 1 : 0;
				if ( m_shadow[i] )
				{
					m_numUsed++;
				}
			}
			m_numUsed = m_numUsed;
			m_arraySize = _indices;
			m_stepSize = -1;
			m_emptyNodes = new DStack<size_t>;
			rebuildStack();
		}

		template <class T>
		DArray <T>::DArray(DArray const &_array, bool _encapsulate)
		{
			m_encapsulated = _encapsulate;
			if ( _encapsulate )
			{
				m_array = new T[_array.m_arraySize];
				memcpy(m_array, _array.m_array, _array.m_arraySize * sizeof(T));
			} else {
				m_array = _array.m_array;
			}
			m_shadow = new char[_array.m_arraySize];
			memset(m_shadow, 0, _array.m_arraySize * sizeof(char));
			m_numUsed = 0;
			for (size_t i = 0; i < _array.m_arraySize; i++)
			{
				m_shadow[i] = m_array[i] != NULL ? 1 : 0;
				if ( m_shadow[i] )
				{
					m_numUsed++;
				}
			}
			m_arraySize = _array.m_arraySize;
			m_stepSize = _array.m_stepSize;
			m_emptyNodes = new DStack<size_t>;
			rebuildStack();
		}

		template <class T>
		DArray <T>::DArray(int _newStepSize)
		{
			if (_newStepSize < 1)
				m_stepSize = -1;
			else
				m_stepSize = _newStepSize;

			m_numUsed = m_arraySize = 0;
			m_shadow = NULL;
			m_array = NULL;
			m_emptyNodes = new DStack<size_t> (_newStepSize + 1);
			m_emptyNodes->push((size_t)-1);
			m_encapsulated = true;
		}

		template <class T>
		DArray <T>::~DArray()
		{
			empty();
			delete m_emptyNodes;
			m_emptyNodes = NULL;
		}

		template <class T>
		size_t DArray <T>::pop()
		{
			CoreAssert ( m_encapsulated );

			size_t freeslot = getNextFree();

			if (m_shadow[freeslot] == 0) m_numUsed++;

			m_shadow[freeslot] = 1;
			return freeslot;
		}

		template <class T>
		void DArray <T>::rebuildStack()
		{
			if ( !m_encapsulated ) return;

			/*  Reset free list */

			m_emptyNodes->empty();
			m_emptyNodes->push((size_t)-1);

			/* Step through, rebuilding */

			for (size_t i = m_arraySize - 1; (int)i >= 0; i--)
				if (m_shadow[i] == 0)
					m_emptyNodes->push(i);
		}

		template <class T>
		void DArray <T>::recount()
		{
			if ( !m_encapsulated ) return;

			m_numUsed = 0;
			for (size_t i = 0; i < m_arraySize; i++)
				if (m_shadow[i] == 1)
					m_numUsed++;
		}

		template <class T>
		void DArray <T>::setSize(size_t newsize)
		{
			if ( !m_encapsulated ) return;

			if (newsize > m_arraySize) {
				size_t oldarraysize = m_arraySize;

				m_arraySize = newsize;
				T     *temparray = new T[m_arraySize];
				char  *tempshadow = new char[m_arraySize];

				if (m_array && m_shadow) {
					//memcpy(&temparray[0], &m_array[0], sizeof(temparray[0]) * oldarraysize);
					for ( size_t a = 0; a < oldarraysize; a++ )
					{
						temparray[0] = m_array[0];
					}
					memcpy(&tempshadow[0], &m_shadow[0], sizeof(tempshadow[0]) * oldarraysize);
				}

				//memset(&temparray[oldarraysize], 0, sizeof(temparray[0]) * (m_arraySize - oldarraysize));
				memset(&tempshadow[oldarraysize], 0, sizeof(tempshadow[0]) * (m_arraySize - oldarraysize));

				for (size_t a = m_arraySize - 1; (int)a >= (int)oldarraysize; a--) {
					m_emptyNodes->push(a);
				}

				delete [] m_array;
				delete [] m_shadow;

				m_array = temparray;
				m_shadow = tempshadow;
			} else if (newsize < m_arraySize) {
				m_arraySize = newsize;
				T    *temparray = new T[m_arraySize];
				char *tempshadow = new char[m_arraySize];

				if (m_array && m_shadow) {
					memcpy(&temparray[0], &m_array[0], sizeof(temparray[0]) * m_arraySize);
					memcpy(&tempshadow[0], &m_shadow[0], sizeof(tempshadow[0]) * m_arraySize);
				}

				/* We may have lost more than one node. It's worth rebuilding over. */
				rebuildStack();
				recount();

				delete [] m_array;
				delete [] m_shadow;<commit_msg>Revert "darray: make safe for non-integer/pointer types"<commit_after>/*
 *   CrissCross
 *   A multi-purpose cross-platform library.
 *
 *   A product of Uplink Laboratories.
 *
 *   (c) 2006-2008 <NAME>.
 *   Licensed under the New BSD License.
 *
 */

#ifndef __included_cc_darray_h
#error "This file shouldn't be compiled directly."
#endif

#include <crisscross/debug.h>

#include <crisscross/darray.h>
#include <crisscross/dstack.h>

namespace CrissCross
{
	namespace Data
	{
		template <class T>
		DArray <T>::DArray()
		{
			m_stepSize = -1;
			m_numUsed = m_arraySize = 0;
			m_shadow = NULL;
			m_array = NULL;
			m_emptyNodes = new DStack<size_t>;
			m_emptyNodes->push((size_t)-1);
			m_encapsulated = true;
		}

		template <class T>
		DArray <T>::DArray(T *_array, size_t _indices, bool _encapsulate)
		{
			m_encapsulated = _encapsulate;
			if ( _encapsulate )
			{
				m_array = new T[_indices];
				memcpy(m_array, _array, _indices * sizeof(T));
			} else {
				m_array = _array;
			}
			m_shadow = new char[_indices];
			memset(m_shadow, 0, _indices * sizeof(char));
			m_numUsed = 0;
			for (size_t i = 0; i < _indices; i++)
			{
				m_shadow[i] = m_array[i] != NULL ? 1 : 0;
				if ( m_shadow[i] )
				{
					m_numUsed++;
				}
			}
			m_numUsed = m_numUsed;
			m_arraySize = _indices;
			m_stepSize = -1;
			m_emptyNodes = new DStack<size_t>;
			rebuildStack();
		}

		template <class T>
		DArray <T>::DArray(DArray const &_array, bool _encapsulate)
		{
			m_encapsulated = _encapsulate;
			if ( _encapsulate )
			{
				m_array = new T[_array.m_arraySize];
				memcpy(m_array, _array.m_array, _array.m_arraySize * sizeof(T));
			} else {
				m_array = _array.m_array;
			}
			m_shadow = new char[_array.m_arraySize];
			memset(m_shadow, 0, _array.m_arraySize * sizeof(char));
			m_numUsed = 0;
			for (size_t i = 0; i < _array.m_arraySize; i++)
			{
				m_shadow[i] = m_array[i] != NULL ? 1 : 0;
				if ( m_shadow[i] )
				{
					m_numUsed++;
				}
			}
			m_arraySize = _array.m_arraySize;
			m_stepSize = _array.m_stepSize;
			m_emptyNodes = new DStack<size_t>;
			rebuildStack();
		}

		template <class T>
		DArray <T>::DArray(int _newStepSize)
		{
			if (_newStepSize < 1)
				m_stepSize = -1;
			else
				m_stepSize = _newStepSize;

			m_numUsed = m_arraySize = 0;
			m_shadow = NULL;
			m_array = NULL;
			m_emptyNodes = new DStack<size_t> (_newStepSize + 1);
			m_emptyNodes->push((size_t)-1);
			m_encapsulated = true;
		}

		template <class T>
		DArray <T>::~DArray()
		{
			empty();
			delete m_emptyNodes;
			m_emptyNodes = NULL;
		}

		template <class T>
		size_t DArray <T>::pop()
		{
			CoreAssert ( m_encapsulated );

			size_t freeslot = getNextFree();

			if (m_shadow[freeslot] == 0) m_numUsed++;

			m_shadow[freeslot] = 1;
			return freeslot;
		}

		template <class T>
		void DArray <T>::rebuildStack()
		{
			if ( !m_encapsulated ) return;

			/*  Reset free list */

			m_emptyNodes->empty();
			m_emptyNodes->push((size_t)-1);

			/* Step through, rebuilding */

			for (size_t i = m_arraySize - 1; (int)i >= 0; i--)
				if (m_shadow[i] == 0)
					m_emptyNodes->push(i);
		}

		template <class T>
		void DArray <T>::recount()
		{
			if ( !m_encapsulated ) return;

			m_numUsed = 0;
			for (size_t i = 0; i < m_arraySize; i++)
				if (m_shadow[i] == 1)
					m_numUsed++;
		}

		template <class T>
		void DArray <T>::setSize(size_t newsize)
		{
			if ( !m_encapsulated ) return;

			if (newsize > m_arraySize) {
				size_t oldarraysize = m_arraySize;

				m_arraySize = newsize;
				T     *temparray = new T[m_arraySize];
				char  *tempshadow = new char[m_arraySize];

				if (m_array && m_shadow) {
					memcpy(&temparray[0], &m_array[0], sizeof(temparray[0]) * oldarraysize);
					memcpy(&tempshadow[0], &m_shadow[0], sizeof(tempshadow[0]) * oldarraysize);
				}

				memset(&temparray[oldarraysize], 0, sizeof(temparray[0]) * (m_arraySize - oldarraysize));
				memset(&tempshadow[oldarraysize], 0, sizeof(tempshadow[0]) * (m_arraySize - oldarraysize));

				for (size_t a = m_arraySize - 1; (int)a >= (int)oldarraysize; a--) {
					m_emptyNodes->push(a);
				}

				delete [] m_array;
				delete [] m_shadow;

				m_array = temparray;
				m_shadow = tempshadow;
			} else if (newsize < m_arraySize) {
				m_arraySize = newsize;
				T    *temparray = new T[m_arraySize];
				char *tempshadow = new char[m_arraySize];

				if (m_array && m_shadow) {
					memcpy(&temparray[0], &m_array[0], sizeof(temparray[0]) * m_arraySize);
					memcpy(&tempshadow[0], &m_shadow[0], sizeof(tempshadow[0]) * m_arraySize);
				}

				/* We may have lost more than one node. It's worth rebuilding over. */
				rebuildStack();
				recount();

				delete [] m_array;
				delete [] m_shadow;
{'fraction_non_alphanumeric': 0.14201370585134424, 'fraction_numerical': 0.011491829204006326, 'mean_word_length': 3.0434782608695654, 'pattern_counts': {'":': 0, '<': 66, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6932523', 'n_tokens_mistral': 4557, 'n_tokens_neox': 3740, 'n_words': 792}