<commit_before>/****************************************************************/
/*               DO NOT MODIFY THIS HEADER                      */
/* MOOSE - Multiphysics Object Oriented Simulation Environment  */
/*                                                              */
/*           (c) 2010 Battelle Energy Alliance, LLC             */
/*                   ALL RIGHTS RESERVED                        */
/*                                                              */
/*          Prepared by Battelle Energy Alliance, LLC           */
/*            Under Contract No. DE-AC07-05ID14517              */
/*            With the U. S. Department of Energy               */
/*                                                              */
/*            See COPYRIGHT for full restrictions               */
/****************************************************************/

#ifndef ARRAY_H
#define ARRAY_H

#include "Moose.h"

template<typename T>
class MooseArray
{
public:
  /**
   * Default constructor.  Doesn't initialize anything.
   */
  MooseArray() :
    _data(NULL),
    _size(0),
    _allocated_size(0)
  {}

  /**
   * @param size The initial size of the array.
   */
  explicit
  MooseArray(const unsigned int size) :
    _data(NULL),
    _allocated_size(0)
  {
    resize(size);
  }

  /**
   * @param size The initial size of the array.
   * @param default_value The default value to set.
   */
  explicit
  MooseArray(const unsigned int size, const T & default_value) :
    _data(NULL),
    _allocated_size(0)
  {
    resize(size);

    setAllValues(default_value);
  }

  /**
   * Sets all values of the array to the passed in value
   * @param value The value every entry of the array will be set to.
   */
  void setAllValues(const T & value);

  /**
   * Manually deallocates the data pointer
   */
  void release()
  {
    if (_data != NULL)
    {
      delete [] _data;
      _data = NULL;
      _allocated_size = _size = 0;
    }
  }

  /**
   * Change the number of elements the array can store.
   *
   * Will allocate more memory if necessary.
   *
   * Can destroy data currently in array!
   * Basically, data retention not guaranteed.
   *
   * Note that this does _not_ free unused memory.
   * This is done for speed.
   */
  void resize(const unsigned int size);

  /**
   * Change the number of elements the array can store.
   *
   * Will allocate more memory if necessary.
   *
   * Can destroy data currently in array!
   * Basically, data retention not guaranteed.
   *
   * Note that this does _not_ free unused memory.
   * This is done for speed.
   *
   * Also note that default_value is only applied to NEW entries.
   */
  void resize(const unsigned int size, const T & default_value);

  /**
   * The number of elements that can currently
   * be stored in the array.
   */
  unsigned int size();

  /**
   * Get element i out of the array.
   */
  T & operator[](const unsigned int i);

  /**
   * Get element i out of the array.
   */
  const T & operator[](const unsigned int i) const;

  /**
   * Doesn't actually make a copy of the data.
   *
   * Just makes _this_ object operate on the same data.
   *
   * Note! You can leak memory with this function if you
   * don't take care to have a copy of _this_ array somewhere
   * else.  This is because the data pointer will get overriden
   * here.
   */
  void shallowCopy(const MooseArray & rhs);

private:

  /**
   * Actual data pointer.
   */
  T * _data;

  /**
   * The current number of elements the array can hold.
   */
  unsigned int _size;

  /**
   * Number of allocated memory positions for storage.
   */
  unsigned int _allocated_size;
};

template<typename T>
inline
void
MooseArray<T>::setAllValues(const T & value)
{
  for(unsigned int i=0; i<_size; i++)
    _data[i] = value;
}

template<typename T>
inline
void
MooseArray<T>::resize(const unsigned int size)
{
  if(size <= _allocated_size)
    _size = size;
  else
  {
    T * new_pointer = new T[size];
    mooseAssert(new_pointer, "Failed to allocate MooseArray memory!");

    if (_data != NULL)
      delete [] _data;
    _data = new_pointer;
    _allocated_size = size;
    _size = size;
  }
}

