insert erase find

master
madrocketsci 2 weeks ago
parent 8f5774695f
commit c40c9ba56e

@ -8,16 +8,16 @@ from shutil import copytree
from amsbuildlib4 import * from amsbuildlib4 import *
libname = "amscppnarray.msvc64" #static library name to generate 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 commondir = "../../winx64" #common directory to pul libraries and includes from
depdir = "./dependencies/winx64" #local pre-compiled dependency libraries and their includes depdir = "./dependencies/winx64" #local pre-compiled dependency libraries and their includes
installdir = "../../winx64" #directory to install to when finished installdir = "../../winx64" #directory to install to when finished
builddir = "./build_msvc64" 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 cc = "cl" #compiler
cflags = "/O2" cflags = "/O2 /EHsc"
libraries = "lib{}.lib".format(libname) libraries = "lib{}.lib libamsmathutil2.msvc64.lib".format(libname)
libdirs = "/LIBPATH:{} /LIBPATH:{}/lib /LIBPATH:{}/lib".format(builddir,commondir,depdir) libdirs = "/LIBPATH:{} /LIBPATH:{}/lib /LIBPATH:{}/lib".format(builddir,commondir,depdir)
linkerflags = "-static -static-libgcc -Wl,-rpath=." linkerflags = "-static -static-libgcc -Wl,-rpath=."
srcexts = [".c",".cpp"] srcexts = [".c",".cpp"]
@ -54,7 +54,7 @@ if(doinstall):
#Copy a binary to the common bin folder #Copy a binary to the common bin folder
#Push any libraries to the common lib 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 #Copy include files to the common include folder
copytree('./include/',commondir+'/include/',dirs_exist_ok=True) copytree('./include/',commondir+'/include/',dirs_exist_ok=True)

@ -14,12 +14,12 @@ depdir = "./dependencies/winx64" #local pre-compiled dependency libraries and th
installdir = "../../winx64" #directory to install to when finished installdir = "../../winx64" #directory to install to when finished
builddir = "./build_msvc64" 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 cc = "cl" #compiler
cflags = "/O2" cflags = "/O2 /EHsc"
libraries = "lib{}.lib".format(libname) libraries = "lib{}.lib libamsmathutil2.msvc64.lib".format(libname)
libdirs = "/LIBPATH:{} /LIBPATH:{}/lib /LIBPATH:{}/lib".format(builddir,commondir,depdir) libdirs = "/LIBPATH:{} /LIBPATH:{}/lib /LIBPATH:{}/lib".format(builddir,commondir,depdir)
linkerflags = "" linkerflags = "-static -static-libgcc -Wl,-rpath=."
srcexts = [".c",".cpp"] srcexts = [".c",".cpp"]
binsrc = ["main.c","main.cpp"] #ignore these files when compiling the static library binsrc = ["main.c","main.cpp"] #ignore these files when compiling the static library

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -86,6 +86,26 @@ template<typename T> class narray
T min() const; T min() const;
T max() 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 //subarray operations
narray<T> subarray(const narray_size_t ind1, const narray_size_t ind2) const; narray<T> subarray(const narray_size_t ind1, const narray_size_t ind2) const;
@ -103,6 +123,9 @@ template<typename T> class narray
const_iterator cbegin() const { return data; } const_iterator cbegin() const { return data; }
const_iterator cend() const { return data+length;} const_iterator cend() const { return data+length;}
//printf (must be template specialized)
void print(int printstyle = 0);
}; };
//Initializers //Initializers
@ -124,6 +147,9 @@ void test_narray1();
void test_narray2(); void test_narray2();
void test_narray3(); void test_narray3();
void test_insertdelete1();
}; //end namespace narray }; //end namespace narray
}; //end namespace ams }; //end namespace ams

@ -338,6 +338,186 @@ template<typename T> const narray_size_t narray<T>::size() const
return length; 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 narray
}; //end namespace ams }; //end namespace ams

