1 template <class T
> int32_t get_vector_sum(svector
<T
> &vec
)
4 auto ii_s
=vec
.begin(),ii_e
=vec
.end();
12 template <class T
> int32_t get_random_biased_index(svector
<T
> &chance
)
16 errorlog_string("Empty biased index vector");
20 T roll
=trandom(get_vector_sum(chance
));
22 auto ii_s
=chance
.begin(),ii_e
=chance
.end();
24 for(;ii_s
<ii_e
;++ii_s
)
26 if(roll
<(*ii_s
))return (int32_t)(ii_s
-ii_b
);
30 errorlog_string("Biased index vector computation error");
31 return 0;//MORE FUNCTIONS WILL BE HAPPIER WITH 0 THAN -1 HERE
34 template <class T
> void zero_vector(svector
<T
> &vc
)
36 //NOTE: vector MEMORY IS GUARANTEED TO BE CONTIGUOUS, AND THIS IS FASTER THAN GOING THROUGH ONE BY ONE
37 //apparently this gives linux a headache though, so back to the slower way
38 //int32_t sz=vc.size();
40 //memset(&(vc[0]),0,sizeof(T)*sz);
41 auto ii_s
=vc
.begin(),ii_e
=vc
.end();
42 for(;ii_s
<ii_e
;++ii_s
)(*ii_s
)=0;
45 template <class T
> bool positive_vector(svector
<T
> &vc
)
47 auto ii_s
=vc
.begin(),ii_e
=vc
.end();
48 for(;ii_s
<ii_e
;++ii_s
)
50 if((*ii_s
)>0)return true;
55 template <class T
> void add_unique_to_vector(T nl
,svector
<T
> &vc
)
57 auto ii_s
=vc
.begin(),ii_e
=vc
.end();
58 for(;ii_s
<ii_e
;++ii_s
)
60 if((*ii_s
)==nl
)return;
65 template <class T
,class T2
> void add_dual_unique_to_vectors(T nl
,T2 nl2
,svector
<T
> &vc
,svector
<T2
> &vc2
)
67 auto ii_s
=vc
.begin(),ii_e
=vc
.end();
68 auto ii_s2
=vc2
.begin(),ii_e2
=vc2
.end();
69 for(;ii_s
<ii_e
;++ii_s
,++ii_s2
)
78 template <class T
,class T2
,class T3
,class T4
> void add_quad_unique_to_vectors(T nl
,T2 nl2
,T3 nl3
,T4 nl4
,
79 svector
<T
> &vc
,svector
<T2
> &vc2
,svector
<T3
> &vc3
,svector
<T4
> &vc4
)
81 auto ii_s
=vc
.begin(),ii_e
=vc
.end();
82 auto ii_s2
=vc2
.begin(),ii_e2
=vc2
.end();
83 auto ii_s3
=vc3
.begin(),ii_e3
=vc3
.end();
84 auto ii_s4
=vc4
.begin(),ii_e4
=vc4
.end();
85 for(;ii_s
<ii_e
;++ii_s
,++ii_s2
,++ii_s3
,++ii_s4
)
98 template <class T
> void remove_all_from_vector(T nl
,svector
<T
> &vc
)
101 for(i
=(int32_t)vc
.size()-1;i
>=0;i
--)
103 if(vc
[i
]==nl
)vc
.erase(i
);
107 template <class T
,class T2
> void remove_all_from_dual_vectors(T nl
,T2 nl2
,svector
<T
> &vc
,svector
<T2
> &vc2
)
110 for(i
=(int32_t)vc
.size()-1;i
>=0;i
--)
121 template <class T
> int32_t get_vector_index(T a
,svector
<T
> &v
)
123 auto ii_s
=v
.begin(),ii_e
=v
.end();
125 for(;ii_s
<ii_e
;++ii_s
)
127 if((*ii_s
)==a
)return (int32_t)(ii_s
-ii_b
);
132 template <class T
,class T2
> int32_t get_dual_vector_index(T a1
,T2 a2
,svector
<T
> &vc
,svector
<T2
> &vc2
)
134 auto ii_s
=vc
.begin(),ii_e
=vc
.end();
135 auto ii_s2
=vc2
.begin(),ii_e2
=vc2
.end();
137 for(;ii_s
<ii_e
;++ii_s
,++ii_s2
)
140 (*ii_s2
)==a2
)return (int32_t)(ii_s
-ii_b
);
145 template <class T
,class T2
,class T3
,class T4
> int32_t get_quad_vector_index(T a1
,T2 a2
,T3 a3
,T4 a4
,
146 svector
<T
> &vc
,svector
<T2
> &vc2
,
147 svector
<T3
> &vc3
,svector
<T4
> &vc4
)
149 auto ii_s
=vc
.begin(),ii_e
=vc
.end();
150 auto ii_s2
=vc2
.begin(),ii_e2
=vc2
.end();
151 auto ii_s3
=vc3
.begin(),ii_e3
=vc3
.end();
152 auto ii_s4
=vc4
.begin(),ii_e4
=vc4
.end();
154 for(;ii_s
<ii_e
;++ii_s
,++ii_s2
,++ii_s3
,++ii_s4
)
159 (*ii_s4
)==a4
)return (int32_t)(ii_s
-ii_b
);
164 template <class T
> void merge_vectors(T
&master
, T
&merger
)
166 auto ii_s
=merger
.begin(),ii_e
=merger
.end();
167 for(;ii_s
<ii_e
;++ii_s
)
169 auto ii_s2
=master
.begin(),ii_e2
=master
.end();
170 for(;ii_s2
<ii_e2
;++ii_s2
)
172 if((*ii_s
)==(*ii_s2
))break;
176 master
.push_back((*ii_s
));
181 template <class T
> int32_t get_common_element_vector_index(T
&master
, T
&merger
)
183 auto ii_s
=merger
.begin(),ii_e
=merger
.end();
184 auto ii_s2
=master
.begin(),ii_e2
=master
.end();
187 for(;ii_s
<ii_e
;++ii_s
)
189 for(;ii_s2
<ii_e2
;++ii_s2
)
191 if((*ii_s
)==(*ii_s2
))return (int32_t)(ii_s2
-ii_b2
);
199 template <class T
,class T2
> void merge_dual_vectors(T
&master
, T2
&master2
, T
&merger
, T2
&merger2
)
201 auto ii_s
=merger
.begin(),ii_e
=merger
.end();
202 auto ii_s2
=merger2
.begin(),ii_e2
=merger2
.end();
203 for(;ii_s
<ii_e
;++ii_s
,++ii_s2
)
205 auto ii_s3
=master
.begin(),ii_e3
=master
.end();
206 auto ii_s4
=master2
.begin(),ii_e4
=master2
.end();
207 for(;ii_s3
<ii_e3
;++ii_s3
,++ii_s4
)
209 if((*ii_s
)==(*ii_s3
)&&
210 (*ii_s2
)==(*ii_s4
))break;
214 master
.push_back((*ii_s
));
215 master2
.push_back((*ii_s2
));
220 template <class T
,class T2
,class T3
,class T4
> void merge_quad_vectors(T
&master
, T2
&master2
, T3
&master3
, T4
&master4
,
221 T
&merger
, T2
&merger2
, T3
&merger3
, T4
&merger4
)
223 auto ii_s
=merger
.begin(),ii_e
=merger
.end();
224 auto ii_s2
=merger2
.begin(),ii_e2
=merger2
.end();
225 auto ii_s3
=merger3
.begin(),ii_e3
=merger3
.end();
226 auto ii_s4
=merger4
.begin(),ii_e4
=merger4
.end();
227 for(;ii_s
<ii_e
;++ii_s
,++ii_s2
,++ii_s3
,++ii_s4
)
229 auto ii_s5
=master
.begin(),ii_e5
=master
.end();
230 auto ii_s6
=master2
.begin(),ii_e6
=master2
.end();
231 auto ii_s7
=master3
.begin(),ii_e7
=master3
.end();
232 auto ii_s8
=master4
.begin(),ii_e8
=master4
.end();
233 for(;ii_s5
<ii_e5
;++ii_s5
,++ii_s6
,++ii_s7
,++ii_s8
)
235 if((*ii_s
)==(*ii_s5
)&&
238 (*ii_s4
)==(*ii_s8
))break;
242 master
.push_back((*ii_s
));
243 master2
.push_back((*ii_s2
));
244 master3
.push_back((*ii_s3
));
245 master4
.push_back((*ii_s4
));
250 template <class T
> void cull_vectors(T
&master
,T
&cull
)
253 for(i
=(int32_t)cull
.size()-1;i
>=0;i
--)
255 for(i2
=(int32_t)master
.size()-1;i2
>=0;i2
--)
257 if(cull
[i
]==master
[i2
])
266 template <class T
> void push_on_vector(T
&master
,T
&new_stuff
)
268 auto ii_s
=new_stuff
.begin(),ii_e
=new_stuff
.end();
269 for(;ii_s
<ii_e
;++ii_s
)
271 master
.push_back(*ii_s
);
275 template<class T
> VIndex
add_to_global_id_vector(T ptr
,svector
<T
> &vect
)
277 int32_t size
=vect
.size();
283 if(vect
[size
-1]->global_id
<ptr
->global_id
)
298 if(cptr
->global_id
==ptr
->global_id
)return -1;
301 if(cptr
->global_id
<ptr
->global_id
)
303 if(start
+1>=size
)return -1;//push_back() FOR UNIQUE ALREADY HANDLED
306 vect
.insert(start
+1,ptr
);
310 else if(cptr
->global_id
>ptr
->global_id
)
312 vect
.insert(start
,ptr
);
318 if(cptr
->global_id
>ptr
->global_id
)end
=mid
-1;
325 if(cptr
->global_id
<ptr
->global_id
)
327 if(start
+1>=size
)return -1;//push_back() FOR UNIQUE ALREADY HANDLED
330 vect
.insert(start
+1,ptr
);
334 else if(cptr
->global_id
>ptr
->global_id
)
336 vect
.insert(start
,ptr
);
343 if(cptr
->global_id
<ptr
->global_id
)
345 if(end
+1>=size
)return -1;//push_back() FOR UNIQUE ALREADY HANDLED
348 vect
.insert(end
+1,ptr
);
352 else if(cptr
->global_id
>ptr
->global_id
)
354 vect
.insert(end
,ptr
);
358 return -1;//push_back() FOR UNIQUE ALREADY HANDLED SO NO else
361 template<class T
> void remove_from_global_id_vector(T ptr
,svector
<T
> &vect
)
364 int32_t end
=(int32_t)vect
.size()-1;
378 if(cptr
->global_id
>ptr
->global_id
)end
=mid
-1;
383 template<class T
> void remove_from_global_id_vector_by_id(int32_t id
,svector
<T
> &vect
)
386 int32_t end
=(int32_t)vect
.size()-1;
394 if(cptr
->global_id
==id
)
400 if(cptr
->global_id
>id
)end
=mid
-1;
405 template<class T
> T
get_from_global_id_vector(int32_t id
,svector
<T
> &vect
)
408 int32_t end
=(int32_t)vect
.size()-1;
417 if(cptr
->global_id
==id
)return cptr
;
418 else if(cptr
->global_id
>id
)end
=mid
-1;
425 template<class T
> int32_t get_index_from_global_id_vector(int32_t id
,svector
<T
> &vect
)
428 int32_t end
=(int32_t)vect
.size()-1;
437 if(cptr
->global_id
==id
)return mid
;
438 else if(cptr
->global_id
>id
)end
=mid
-1;
445 template<class T
> VIndex
add_to_binary_vector(T ptr
,svector
<T
> &vect
)
447 int32_t size
=vect
.size();
471 vect
.insert(mid
,ptr
);//INSERT A COPY HERE SINCE THIS IS A NON-UNIQUE VECTOR
478 if(start
+1>=size
)vect
.push_back(ptr
);
479 else vect
.insert(start
+1,ptr
);
484 vect
.insert(start
,ptr
);
489 if(cptr
>ptr
)end
=mid
-1;
498 if(start
+1>=size
)vect
.push_back(ptr
);
499 else vect
.insert(start
+1,ptr
);
504 vect
.insert(start
,ptr
);
513 if(end
+1>=size
)vect
.push_back(ptr
);
514 else vect
.insert(end
+1,ptr
);
519 vect
.insert(end
,ptr
);
523 else vect
.push_back(ptr
);
527 //NOTE: RETURNS -1 IF ALREADY PRESENT, NOT THE INDEX
528 template<class T
> VIndex
add_unique_to_binary_vector(T ptr
,svector
<T
> &vect
)
530 int32_t size
=vect
.size();
552 if(cptr
==ptr
)return -1;//ALREADY IN VECTOR
557 if(start
+1>=(int32_t)vect
.size())return -1;//push_back() CASE ALREADY HANDLED
559 vect
.insert(start
+1,ptr
);
564 vect
.insert(start
,ptr
);
570 if(cptr
>ptr
)end
=mid
-1;
579 if(start
+1>=size
)return -1;//push_back() CASE ALREADY HANDLED
581 vect
.insert(start
+1,ptr
);
586 vect
.insert(start
,ptr
);
595 if(end
+1>=size
)return -1;//push_back() CASE ALREADY HANDLED
597 vect
.insert(end
+1,ptr
);
602 vect
.insert(end
,ptr
);
606 return -1;//push_back() CASE ALREADY HANDLED SO NO else
609 //NOTE: RETURNS INDEX IF ALREADY PRESENT
610 template<class T
> VIndex
add_unique_to_binary_vector_always_index(T ptr
,svector
<T
> &vect
,bool &was_present
)
614 int32_t size
=vect
.size();
636 if(cptr
==ptr
){was_present
=true;return mid
;}//ALREADY IN VECTOR
641 if(start
+1>=(int32_t)vect
.size())return -1;//push_back() CASE ALREADY HANDLED
643 vect
.insert(start
+1,ptr
);
648 vect
.insert(start
,ptr
);
654 if(cptr
>ptr
)end
=mid
-1;
663 if(start
+1>=size
)return -1;//push_back() CASE ALREADY HANDLED
665 vect
.insert(start
+1,ptr
);
670 vect
.insert(start
,ptr
);
673 if(cptr
==ptr
){was_present
=true;return start
;}//ALREADY IN VECTOR
680 if(end
+1>=size
)return -1;//push_back() CASE ALREADY HANDLED
682 vect
.insert(end
+1,ptr
);
687 vect
.insert(end
,ptr
);
690 if(cptr
==ptr
){was_present
=true;return end
;}//ALREADY IN VECTOR
692 return -1;//push_back() CASE ALREADY HANDLED SO NO else
695 template<class T
> void remove_from_binary_vector(T ptr
,svector
<T
> &vect
)
698 int32_t end
=(int32_t)vect
.size()-1;
713 if(cptr
>ptr
)end
=mid
-1;
718 template<class T
> int32_t get_index_from_binary_vector(T id
,svector
<T
> &vect
)
720 int32_t size
=vect
.size();
721 if(size
==0||id
==-1)return -1;
738 if(cptr
>id
)end
=mid
-1;
745 template<class T
> int32_t get_floor_index_from_binary_vector(T ptr
,svector
<T
> &vect
)
747 int32_t size
=vect
.size();
748 if(size
==0)return -1;
749 if(vect
[size
-1]<ptr
)return size
-1;
765 if(vect
[mid
-1]<ptr
)return mid
;
774 if(start
+1>=size
)return start
;
777 //NOTE: cptr>ptr as == is already handled above
780 if(start
<=0)return 0;
785 if(cptr
>ptr
)end
=mid
-1;
794 if(start
+1>=size
)return start
;
799 if(start
<=0)return 0;
808 if(end
+1>=size
)return end
;
820 template<class T
> void fixed_array_push_back(T ptr
,T
*vect
,int32_t &size
,int32_t max
)
827 template<class T
> void fixed_array_insert(int32_t index
,T ptr
,T
*vect
,int32_t &size
,int32_t max
)
833 T
*ptr2
=vect
+(size
-1);
851 template<class T
> void add_to_fixed_binary_array(T ptr
,T
*vect
,int32_t &size
,int32_t max
)
855 fixed_array_push_back(ptr
,vect
,size
,max
);
860 fixed_array_push_back(ptr
,vect
,size
,max
);
876 fixed_array_insert(mid
,ptr
,vect
,size
,max
);//INSERT A COPY SINCE THIS IS NON-UNIQUE CASE
883 if(start
+1>=size
)fixed_array_push_back(ptr
,vect
,size
,max
);
884 else fixed_array_insert(start
+1,ptr
,vect
,size
,max
);
886 else fixed_array_insert(start
,ptr
,vect
,size
,max
);
890 if(cptr
>ptr
)end
=mid
-1;
899 if(start
+1>=size
)fixed_array_push_back(ptr
,vect
,size
,max
);
900 else fixed_array_insert(start
+1,ptr
,vect
,size
,max
);
902 else fixed_array_insert(start
,ptr
,vect
,size
,max
);
909 if(end
+1>=size
)fixed_array_push_back(ptr
,vect
,size
,max
);
910 else fixed_array_insert(end
+1,ptr
,vect
,size
,max
);
912 else fixed_array_insert(end
,ptr
,vect
,size
,max
);
914 else fixed_array_push_back(ptr
,vect
,size
,max
);
917 template<class T
> void add_unique_to_fixed_binary_array(T ptr
,T
*vect
,int32_t &size
,int32_t max
)
921 fixed_array_push_back(ptr
,vect
,size
,max
);
926 fixed_array_push_back(ptr
,vect
,size
,max
);
945 if(start
+1>=size
)return;//WAS push_back()
946 else fixed_array_insert(start
+1,ptr
,vect
,size
,max
);
948 else if(cptr
>ptr
)fixed_array_insert(start
,ptr
,vect
,size
,max
);
952 if(cptr
>ptr
)end
=mid
-1;
961 if(start
+1>=size
)return;//WAS push_back()
962 else fixed_array_insert(start
+1,ptr
,vect
,size
,max
);
964 else if(cptr
>ptr
)fixed_array_insert(start
,ptr
,vect
,size
,max
);
971 if(end
+1>=size
)return;//WAS push_back()
972 else fixed_array_insert(end
+1,ptr
,vect
,size
,max
);
974 else if(cptr
>ptr
)fixed_array_insert(end
,ptr
,vect
,size
,max
);
976 //NOTE: NO else CASE HERE BECAUSE IN THE NON-UNIQUE VERSION IT JUST PUSHED BACK
977 //AND PUSHING BACK IS HANDLED IN THE OPENING FUNCTION
980 template<class T
> void add_to_local_id_vector(T ptr
,svector
<T
> &vect
)
982 int32_t size
=vect
.size();
988 if(vect
[size
-1]->local_id
<ptr
->local_id
)
1004 if(cptr
->local_id
==ptr
->local_id
)return;
1007 if(cptr
->local_id
<ptr
->local_id
)
1009 if(start
+1>=size
)return;//push_back() FOR UNIQUE ALREADY HANDLED
1010 else vect
.insert(start
+1,ptr
);
1012 else if(cptr
->local_id
>ptr
->local_id
)vect
.insert(start
,ptr
);
1016 if(cptr
->local_id
>ptr
->local_id
)end
=mid
-1;
1023 if(cptr
->local_id
<ptr
->local_id
)
1025 if(start
+1>=size
)return;//push_back() FOR UNIQUE ALREADY HANDLED
1026 else vect
.insert(start
+1,ptr
);
1028 else if(cptr
->local_id
>ptr
->local_id
)vect
.insert(start
,ptr
);
1033 if(cptr
->local_id
<ptr
->local_id
)
1035 if(end
+1>=size
)return;//push_back() FOR UNIQUE ALREADY HANDLED
1036 else vect
.insert(end
+1,ptr
);
1038 else if(cptr
->local_id
>ptr
->local_id
)vect
.insert(end
,ptr
);
1042 template<class T
> void remove_from_local_id_vector(T ptr
,svector
<T
> &vect
)
1045 int32_t end
=(int32_t)vect
.size()-1;
1060 if(cptr
->local_id
>ptr
->local_id
)end
=mid
-1;
1065 template<class T
> T
get_from_local_id_vector(int32_t id
,svector
<T
> &vect
)
1068 int32_t end
=(int32_t)vect
.size()-1;
1077 if(cptr
->local_id
==id
)return cptr
;
1078 else if(cptr
->local_id
>id
)end
=mid
-1;
1085 template<class T
> int32_t get_index_from_local_id_vector(int32_t id
,svector
<T
> &vect
)
1088 int32_t end
=(int32_t)vect
.size()-1;
1097 if(cptr
->local_id
==id
)return mid
;
1098 else if(cptr
->local_id
>id
)end
=mid
-1;
1105 template<class T
> void add_to_short_id_vector(T ptr
,svector
<T
> &vect
)
1107 int16_t size
=vect
.size();
1110 vect
.push_back(ptr
);
1113 if(vect
[size
-1]->short_id
<ptr
->short_id
)
1115 vect
.push_back(ptr
);
1129 if(cptr
->short_id
==ptr
->short_id
)return;
1132 if(cptr
->short_id
<ptr
->short_id
)
1134 if(start
+1>=size
)return;//push_back() FOR UNIQUE ALREADY HANDLED
1135 else vect
.insert(start
+1,ptr
);
1137 else if(cptr
->short_id
>ptr
->short_id
)vect
.insert(start
,ptr
);
1141 if(cptr
->short_id
>ptr
->short_id
)end
=mid
-1;
1148 if(cptr
->short_id
<ptr
->short_id
)
1150 if(start
+1>=size
)return;//push_back() FOR UNIQUE ALREADY HANDLED
1151 else vect
.insert(start
+1,ptr
);
1153 else if(cptr
->short_id
>ptr
->short_id
)vect
.insert(start
,ptr
);
1158 if(cptr
->short_id
<ptr
->short_id
)
1160 if(end
+1>=size
)return;//push_back() FOR UNIQUE ALREADY HANDLED
1161 else vect
.insert(end
+1,ptr
);
1163 else if(cptr
->short_id
>ptr
->short_id
)vect
.insert(end
,ptr
);
1167 template<class T
> void remove_from_short_id_vector(T ptr
,svector
<T
> &vect
)
1170 int16_t end
=(int16_t)vect
.size()-1;
1175 int16_t mid
=(start
+end
)>>1;
1184 if(cptr
->short_id
>ptr
->short_id
)end
=mid
-1;
1189 template<class T
> T
get_from_short_id_vector(int16_t id
,svector
<T
> &vect
)
1192 int16_t end
=(int16_t)vect
.size()-1;
1201 if(cptr
->short_id
==id
)
1206 if(cptr
->short_id
>id
)end
=mid
-1;
1213 template<class T
> int16_t get_index_from_short_id_vector(int16_t id
,svector
<T
> &vect
)
1216 int16_t end
=(int16_t)vect
.size()-1;
1225 if(cptr
->short_id
==id
)
1230 if(cptr
->short_id
>id
)end
=mid
-1;