170 lines
4.8 KiB
C++
170 lines
4.8 KiB
C++
#ifndef __AMSCPPNARRAY_HPP__
|
|
#define __AMSCPPNARRAY_HPP__
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <math.h>
|
|
#include <time.h>
|
|
#include <stdint.h>
|
|
|
|
#include <vector>
|
|
#include <new>
|
|
#include <initializer_list>
|
|
#include <thread>
|
|
#include <algorithm>
|
|
|
|
#include <amsmathutil25/amsmathutil25.hpp>
|
|
|
|
namespace ams
|
|
{
|
|
namespace narray
|
|
{
|
|
|
|
// Forward declaration of the operator- template function
|
|
//template<typename T> class narray;
|
|
//template<typename T> narray<T> operator-(const narray<T> &n);
|
|
|
|
typedef int64_t narray_size_t;
|
|
|
|
static const int narray_success = 1;
|
|
static const int narray_failure = -1;
|
|
|
|
//problem size at which to begin using threaded operations
|
|
static const narray_size_t narray_thread_sz = 5000;
|
|
|
|
//maximum number of threads to use
|
|
static const int narray_max_threads = 50;
|
|
|
|
template<typename T> class narray
|
|
{
|
|
public:
|
|
narray_size_t length;
|
|
T *data;
|
|
|
|
narray();
|
|
narray(const narray<T>& other);
|
|
narray(narray<T>&& other) noexcept;
|
|
|
|
narray<T>& operator=(const narray<T>& other);
|
|
narray<T>& operator=(narray<T>&& other) noexcept;
|
|
|
|
narray<T>& operator=(const std::vector<T>& other);
|
|
narray(const std::initializer_list<T> initlist);
|
|
narray<T>& operator=(const std::initializer_list<T> initlist);
|
|
~narray();
|
|
|
|
template<typename T2> operator narray<T2>() const; //casting datatypes
|
|
|
|
int resize(const narray_size_t newlength);
|
|
const narray_size_t size() const;
|
|
|
|
T& operator[](const narray_size_t ind);
|
|
const T& operator[](const narray_size_t ind) const;
|
|
T& at(const narray_size_t ind);
|
|
const T& at(const narray_size_t ind) const;
|
|
|
|
void clear(); //erases all elements, sets size to 0
|
|
void setall(const T& val); //sets all elements to val
|
|
|
|
//Comparators
|
|
bool operator==(const narray<T>& other) const;
|
|
bool operator!=(const narray<T>& other) const;
|
|
|
|
//Operations
|
|
narray<T> operator+(const narray<T>& other) const;
|
|
narray<T> operator-(const narray<T>& other) const;
|
|
narray<T> operator*(const narray<T>& other) const;
|
|
narray<T> operator/(const narray<T>& other) const;
|
|
narray<T> operator+(const T val) const;
|
|
narray<T> operator-(const T val) const;
|
|
narray<T> operator*(const T val) const;
|
|
narray<T> operator/(const T val) const;
|
|
template<class U> friend narray<U> operator-(const narray<U> &n);
|
|
narray<T> applyfn(T (*fnptr)(T)) const;
|
|
|
|
//min,max
|
|
T min() const;
|
|
T max() const;
|
|
|
|
//insertions and deletions
|
|
// naive, assumes no ordering to the array
|
|
|
|
//inserts a value at (before) index ind
|
|
//insert(0,val) would give {val, oldval0, oldval1, ...}
|
|
//insert(N,val)
|
|
int insert(const narray_size_t ind, const T val);
|
|
|
|
//removes the value at index ind, and reduces array size by 1
|
|
int erase(const narray_size_t ind);
|
|
|
|
//finds the first instance of val in the array
|
|
narray_size_t find(const T val);
|
|
|
|
//finds the next instance of val, starting consideration at indstart
|
|
narray_size_t findnext(const narray_size_t indstart, const T val);
|
|
|
|
//finds where to insert a particular value such that the list remains ordered
|
|
narray_size_t find_insert_ordered(const T val);
|
|
|
|
|
|
//subarray operations
|
|
narray<T> subarray(const narray_size_t ind1, const narray_size_t ind2) const;
|
|
|
|
//permutations and sorting
|
|
|
|
|
|
//C++11 iterators
|
|
typedef T* iterator;
|
|
typedef const T* const_iterator;
|
|
iterator begin() {return data;}
|
|
iterator end() {return data + length;}
|
|
const_iterator begin() const {return data;}
|
|
const_iterator end() const {return data + length;}
|
|
const_iterator cbegin() const { return data; }
|
|
const_iterator cend() const { return data+length;}
|
|
|
|
//printf (must be template specialized)
|
|
void print(int printstyle = 0);
|
|
|
|
};
|
|
|
|
//Initializers
|
|
template<typename T> narray<T> arange(const T low, const T high, const T incr);
|
|
template<typename T> narray<T> zeros(const narray_size_t N);
|
|
template<typename T> narray<T> ones(const narray_size_t N);
|
|
|
|
narray<float> linspacef(const float low, const float high, narray_size_t N);
|
|
narray<double> linspace(const double low, const double high, narray_size_t N);
|
|
|
|
//cast/conversion
|
|
|
|
|
|
/////////
|
|
//Tests//
|
|
/////////
|
|
|
|
void test_narray1();
|
|
void test_narray2();
|
|
void test_narray3();
|
|
|
|
void test_insertdelete1();
|
|
|
|
|
|
|
|
|
|
}; //end namespace narray
|
|
}; //end namespace ams
|
|
|
|
#include <amscppnarray/amscppnarray_llbops.hpp>
|
|
|
|
#include <amscppnarray/amscppnarray_impl.hpp>
|
|
#include <amscppnarray/amscppnarray_imploper.hpp>
|
|
#include <amscppnarray/amscppnarray_implsubarray.hpp>
|
|
#include <amscppnarray/amscppnarray_implinits.hpp>
|
|
|
|
#include <amscppnarray/amscppnarray_random.hpp>
|
|
#include <amscppnarray/amscppnarray_math.hpp>
|
|
|
|
#include <amscppnarray/amscppnarray_binrw.hpp>
|
|
|
|
#endif |