template<typename T>
inline
void
MooseArray<T>::resize(const unsigned int size, const T & default_value)
{
  if (size <= _allocated_size)
    _size = size;
  else
  {
    T * new_pointer = new T[size];
    mooseAssert(new_pointer, "Failed to allocate MooseArray memory!");

    if (_data != NULL)
      delete [] _data;
    _data = new_pointer;

    for(unsigned int i=_size; i<size; i++)
      _data[i] = default_value;
<commit_msg>Fix uninitialized memory access in MooseArray. Closes #764<commit_after>/****************************************************************/
/*               DO NOT MODIFY THIS HEADER                      */
/* MOOSE - Multiphysics Object Oriented Simulation Environment  */
/*                                                              */
/*           (c) 2010 Battelle Energy Alliance, LLC             */
/*                   ALL RIGHTS RESERVED                        */
/*                                                              */
/*          Prepared by Battelle Energy Alliance, LLC           */
/*            Under Contract No. DE-AC07-05ID14517              */
/*            With the U. S. Department of Energy               */
/*                                                              */
/*            See COPYRIGHT for full restrictions               */
/****************************************************************/

#ifndef ARRAY_H
#define ARRAY_H

#include "Moose.h"

template<typename T>
class MooseArray
{
public:
  /**
   * Default constructor.  Doesn't initialize anything.
   */
  MooseArray() :
    _data(NULL),
    _size(0),
    _allocated_size(0)
  {}

  /**
   * @param size The initial size of the array.
   */
  explicit
  MooseArray(const unsigned int size) :
    _data(NULL),
    _allocated_size(0)
  {
    resize(size);
  }

  /**
   * @param size The initial size of the array.
   * @param default_value The default value to set.
   */
  explicit
  MooseArray(const unsigned int size, const T & default_value) :
    _data(NULL),
    _allocated_size(0)
  {
    resize(size);

    setAllValues(default_value);
  }

  /**
   * Sets all values of the array to the passed in value
   * @param value The value every entry of the array will be set to.
   */
  void setAllValues(const T & value);

  /**
   * Manually deallocates the data pointer
   */
  void release()
  {
    if (_data != NULL)
    {
      delete [] _data;
      _data = NULL;
      _allocated_size = _size = 0;
    }
  }

  /**
   * Change the number of elements the array can store.
   *
   * Will allocate more memory if necessary.
   *
   * Can destroy data currently in array!
   * Basically, data retention not guaranteed.
   *
   * Note that this does _not_ free unused memory.
   * This is done for speed.
   */
  void resize(const unsigned int size);

  /**
   * Change the number of elements the array can store.
   *
   * Will allocate more memory if necessary.
   *
   * Can destroy data currently in array!
   * Basically, data retention not guaranteed.
   *
   * Note that this does _not_ free unused memory.
   * This is done for speed.
   *
   * Also note that default_value is only applied to NEW entries.
   */
  void resize(const unsigned int size, const T & default_value);

  /**
   * The number of elements that can currently
   * be stored in the array.
   */
  unsigned int size();

  /**
   * Get element i out of the array.
   */
  T & operator[](const unsigned int i);

  /**
   * Get element i out of the array.
   */
  const T & operator[](const unsigned int i) const;

  /**
   * Doesn't actually make a copy of the data.
   *
   * Just makes _this_ object operate on the same data.
   *
   * Note! You can leak memory with this function if you
   * don't take care to have a copy of _this_ array somewhere
   * else.  This is because the data pointer will get overriden
   * here.
   */
  void shallowCopy(const MooseArray & rhs);

private:

  /**
   * Actual data pointer.
   */
  T * _data;

  /**
   * The current number of elements the array can hold.
   */
  unsigned int _size;

  /**
   * Number of allocated memory positions for storage.
   */
  unsigned int _allocated_size;
};

template<typename T>
inline
void
MooseArray<T>::setAllValues(const T & value)
{
  for(unsigned int i=0; i<_size; i++)
    _data[i] = value;
}

template<typename T>
inline
void
MooseArray<T>::resize(const unsigned int size)
{
  if(size <= _allocated_size)
    _size = size;
  else
  {
    T * new_pointer = new T[size];
    mooseAssert(new_pointer, "Failed to allocate MooseArray memory!");

    if (_data != NULL)
      delete [] _data;
    _data = new_pointer;
    _allocated_size = size;
    _size = size;
  }
}

template<typename T>
inline
void
MooseArray<T>::resize(const unsigned int size, const T & default_value)
{
  if (size <= _allocated_size)
    _size = size;
  else
  {
    T * new_pointer = new T[size];
    mooseAssert(new_pointer, "Failed to allocate MooseArray memory!");

    if (_data != NULL)
    {
      for (unsigned int i=0; i<_size; i++)
        new_pointer[i] = _data[i];
      delete [] _data;
    }
    _data = new_pointer;

    for(unsigned int i=_size; i<size; i++)
      _data[i] = default_value;

{'fraction_non_alphanumeric': 0.12301121195942338, 'fraction_numerical': 0.004484783769353977, 'mean_word_length': 2.0748522652659225, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6066782', 'n_tokens_mistral': 2799, 'n_tokens_neox': 2703, 'n_words': 941}