diff --git a/build/__pycache__/amsbuildlib4.cpython-312.pyc b/build/__pycache__/amsbuildlib4.cpython-312.pyc new file mode 100644 index 0000000..5596d1b Binary files /dev/null and b/build/__pycache__/amsbuildlib4.cpython-312.pyc differ diff --git a/build/make.msvc64.lib.py b/build/make.msvc64.lib.py index cac461f..6fa819c 100644 --- a/build/make.msvc64.lib.py +++ b/build/make.msvc64.lib.py @@ -8,16 +8,16 @@ from shutil import copytree from amsbuildlib4 import * libname = "amscppnarray.msvc64" #static library name to generate -binname = "tests" #create this executable when compiling main.c or main.cpp +binname = "tests.exe" #create this executable when compiling main.c or main.cpp commondir = "../../winx64" #common directory to pul libraries and includes from depdir = "./dependencies/winx64" #local pre-compiled dependency libraries and their includes installdir = "../../winx64" #directory to install to when finished builddir = "./build_msvc64" -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 = "cl" #compiler -cflags = "/O2" -libraries = "lib{}.lib".format(libname) +cflags = "/O2 /EHsc" +libraries = "lib{}.lib libamsmathutil2.msvc64.lib".format(libname) libdirs = "/LIBPATH:{} /LIBPATH:{}/lib /LIBPATH:{}/lib".format(builddir,commondir,depdir) linkerflags = "-static -static-libgcc -Wl,-rpath=." srcexts = [".c",".cpp"] @@ -54,7 +54,7 @@ if(doinstall): #Copy a binary to the common bin folder #Push any libraries to the common lib folder - shutil.copy('{}/lib/lib{}.lib'.format(builddir,libname),commondir) + shutil.copy('{}/lib{}.lib'.format(builddir,libname),"{}/lib".format(commondir)) #Copy include files to the common include folder copytree('./include/',commondir+'/include/',dirs_exist_ok=True) diff --git a/build/make.msvc64.test.py b/build/make.msvc64.test.py index e92dce4..aaff335 100644 --- a/build/make.msvc64.test.py +++ b/build/make.msvc64.test.py @@ -14,12 +14,12 @@ depdir = "./dependencies/winx64" #local pre-compiled dependency libraries and th installdir = "../../winx64" #directory to install to when finished builddir = "./build_msvc64" -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 = "cl" #compiler -cflags = "/O2" -libraries = "lib{}.lib".format(libname) +cflags = "/O2 /EHsc" +libraries = "lib{}.lib libamsmathutil2.msvc64.lib".format(libname) libdirs = "/LIBPATH:{} /LIBPATH:{}/lib /LIBPATH:{}/lib".format(builddir,commondir,depdir) -linkerflags = "" +linkerflags = "-static -static-libgcc -Wl,-rpath=." srcexts = [".c",".cpp"] binsrc = ["main.c","main.cpp"] #ignore these files when compiling the static library diff --git a/build_linux64/libamscppnarray.linux64.a b/build_linux64/libamscppnarray.linux64.a index c3c1898..1778633 100644 Binary files a/build_linux64/libamscppnarray.linux64.a and b/build_linux64/libamscppnarray.linux64.a differ diff --git a/build_linux64/objstore/amscppnarray.o b/build_linux64/objstore/amscppnarray.o index 06f74ea..7fd85af 100644 Binary files a/build_linux64/objstore/amscppnarray.o and b/build_linux64/objstore/amscppnarray.o differ diff --git a/build_linux64/objstore/amscppnarray_random.o b/build_linux64/objstore/amscppnarray_random.o index 5a8810a..89ce430 100644 Binary files a/build_linux64/objstore/amscppnarray_random.o and b/build_linux64/objstore/amscppnarray_random.o differ diff --git a/build_linux64/tests b/build_linux64/tests index c34c08d..eadb535 100644 Binary files a/build_linux64/tests and b/build_linux64/tests differ diff --git a/build_msvc64/libamscppnarray.msvc64.lib b/build_msvc64/libamscppnarray.msvc64.lib new file mode 100644 index 0000000..bb6bf89 Binary files /dev/null and b/build_msvc64/libamscppnarray.msvc64.lib differ diff --git a/build_msvc64/objstore/amscppnarray.obj b/build_msvc64/objstore/amscppnarray.obj new file mode 100644 index 0000000..0c666a1 Binary files /dev/null and b/build_msvc64/objstore/amscppnarray.obj differ diff --git a/build_msvc64/objstore/amscppnarray_inits.obj b/build_msvc64/objstore/amscppnarray_inits.obj new file mode 100644 index 0000000..e651932 Binary files /dev/null and b/build_msvc64/objstore/amscppnarray_inits.obj differ diff --git a/build_msvc64/objstore/amscppnarray_math.obj b/build_msvc64/objstore/amscppnarray_math.obj new file mode 100644 index 0000000..0e93352 Binary files /dev/null and b/build_msvc64/objstore/amscppnarray_math.obj differ diff --git a/build_msvc64/objstore/amscppnarray_random.obj b/build_msvc64/objstore/amscppnarray_random.obj new file mode 100644 index 0000000..6227b18 Binary files /dev/null and b/build_msvc64/objstore/amscppnarray_random.obj differ diff --git a/build_msvc64/tests b/build_msvc64/tests new file mode 100644 index 0000000..687bda0 Binary files /dev/null and b/build_msvc64/tests differ diff --git a/build_msvc64/tests.exe b/build_msvc64/tests.exe new file mode 100644 index 0000000..13ab8ac Binary files /dev/null and b/build_msvc64/tests.exe differ diff --git a/include/amscppnarray/amscppnarray.hpp b/include/amscppnarray/amscppnarray.hpp index fdf3911..883d40f 100644 --- a/include/amscppnarray/amscppnarray.hpp +++ b/include/amscppnarray/amscppnarray.hpp @@ -86,7 +86,27 @@ template class narray 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; @@ -103,6 +123,9 @@ template class narray const_iterator cbegin() const { return data; } const_iterator cend() const { return data+length;} + //printf (must be template specialized) + void print(int printstyle = 0); + }; //Initializers @@ -124,6 +147,9 @@ void test_narray1(); void test_narray2(); void test_narray3(); +void test_insertdelete1(); + + }; //end namespace narray }; //end namespace ams diff --git a/include/amscppnarray/amscppnarray_impl.hpp b/include/amscppnarray/amscppnarray_impl.hpp index 0e330ab..0d196fb 100644 --- a/include/amscppnarray/amscppnarray_impl.hpp +++ b/include/amscppnarray/amscppnarray_impl.hpp @@ -338,6 +338,186 @@ 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 diff --git a/include/amscppnarray/amscppnarray_random.hpp b/include/amscppnarray/amscppnarray_random.hpp index 8caa5e8..a5a7c6f 100644 --- a/include/amscppnarray/amscppnarray_random.hpp +++ b/include/amscppnarray/amscppnarray_random.hpp @@ -23,11 +23,11 @@ namespace rand extern narray_rands_t dpr32_rseed; // global random number seed for numeric array class narray_rands_t dpr32_nextseed(narray_rands_t seed); - double dpr32_randd(narray_rands_t *seed); - float dpr32_randf(narray_rands_t *seed); - double dpr32_gaussian(narray_rands_t *seed); - float dpr32_gaussianf(narray_rands_t *seed); - int dpr32_randint(narray_rands_t *seed, int low, int high); + double dpr32_randd(narray_rands_t *seed = &(ams::narray::rand::dpr32_rseed)); + float dpr32_randf(narray_rands_t *seed = &(ams::narray::rand::dpr32_rseed)); + double dpr32_gaussian(narray_rands_t *seed = &(ams::narray::rand::dpr32_rseed)); + float dpr32_gaussianf(narray_rands_t *seed = &(ams::narray::rand::dpr32_rseed)); + int dpr32_randint(int low, int high, narray_rands_t *seed = &(ams::narray::rand::dpr32_rseed)); ams::vect2 dpr32_randuvect2(narray_rands_t *seed); ams::vect3 dpr32_randuvect3(narray_rands_t *seed); diff --git a/src/amscppnarray/amscppnarray.cpp b/src/amscppnarray/amscppnarray.cpp index 7e5826d..70ee5b3 100644 --- a/src/amscppnarray/amscppnarray.cpp +++ b/src/amscppnarray/amscppnarray.cpp @@ -5,6 +5,65 @@ namespace ams namespace narray { +template<> void narray::print(const int printstyle) +{ + narray_size_t I; + + printf("{"); + for(I=0;Ilength-1;I++) + { + printf("%1.3f,",this->data[I]); + } + if(this->length>=1) printf("%1.3f",this->data[this->length-1]); + printf("}"); + + return; +} + +template<> void narray::print(const int printstyle) +{ + narray_size_t I; + + printf("{"); + for(I=0;Ilength-1;I++) + { + printf("%1.3f,",this->data[I]); + } + if(this->length>=1) printf("%1.3f",this->data[this->length-1]); + printf("}"); + + return; +} + +template<> void narray::print(const int printstyle) +{ + narray_size_t I; + + printf("{"); + for(I=0;Ilength-1;I++) + { + printf("%d,",this->data[I]); + } + if(this->length>=1) printf("%d",this->data[this->length-1]); + printf("}"); + + return; +} + +template<> void narray::print(const int printstyle) +{ + narray_size_t I; + + printf("{"); + for(I=0;Ilength-1;I++) + { + printf("%ld,",this->data[I]); + } + if(this->length>=1) printf("%ld",this->data[this->length-1]); + printf("}"); + + return; +} void test_narray1() { @@ -110,6 +169,62 @@ void test_narray3() } } +void test_insertdelete1() +{ + narray q; + int I,J,K; + + q.insert(0,1); + q.print(); printf("\n"); + q.insert(0,2); + q.print(); printf("\n"); + q.insert(0,3); + q.print(); printf("\n"); + q.insert(-1,1); + q.print(); printf("\n"); + q.insert(10,99); + q.print(); printf("\n"); + + I = 3; + printf("q.find(%d)=%d\n",I,(int)q.find(I)); + I = 1; + printf("q.find(%d)=%d\n",I,(int)q.find(I)); + I = 5; + printf("q.find(%d)=%d\n",I,(int)q.find(I)); + I = 99; + printf("q.find(%d)=%d\n",I,(int)q.find(I)); + + I = 1; + printf("q.findinsert(%d)=%d\n",I,(int)q.find_insert_ordered(I)); + I = -1; + printf("q.findinsert(%d)=%d\n",I,(int)q.find_insert_ordered(I)); + I = 5; + printf("q.findinsert(%d)=%d\n",I,(int)q.find_insert_ordered(I)); + I = 99; + printf("q.findinsert(%d)=%d\n",I,(int)q.find_insert_ordered(I)); + I = 100; + printf("q.findinsert(%d)=%d\n",I,(int)q.find_insert_ordered(I)); + + q.erase(0); + q.print(); printf("\n"); + q.erase(-1); + q.print(); printf("\n"); + q.erase(q.length); + q.print(); printf("\n"); + q.erase(q.length-1); + q.print(); printf("\n"); + + q.resize(0); + for(I=0;I<25;I++) + { + J = rand::dpr32_randint(5,25); + K = q.find_insert_ordered(J); + q.insert(K,J); + q.print(); printf("\n"); + } + + return; +} }; //end namespace narray }; //end namespace ams \ No newline at end of file diff --git a/src/amscppnarray/amscppnarray_random.cpp b/src/amscppnarray/amscppnarray_random.cpp index 85a8dad..ac421f4 100644 --- a/src/amscppnarray/amscppnarray_random.cpp +++ b/src/amscppnarray/amscppnarray_random.cpp @@ -455,15 +455,15 @@ namespace rand } - int dpr32_randint(narray_rands_t *seed, int low, int high) + int dpr32_randint(int low, int high, narray_rands_t *seed) { int ret = 0; - int val; + if(high-low>0) { *seed = dpr32_nextseed(*seed); - val = low + (int)(*seed)%(high-low); + ret = low + (int)((*seed)%(high-low)); } return ret; @@ -472,7 +472,7 @@ namespace rand template void narray_rand_threadf2( narray *out, narray *rseeds, - T (*randfunc)(narray_rands_t *, T, T), + T (*randfunc)(T, T, narray_rands_t*), T low, T highexcl, int threadnum, @@ -485,7 +485,7 @@ namespace rand I1 = (threadnum>=(nthreads-1)) ? out->length : Is*(threadnum+1); for(I=I0;Idata[I] = randfunc(&(rseeds->data[threadnum]),low,highexcl); + out->data[I] = randfunc(low,highexcl,&(rseeds->data[threadnum])); } return; } @@ -493,7 +493,7 @@ namespace rand template void narray_random_threadexec2( narray *out, narray_size_t N, - T (*randfunc)(narray_rands_t *, T, T), + T (*randfunc)(T, T, narray_rands_t *), T low, T highexcl, narray_rands_t *rseed @@ -518,7 +518,7 @@ namespace rand //single threaded for(I=0;Idata[I] = randfunc(rseed,low,highexcl); + out->data[I] = randfunc(low,highexcl,rseed); } } else diff --git a/src/main.cpp b/src/main.cpp index 5d8bf2d..9b551cd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,8 +8,9 @@ int main(int argc, char* argv[]) // ams::narray::test_narray2(); // ams::narray::test_narray3(); // ams::narray::narray_testmath1(); - ams::narray::narray_testmath2(); + //ams::narray::narray_testmath2(); //ams::narray::narray_testmath3(); + ams::narray::test_insertdelete1(); return ret; } \ No newline at end of file