@ -23,11 +23,11 @@ namespace rand
extern narray_rands_t dpr32_rseed; // global random number seed for numeric array class extern narray_rands_t dpr32_rseed; // global random number seed for numeric array class
narray_rands_t dpr32_nextseed(narray_rands_t seed); narray_rands_t dpr32_nextseed(narray_rands_t seed);
double dpr32_randd(narray_rands_t *seed); double dpr32_randd(narray_rands_t *seed = &(ams::narray::rand::dpr32_rseed));
float dpr32_randf(narray_rands_t *seed); float dpr32_randf(narray_rands_t *seed = &(ams::narray::rand::dpr32_rseed));
double dpr32_gaussian(narray_rands_t *seed); double dpr32_gaussian(narray_rands_t *seed = &(ams::narray::rand::dpr32_rseed));
float dpr32_gaussianf(narray_rands_t *seed); float dpr32_gaussianf(narray_rands_t *seed = &(ams::narray::rand::dpr32_rseed));
int dpr32_randint(narray_rands_t *seed, int low, int high); 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::vect2 dpr32_randuvect2(narray_rands_t *seed);
ams::vect3 dpr32_randuvect3(narray_rands_t *seed); ams::vect3 dpr32_randuvect3(narray_rands_t *seed);

@ -5,6 +5,65 @@ namespace ams
namespace narray namespace narray
{ {
template<> void narray<float>::print(const int printstyle)
{
narray_size_t I;
printf("{");
for(I=0;I<this->length-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<double>::print(const int printstyle)
{
narray_size_t I;
printf("{");
for(I=0;I<this->length-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<int>::print(const int printstyle)
{
narray_size_t I;
printf("{");
for(I=0;I<this->length-1;I++)
{
printf("%d,",this->data[I]);
}
if(this->length>=1) printf("%d",this->data[this->length-1]);
printf("}");
return;
}
template<> void narray<long>::print(const int printstyle)
{
narray_size_t I;
printf("{");
for(I=0;I<this->length-1;I++)
{
printf("%ld,",this->data[I]);
}
if(this->length>=1) printf("%ld",this->data[this->length-1]);
printf("}");
return;
}
void test_narray1() void test_narray1()
{ {
@ -110,6 +169,62 @@ void test_narray3()
} }
} }
void test_insertdelete1()
{
narray<int> 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 narray
}; //end namespace ams }; //end namespace ams

@ -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 ret = 0;
int val;
if(high-low>0) if(high-low>0)
{ {
*seed = dpr32_nextseed(*seed); *seed = dpr32_nextseed(*seed);
val = low + (int)(*seed)%(high-low); ret = low + (int)((*seed)%(high-low));
} }
return ret; return ret;
@ -472,7 +472,7 @@ namespace rand
template<typename T> void narray_rand_threadf2( template<typename T> void narray_rand_threadf2(
narray<T> *out, narray<T> *out,
narray<narray_rands_t> *rseeds, narray<narray_rands_t> *rseeds,
T (*randfunc)(narray_rands_t *, T, T), T (*randfunc)(T, T, narray_rands_t*),
T low, T low,
T highexcl, T highexcl,
int threadnum, int threadnum,
@ -485,7 +485,7 @@ namespace rand
I1 = (threadnum>=(nthreads-1)) ? out->length : Is*(threadnum+1); I1 = (threadnum>=(nthreads-1)) ? out->length : Is*(threadnum+1);
for(I=I0;I<I1;I++) for(I=I0;I<I1;I++)
{ {
out->data[I] = randfunc(&(rseeds->data[threadnum]),low,highexcl); out->data[I] = randfunc(low,highexcl,&(rseeds->data[threadnum]));
} }
return; return;
} }
@ -493,7 +493,7 @@ namespace rand
template<typename T> void narray_random_threadexec2( template<typename T> void narray_random_threadexec2(
narray<T> *out, narray<T> *out,
narray_size_t N, narray_size_t N,
T (*randfunc)(narray_rands_t *, T, T), T (*randfunc)(T, T, narray_rands_t *),
T low, T low,
T highexcl, T highexcl,
narray_rands_t *rseed narray_rands_t *rseed
@ -518,7 +518,7 @@ namespace rand
//single threaded //single threaded
for(I=0;I<N;I++) for(I=0;I<N;I++)
{ {
out->data[I] = randfunc(rseed,low,highexcl); out->data[I] = randfunc(low,highexcl,rseed);
} }
} }
else else

@ -8,8 +8,9 @@ int main(int argc, char* argv[])
// ams::narray::test_narray2(); // ams::narray::test_narray2();
// ams::narray::test_narray3(); // ams::narray::test_narray3();
// ams::narray::narray_testmath1(); // ams::narray::narray_testmath1();
ams::narray::narray_testmath2(); //ams::narray::narray_testmath2();
//ams::narray::narray_testmath3(); //ams::narray::narray_testmath3();
ams::narray::test_insertdelete1();
return ret; return ret;
} }
Loading…
Cancel
Save