<commit_before>#ifndef SimTK_SimTKCOMMON_ARRAY_H_
#define SimTK_SimTKCOMMON_ARRAY_H_

/* -------------------------------------------------------------------------- *
 *                      SimTK Core: SimTKcommon                               *
 * -------------------------------------------------------------------------- *
 * This is part of the SimTK Core biosimulation toolkit originating from      *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org.               *
 *                                                                            *
 * Portions copyright (c) 2005-7 Stanford University and the Authors.         *
 * Authors: <NAME>                                                   *
 * Contributors:                                                              *
 *                                                                            *
 * Permission is hereby granted, free of charge, to any person obtaining a    *
 * copy of this software and associated documentation files (the "Software"), *
 * to deal in the Software without restriction, including without limitation  *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
 * and/or sell copies of the Software, and to permit persons to whom the      *
 * Software is furnished to do so, subject to the following conditions:       *
 *                                                                            *
 * The above copyright notice and this permission notice shall be included in *
 * all copies or substantial portions of the Software.                        *
 *                                                                            *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
 * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
 * -------------------------------------------------------------------------- */

#include "SimTKcommon/internal/common.h"

#include <cstddef>
#include <cassert>
#include <ostream>


namespace SimTKimpl {
	
class ArrayHelperImpl;

/**
 * Non-templatized helper class for ArrayBase<T>.
 */

class SimTK_SimTKCOMMON_EXPORT ArrayHelper {
public:		
	// no default constructor
	explicit ArrayHelper(const TypeManipulatorT&, int n=0);
	ArrayHelper(const TypeManipulatorT&, int n, const void* ival, bool repeat);
	ArrayHelper(const ArrayHelper&);

    // Sub-array constructors, read only and writable
    ArrayHelper(const ArrayHelper&, int offset, int length);
    ArrayHelper(ArrayHelper&,       int offset, int length);    
    
	~ArrayHelper();
	ArrayHelper& operator=(const ArrayHelper&);

    void reverse(); // reverse the order of all elements
	
    // use signed integers to avoid signed/unsigned mismatch misery
	int capacity() const;
	int size() const;	
		
	const void* operator[](int i) const; 
	void* operator[](int i);
	
	void push_back(const void*);
	void pop_back();
	void clear();
	void resize(int n, const void* x=0);
    void reserve(int n);
	
private:
	ArrayHelperImpl* impl;
    
    ArrayHelperImpl&        updImpl()       {assert(impl); return *impl;}
    const ArrayHelperImpl&  getImpl() const {assert(impl); return *impl;}
};

} //namespace SimTKimpl

