#ifndef __AMSCPPNARRAY_HPP__ #define __AMSCPPNARRAY_HPP__ #include #include #include #include #include #include #include #include #include #include #include namespace ams { namespace narray { // Forward declaration of the operator- template function //template class narray; //template narray operator-(const narray &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 class narray { public: narray_size_t length; T *data; narray(); narray(const narray& other); narray(narray&& other) noexcept; narray& operator=(const narray& other); narray& operator=(narray&& other) noexcept; narray& operator=(const std::vector& other); narray(const std::initializer_list initlist); narray& operator=(const std::initializer_list initlist); ~narray(); template operator narray() 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& other) const; bool operator!=(const narray& other) const; //Operations narray operator+(const narray& other) const; narray operator-(const narray& other) const; narray operator*(const narray& other) const; narray operator/(const narray& other) const; narray operator+(const T val) const; narray operator-(const T val) const; narray operator*(const T val) const; narray operator/(const T val) const; template friend narray operator-(const narray &n); narray 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 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 narray arange(const T low, const T high, const T incr); template narray zeros(const narray_size_t N); template narray ones(const narray_size_t N); narray linspacef(const float low, const float high, narray_size_t N); narray 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 #include #include #include #include #include #include #include #endif