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

Binary file not shown.

View File

@ -14,7 +14,7 @@ depdir = "./dependencies/linux64" #local pre-compiled dependency libraries and t
installdir = "../../linux64" #directory to install to when finished
builddir = "./build_linux64"
doinstall = False #copies the build_output to the install dir when finished
doinstall = True #copies the build_output to the install dir when finished
cc = "g++" #compiler
cflags = "-O3 -fPIC"
libraries = "-l{}".format(libname)

Binary file not shown.

Binary file not shown.

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

View File

@ -293,7 +293,33 @@ void test_amsarray_minimal()
}
}
void test_amsarray_arrayinsert()
{
amsarray<int> q1,q2,q3;
int I;
q1 = {1,2,3,4,5};
q2 = {0,0,1,0};
q3 = q1;
printf("Array append test\n");
printf("q3="); q3.print(1);
printf("q2="); q2.print(1);
q3.append(q2);
printf("q3+q2="); q3.print(1);
printf("q3="); q3.print(1);
printf("q2="); q2.print(1);
for(I=-2;I<q1.length+2;I++)
{
q3 = q1;
q3.insert(I,q2);
printf("q3.insert(%d,q2)=",I); q3.print(1);
}
return;
}
}; //end namespace amsmathutil25

View File

@ -14,7 +14,8 @@ int main(int argc, char* argv[])
//ams::amsmathutil25::test_amsarray_sort1();
//test_amsarray_insertdelete1();
//test_amsarray_insertdelete2();
test_amsarray_minimal();
//test_amsarray_minimal();
//test_amsarray_arrayinsert();
return ret;