namespace SimTK {
template <class T> class Array;
template <class T> class ArrayView;

template <class T> class ArrayBase {
public:
    ArrayBase() : ah(type()) { }
    explicit ArrayBase(int n) : ah(type(),n) { }
    ArrayBase(int n, const T& ival)  : ah(type(),n,&ival,true) { }
    ArrayBase(int n, const T* ivals) : ah(type(),n,ivals,false) { }
        
    // Sub-array constructors, read only and writable, and their operator equivalents
    ArrayBase(const ArrayBase& a, int offset, int length) : ah(a.ah,offset,length) { }
    ArrayBase(ArrayBase&       a, int offset, int length) : ah(a.ah,offset,length) { }

    // Default copy, assignment, destructor made explicit here for convenient debugging.
    // These are "deep" copies.
    ArrayBase(const ArrayBase& src) : ah(src.ah) { }
    ~ArrayBase() { }
    ArrayBase& operator=(const ArrayBase& src) {
        if (this != &src)
            ah = src.ah;
        return *this;
    }

    // Assign the same value to every element.
    ArrayBase& operator=(const T& x) {
        for (int i=0; i < size(); ++i)
            (*this)[i] = x;
        return *this;
    }

    typedef T*       iterator;
    typedef const T* const_iterator;
    typedef T&       reference;
    typedef const T& const_reference;

    reference       front()       {assert(size()); return (*this)[0];}
    const_reference front() const {assert(size()); return (*this)[0];}
    reference       back()        {assert(size()); return (*this)[size()-1];}
    const_reference back()  const {assert(size()); return (*this)[size()-1];}

    iterator       begin()       {return &front();}
    const_iterator begin() const {return &front();}
    iterator       end()         {return &back() + 1;}
    const_iterator end()   const {return &back() + 1;}

    void reverse() {ah.reverse();}

    int capacity() const { return ah.capacity(); }
    int size()     const { return ah.size(); }

    const T& operator[](int i) const {return getAs(ah[i]);}
    T&       operator[](int i)       {return updAs(ah[i]);}<commit_msg>Fixed Array<> begin() and end() so that they return null if the Array is empty rather than throwing an exception. Now the test begin()==end() will work properly when Array is empty. Note that front() and back() still throw exceptions for empty Arrays since they return references.<commit_after>#ifndef SimTK_SimTKCOMMON_ARRAY_H_
#define SimTK_SimTKCOMMON_ARRAY_H_

/* -------------------------------------------------------------------------- *
 *                      SimTK Core: SimTKcommon                               *
 * -------------------------------------------------------------------------- *
 * This is part of the SimTK Core biosimulation toolkit originating from      *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org.               *
 *                                                                            *
 * Portions copyright (c) 2005-7 Stanford University and the Authors.         *
 * Authors: <NAME>                                                   *
 * Contributors:                                                              *
 *                                                                            *
 * Permission is hereby granted, free of charge, to any person obtaining a    *
 * copy of this software and associated documentation files (the "Software"), *
 * to deal in the Software without restriction, including without limitation  *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
 * and/or sell copies of the Software, and to permit persons to whom the      *
 * Software is furnished to do so, subject to the following conditions:       *
 *                                                                            *
 * The above copyright notice and this permission notice shall be included in *
 * all copies or substantial portions of the Software.                        *
 *                                                                            *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
 * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
 * -------------------------------------------------------------------------- */

#include "SimTKcommon/internal/common.h"

#include <cstddef>
#include <cassert>
#include <ostream>


namespace SimTKimpl {
	
class ArrayHelperImpl;

/**
 * Non-templatized helper class for ArrayBase<T>.
 */

class SimTK_SimTKCOMMON_EXPORT ArrayHelper {
public:		
	// no default constructor
	explicit ArrayHelper(const TypeManipulatorT&, int n=0);
	ArrayHelper(const TypeManipulatorT&, int n, const void* ival, bool repeat);
	ArrayHelper(const ArrayHelper&);

    // Sub-array constructors, read only and writable
    ArrayHelper(const ArrayHelper&, int offset, int length);
    ArrayHelper(ArrayHelper&,       int offset, int length);    
    
	~ArrayHelper();
	ArrayHelper& operator=(const ArrayHelper&);

    void reverse(); // reverse the order of all elements
	
    // use signed integers to avoid signed/unsigned mismatch misery
	int capacity() const;
	int size() const;	
		
	const void* operator[](int i) const; 
	void* operator[](int i);
	
	void push_back(const void*);
	void pop_back();
	void clear();
	void resize(int n, const void* x=0);
    void reserve(int n);
	
private:
	ArrayHelperImpl* impl;
    
    ArrayHelperImpl&        updImpl()       {assert(impl); return *impl;}
    const ArrayHelperImpl&  getImpl() const {assert(impl); return *impl;}
};

} //namespace SimTKimpl

namespace SimTK {
template <class T> class Array;
template <class T> class ArrayView;

template <class T> class ArrayBase {
public:
    ArrayBase() : ah(type()) { }
    explicit ArrayBase(int n) : ah(type(),n) { }
    ArrayBase(int n, const T& ival)  : ah(type(),n,&ival,true) { }
    ArrayBase(int n, const T* ivals) : ah(type(),n,ivals,false) { }
        
    // Sub-array constructors, read only and writable, and their operator equivalents
    ArrayBase(const ArrayBase& a, int offset, int length) : ah(a.ah,offset,length) { }
    ArrayBase(ArrayBase&       a, int offset, int length) : ah(a.ah,offset,length) { }

    // Default copy, assignment, destructor made explicit here for convenient debugging.
    // These are "deep" copies.
    ArrayBase(const ArrayBase& src) : ah(src.ah) { }
    ~ArrayBase() { }
    ArrayBase& operator=(const ArrayBase& src) {
        if (this != &src)
            ah = src.ah;
        return *this;
    }

    // Assign the same value to every element.
    ArrayBase& operator=(const T& x) {
        for (int i=0; i < size(); ++i)
            (*this)[i] = x;
        return *this;
    }

    typedef T*       iterator;
    typedef const T* const_iterator;
    typedef T&       reference;
    typedef const T& const_reference;

    reference       front()       {assert(size()); return (*this)[0];}
    const_reference front() const {assert(size()); return (*this)[0];}
    reference       back()        {assert(size()); return (*this)[size()-1];}
    const_reference back()  const {assert(size()); return (*this)[size()-1];}

    // If the Array is empty, begin() and end() both return null so 
    // begin()==end() will return true.
    iterator       begin()       {return size() ? &front() : 0;}
    const_iterator begin() const {return size() ? &front() : 0;}
    iterator       end()         {return size() ? &back() + 1 : 0;}
    const_iterator end()   const {return size() ? &back() + 1 : 0;}

    void reverse() {ah.reverse();}

    int capacity() const { return ah.capacity(); }
    int size()     const { return ah.size(); }

    const T& operator[](int i) const {return getAs(ah[i]);}
    T&       operator[](int i)       {return updAs(ah[i]);}
{'fraction_non_alphanumeric': 0.13796250207400032, 'fraction_numerical': 0.003982080637132902, 'mean_word_length': 2.005484916479681, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 20, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1875923', 'n_tokens_mistral': 3203, 'n_tokens_neox': 3036, 'n_words': 1140}