|
|
|
@ -338,6 +338,186 @@ template<typename T> const narray_size_t narray<T>::size() const
|
|
|
|
|
return length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//inserts a value at (before) index ind
|
|
|
|
|
//insert(0,val) would give {val, oldval0, oldval1, ...}
|
|
|
|
|
//insert(N,val)
|
|
|
|
|
template<typename T> int narray<T>::insert(const narray_size_t ind, const T val)
|
|
|
|
|
{
|
|
|
|
|
int ret = narray_success;
|
|
|
|
|
int res;
|
|
|
|
|
narray<T> 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;I<ind && I<this->length;I++)
|
|
|
|
|
{
|
|
|
|
|
narr.data[I] = this->data[I];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
narr.data[ind] = val;
|
|
|
|
|
if(this->data!=NULL)
|
|
|
|
|
{
|
|
|
|
|
for(I=ind+1;I<narr.length;I++)
|
|
|
|
|
{
|
|
|
|
|
narr.data[I] = this->data[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;I<this->length;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<typename T> int narray<T>::erase(const narray_size_t ind)
|
|
|
|
|
{
|
|
|
|
|
int ret = narray_success;
|
|
|
|
|
int res;
|
|
|
|
|
narray<T> 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;I<ind && I<this->length && I<narr.length;I++)
|
|
|
|
|
{
|
|
|
|
|
narr.data[I] = this->data[I];
|
|
|
|
|
}
|
|
|
|
|
for(I=ind+1;I<this->length && (I-1)<narr.length;I++)
|
|
|
|
|
{
|
|
|
|
|
narr.data[I-1] = this->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<typename T> narray_size_t narray<T>::find(const T val)
|
|
|
|
|
{
|
|
|
|
|
narray_size_t ret = -1;
|
|
|
|
|
narray_size_t I;
|
|
|
|
|
|
|
|
|
|
for(I=0;I<this->length;I++)
|
|
|
|
|
{
|
|
|
|
|
if(this->data[I]==val)
|
|
|
|
|
{
|
|
|
|
|
ret = I;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//finds the next instance of val, starting consideration at indstart
|
|
|
|
|
template<typename T> narray_size_t narray<T>::findnext(const narray_size_t indstart, const T val)
|
|
|
|
|
{
|
|
|
|
|
narray_size_t ret = -1;
|
|
|
|
|
narray_size_t I;
|
|
|
|
|
|
|
|
|
|
for(I=indstart;I<this->length;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<typename T> narray_size_t narray<T>::find_insert_ordered(const T val)
|
|
|
|
|
{
|
|
|
|
|
narray_size_t ret = -1;
|
|
|
|
|
narray_size_t I;
|
|
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
for(I=0;I<this->length;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
|
|
|
|
|
|
|
|
|
|