void BigNumber::push_front(int num,const unsigned char c) {//Push multiple characters front if(!len){ cap=len=num+1; val=alloc.allocate(cap); first_free=val; dot=0; while(num--) alloc.construct(first_free++,c); alloc.construct(first_free++,'+'); }else{ int newcap=len+num; int newdot=dot; unsigned char *newval=alloc.allocate(newcap); unsigned char *newfirst_free=newval; unsigned char *oldval=val; while(num--) alloc.construct(newfirst_free++,c); while(oldval!=first_free) alloc.construct(newfirst_free++,*oldval++); release(); val=newval; first_free=newfirst_free; cap=len=newcap; dot=newdot; } }
String &String::operator+(const String &str) { size_t sz_temp = sz + str.sz; char *p = alloc.allocate(sz_temp); char *q = p; for(size_t i = 0;i < sz_temp; ++i) { if(i < sz) alloc.construct(q++,s[i]); else alloc.construct(q++,str.s[i-sz]); } free(); s = p; sz = sz_temp; return *this; }
void BigNumber::push_back(const unsigned char c) {//Push a character back if(!len||len>=cap){ int newcap=cap?(2*cap):2; int newdot=dot; unsigned char *newval=alloc.allocate(newcap); unsigned char *newfirst_free=newval; unsigned char *oldval=val; while(oldval!=first_free) alloc.construct(newfirst_free++,*oldval++); alloc.construct(newfirst_free++,c); release(); val=newval; first_free=newfirst_free; len=first_free-val; cap=newcap; dot=newdot; }else{alloc.construct(first_free++,c);len++;} }
BigNumber& BigNumber::reverse_copy(BigNumber &A) {//Reverse copy cap=len=A.len; val=alloc.allocate(cap); first_free=val; dot=0; unsigned char *begin=A.first_free; while(begin!=A.val){ alloc.construct(first_free++,*--begin); if(*begin=='.') dot=first_free-val-1; } }
String &String::operator=(const String &str) { char *p = alloc.allocate(str.sz); char *q = p; for(size_t i = 0;i < str.sz; ++i) alloc.construct(q++,str.s[i]); free(); s = p; sz = str.sz; return *this; }
BigNumber& BigNumber::operator=(long long num) {//Copy a long long integer release(); cap=21; val=alloc.allocate(cap); first_free=val; bool flag=true; long long e=num%10; num/=10; if(e<0) e=-e; if(num<0) {flag=false;num=-num;} alloc.construct(first_free++,e+48); while(num){ e=num%10; num/=10; alloc.construct(first_free++,e+48); } if(flag) alloc.construct(first_free++,'+'); else alloc.construct(first_free++,'-'); len=first_free-val; dot=0; }
void StrVec::reallocate() { auto newcapacity = size() ? 2 * size() : 1; auto newdata = alloc.allocate(newcapacity); auto dest = newdata; auto elem = elements; 0 for (size_t i = 0; i != size(); ++i) { alloc.construct(dest++, std::move(*elem++)); } free(); elements = newdata; first_free = dest; cap = elements + newcapacity; }
BigNumber& BigNumber::operator=(const BigNumber &A) {//Copy a BigNumber int newcap = A.len; unsigned char *newval = alloc.allocate(newcap); unsigned char *newfirst_free = newval; int newlen = A.len; int newdot = A.dot; int count = 0; while(count < A.len) alloc.construct(newfirst_free++,A.val[count++]); release(); cap=newcap; val=newval; first_free=newfirst_free; len=newlen; dot=newdot; return *this; }
BigNumber& BigNumber::dot_erase_copy(BigNumber &A) {//Copy BigNumber without dot before division release(); cap = A.dot ? (A.len - 1) : A.len; len = cap; dot = 0; val=alloc.allocate(cap); first_free = val; int count = 0; while(count < A.len){ alloc.construct(first_free++,A.val[count]); if(A.val[++count]=='.') if(++count == (A.len - 2) && A.val[count] == '0'){ ++count; --len; } } return *this; }
void BigNumber::cut_tail(int num) { int newcap = num + 3; int newdot; unsigned char *newval = alloc.allocate(newcap); unsigned char *newfirst_free = newval; unsigned char *oldval = val; while((first_free - oldval) != newcap) ++oldval; unsigned char *start = oldval; while(oldval != first_free){ if(*oldval == '.') newdot = oldval - start; alloc.construct(newfirst_free++,*oldval++); } release(); val=newval; first_free=newfirst_free; len=first_free-val; cap=newcap; dot=newdot; }
void StrVec::push_back(const string& s) { chk_n_alloc(); alloc.construct(first_free++, s); }
static T* create() { T* p = alloc_.allocate(1); if (!p) return nullptr; alloc_.construct(p); return p; };