array insertions

This commit is contained in:
2025-11-09 08:56:07 -05:00
parent 4dd6b805cb
commit 06c2ca306a
30 changed files with 171 additions and 2 deletions

View File

@ -14,6 +14,7 @@ namespace amsmathutil25
void test_amsarray_insertdelete1();
void test_amsarray_insertdelete2();
void test_amsarray_minimal();
void test_amsarray_arrayinsert();

View File

@ -79,6 +79,10 @@ namespace ams
T pop_back();
T pop_front();
int insert(amsarray_size_t ind, const amsarray<T>& other);
int append(const amsarray<T>& other);
//finds the first instance of val in the array
amsarray_size_t find(const T& val);

View File

@ -470,6 +470,87 @@ template<typename T> int amsarray<T>::insert(amsarray_size_t ind, const T& val)
return ret;
}
template<typename T> int amsarray<T>::insert(amsarray_size_t ind, const amsarray<T>& other)
{
int ret = amsarray_success;
int res;
amsarray_size_t I, oldlen;
amsarray_size_t I0,I1;
if(other.length==0)
{
ret = amsarray_success;
return ret;
}
if(ind<0)
{
ret = amsarray_failure;
return ret;
}
else if(ind<=this->length)
{
oldlen = this->length;
res = this->resize_insert(this->length + other.length);
if(res==amsarray_success)
{
//for now, do things single threaded.
//to do things multithreaded, I'd need another buffer
if(this->data!=NULL && other.data!=NULL)
{
for(I=oldlen;I>=ind+other.length;I--)
{
this->data[I] = this->data[I-1];
}
for(I=ind;I<ind+other.length;I++)
{
this->data[I] = other.data[I-ind];
}
}
ret = amsarray_success;
return ret;
}
else
{
ret = amsarray_failure;
return ret;
}
}
else
{
//inserting past the end of the array
res = this->resize_insert(ind+other.length);
if(res!=amsarray_success)
{
ret = amsarray_failure;
return ret;
}
else
{
if(this->data !=NULL && other.data !=NULL)
{
for(I=ind;I<ind+other.length;I++)
{
this->data[I] = other.data[I-ind];
}
}
ret = amsarray_success;
return ret;
}
}
return ret;
}
template<typename T> int amsarray<T>::append(const amsarray<T> &other)
{
int ret = amsarray_success;
ret = insert(length,other);
return ret;
}
//removes the value at index ind, and reduces array size by 1
template<typename T> int amsarray<T>::erase(amsarray_size_t ind)
{

View File

@ -9,6 +9,12 @@ namespace ams
//psize must be supplied to determine whether to execute in threaded mode or not.
template<typename callable, typename ... argst> int threaded_execute(callable &&fptr, int64_t psize, argst&&... args);
//A template function that takes as input a function pointer and a series of arguments
//The function is executed with fptr(threadnum, nthreads, otherargs...) with a given number of threads
template<typename callable, typename ... argst> int numthreaded_execute(callable &&fptr, int nthreads, argst&&... args);
template<typename T1, typename T2> struct pair
{
public:

View File

@ -68,6 +68,56 @@ template<typename callable, typename ... argst> int threaded_execute(callable &&
return ret;
}
//A template function that takes as input a function pointer and a series of arguments
//The function is executed with fptr(threadnum, nthreads, otherargs...) with a given number of threads
template<typename callable, typename ... argst> int numthreaded_execute(callable &&fptr, int nthreads, argst&&... args)
{
int ret = amsmathutil25_success;
int I;
std::vector<std::thread*> threads;
if(nthreads<=1)
{
nthreads = 1;
I = 0;
fptr(I,nthreads,std::forward<argst>(args)...);
}
else
{
threads.resize(nthreads);
for(I=0;I<nthreads;I++) threads[I] = NULL;
for(I=0;I<nthreads;I++)
{
threads[I] = new(std::nothrow) std::thread
(
std::forward<callable>(fptr),
I,
nthreads,
std::forward<argst>(args)...
);
}
for(I=0;I<nthreads;I++)
{
if(threads[I]==NULL)
{ //null thread creation failure check
//printf("debug check!\n");
ret = amsmathutil25_failure;
}
}
for(I=0;I<nthreads;I++)
{
if(threads[I]!=NULL)
{
threads[I]->join();
delete threads[I];
threads[I] = NULL;
}
}
}
return ret;
}
}; //end namespace ams