#ifndef __AMSCPPNARRAY_IMPL_HPP__ #define __AMSCPPNARRAY_IMPL_HPP__ namespace ams { namespace narray { template narray::narray() { //printf("debug: constructor called for %p.\n",this); length = 0; data = NULL; return; } template int narray::resize(const narray_size_t newlength) { int ret = narray_failure; narray_size_t I; T *newdata = NULL; narray_size_t ml; if(newlength<=0) { if(data!=NULL) {delete[] data; data=NULL;} length = 0; ret = narray_success; return ret; } newdata = new(std::nothrow) T[newlength]; if(newdata==NULL) { ret = narray_failure; return ret; } //threaded initialization for speed ml = (length(newdata,data,ml); buffer_set(newdata,T(),length,newlength); // for(I=0;I narray::narray(const narray& other) { narray_size_t I; int res; length = 0; data = NULL; narray_size_t ml; //printf("debug: copy constructor called for\n\t%p\n\t%p\n", this, &other); if(this!=&other) { res = this->resize(other.length); if(res!=narray_success) { return; } else { //threaded copy for speed ml = (length(data,other.data,ml); // for(I=0;I narray::narray(narray&& other) noexcept { //this is a move operator //I still need to understand when these are called. //printf("debug: move constructor called for\n\t%p\n\t%p\n", this, &other); //steal the resources from other and reset other to prevent double deletion length = other.length; data = other.data; other.length = 0; other.data = NULL; return; } template narray& narray::operator=(narray&& other) noexcept { //printf("debug: move= called for\n\t%p\n\t%p\n", this, &other); if(this!=&other) { if(data!=NULL) {delete[] data; data=NULL;} length = other.length; data = other.data; other.length = 0; other.data = NULL; } return *this; } template narray& narray::operator=(const narray& other) { narray_size_t I; int res; narray_size_t ml; //printf("debug: operator= called for\n\t%p\n\t%p\n", this, &other); if(this!=&other) { res = this->resize(other.length); if(res!=narray_success) { return *this; } else { //threaded copy for speed ml = (length(data,other.data,ml); // for(I=0;I narray& narray::operator=(const std::vector& other) { narray_size_t I; int res; if(this!=&other) { res = this->resize(other.size()); if(res!=narray_success) { return *this; } else { for(I=0;I narray::narray(const std::initializer_list initlist) { narray_size_t I; int res; length = 0; data = NULL; res = this->resize(initlist.size()); if(res!=narray_success) { return; } else { I = 0; for(T elem : initlist) { data[I] = elem; if(I narray& narray::operator=(const std::initializer_list initlist) { narray_size_t I; int res; res = this->resize(initlist.size()); if(res!=narray_success) { this->length = 0; if(this->data != NULL) {delete[] this->data; this->data=NULL;} return *this; } else { I = 0; for(T elem : initlist) { data[I] = elem; if(I template narray::operator narray() const { //casting datatypes narray ret; int res; res = ret.resize(this->length); if(res!=narray_success) { ret.resize(0); return ret; } else { buffer_copycast(ret.data,data,length); } return ret; } template narray::~narray() { //printf("debug: destructor called for %p\n",this); length = 0; if(data!=NULL) {delete[] data; data=NULL;} return; } template T& narray::operator[](const narray_size_t ind) { return data[ind]; } template const T& narray::operator[](const narray_size_t ind) const { return data[ind]; } template T& narray::at(const narray_size_t ind) { return data[ind]; } template const T& narray::at(const narray_size_t ind) const { return data[ind]; } template void narray::clear() { this->resize(0); return; } template void narray_setall_tf(narray *q, const T& val, int threadnum, int nthreads) { int I0,I1,Is,I; Is = q->length/nthreads; I0 = Is*threadnum; I1 = (threadnum>=(nthreads-1))? q->length: Is*(threadnum+1); for(I=I0;Idata[I] = val; } return; } template void narray::setall(const T& val) { narray_size_t I; int J; int nthreads; std::vector threads; if(lengthnarray_max_threads) nthreads = narray_max_threads; threads.resize(nthreads); for(J=0;J,this,val,J,nthreads ); } for(J=0;Jjoin(); delete threads[J]; threads[J]= NULL; } } } return; } template const narray_size_t narray::size() const { return length; } //inserts a value at (before) index ind //insert(0,val) would give {val, oldval0, oldval1, ...} //insert(N,val) template int narray::insert(const narray_size_t ind, const T val) { int ret = narray_success; int res; narray narr; narray_size_t I; if(ind<0) { ret = narray_failure; return ret; } else if(ind<=this->length) { res = narr.resize(this->length+1); if(res!=narray_success) { ret = narray_failure; return ret; } if(this->data!=NULL) { for(I=0;Ilength;I++) { narr.data[I] = this->data[I]; } } narr.data[ind] = val; if(this->data!=NULL) { for(I=ind+1;Idata[I-1]; } } //steal data from narr if(this->data!=NULL) {delete[] this->data; this->data=NULL;} this->length = narr.length; this->data = narr.data; narr.length = 0; narr.data = NULL; } else { //inserting past the end of the array res = narr.resize(ind+1); if(res!=narray_success) { ret = narray_failure; return ret; } if(this->data!=NULL) { for(I=0;Ilength;I++) { narr.data[I] = this->data[I]; } } narr.data[ind] = val; //steal data from narr if(this->data!=NULL) {delete[] this->data; this->data=NULL;} this->length = narr.length; this->data = narr.data; narr.length = 0; narr.data = NULL; } return ret; } //removes the value at index ind, and reduces array size by 1 template int narray::erase(const narray_size_t ind) { int ret = narray_success; int res; narray narr; narray_size_t I; if(ind<0 || ind>=this->length) { ret = narray_failure; return ret; } res = narr.resize(this->length-1); if(res!=narray_success) { ret = narray_failure; return ret; } if(this->data!=NULL) { for(I=0;Ilength && Idata[I]; } for(I=ind+1;Ilength && (I-1)data[I]; } } //steal data from narr (should be similar to what std::move would do?) if(this->data!=NULL) {delete[] this->data; this->data=NULL;} this->length = narr.length; this->data = narr.data; narr.length = 0; narr.data = NULL; return ret; } //finds the first instance of val in the array template narray_size_t narray::find(const T val) { narray_size_t ret = -1; narray_size_t I; for(I=0;Ilength;I++) { if(this->data[I]==val) { ret = I; break; } } return ret; } //finds the next instance of val, starting consideration at indstart template narray_size_t narray::findnext(const narray_size_t indstart, const T val) { narray_size_t ret = -1; narray_size_t I; for(I=indstart;Ilength;I++) { if(this->data[I]==val) { ret = I; break; } } return ret; } //finds where to insert a particular value such that the list remains ordered template narray_size_t narray::find_insert_ordered(const T val) { narray_size_t ret = -1; narray_size_t I; ret = 0; for(I=0;Ilength;I++) { ret = I+1; if(this->data[I]>val) { ret = I; break; } } ret = (ret<0) ? 0 : ret; ret = (ret>this->length) ? this->length : ret; return ret; } }; //end namespace narray }; //end namespace ams #endif