Browse Source

Resed trash bigint and bitset

mawinkle 6 years ago
parent
commit
469718f91b
3 changed files with 6 additions and 439 deletions
  1. 2 122
      BigInt.hpp
  2. 0 313
      semi_bitset.hpp
  3. 4 4
      test.cpp

+ 2 - 122
BigInt.hpp

@@ -1,123 +1,3 @@
-#ifndef BIG_INT_HPP
-#define BIG_INT_HPP
-#include "semi_bitset.hpp"
-#include <cstdlib>
-#include <string>
-#include <iostream>
-#include <cassert>
-template<size_t size = dynamic>
-class BigInt{
-private:
-	int signum = 1;
-	semi_bitset<size> bits;
-public:
-	inline BigInt() : bits(1){}
-	inline BigInt(size_t _size, bool dummy) : bits(_size){
-		
-	}
-	inline BigInt(int o) : BigInt(2,false){
-		if(o < 0){signum = -1;o *= -1;}
-		bits.data[bits.length() - 1] = (unsigned int)o;
-	}
-	inline BigInt(unsigned int o) : BigInt(2,false){
-		bits.data[bits.length() - 1] = o;
-	}
-	inline BigInt(long long o) : BigInt(3,false){
-		if(o < 0){signum = -1;o = 0ULL - o;}
-		bits.data[bits.length() - 1] = (o & lower_half);
-		bits.data[bits.length() - 2] = (o >> 32);
-	}
-	inline BigInt(unsigned long long o) : BigInt(3,false){
-		bits.data[bits.length() - 1] = (o & lower_half);
-		bits.data[bits.length() - 2] = (o >> 32);
-	}
-	template<std::size_t osize>
-	inline BigInt<size> add(const BigInt<osize>& o)const{
-		static_assert(size == osize);
-		BigInt<size> ret;
-		uint64_t carry = 0;
-		for(size_t i = size - 1;i >= 0;i--){
-			uint64_t res = (*this)[i] + o[i] + carry;
-			carry = (res >> 32);
-			ret[i] = res & lower_half;
-			if(i == 0)break;
-		}
-		return ret;
-	}
-	template<size_t osize>
-	inline BigInt<size + osize> mult(const BigInt<osize>& o)const{
-		BigInt<size + osize> ret;
-		uint64_t mat[size][size] = {0};
-		uint64_t carry = 0;
-		for(size_t i = 0;i < size;i++){
-			for(size_t ih = size - 1;ih != ~(0ULL);ih--){
-				mat[size - i - 1][ih] = (*this)[i] * o[ih] + carry;
-				carry = mat[size - i - 1][ih] >> 32;
-				mat[size - i - 1][ih] &= lower_half;
-				if(ih == 0)break;
-			}
-		}
-		for(size_t i = 0;i < size;i++){
-			for(size_t ih = 0;ih < size + osize;ih++){
-				std::cout << mat[i][ih] << " ";
-			}
-			std::cout << std::endl;
-		}
-		carry = 0;
-		for(std::int64_t i = size - 1;i > -((std::int64_t)size);i--){
-			uint64_t accum = 0;
-			for(std::int64_t ih = 0;ih < size;ih++){
-				accum += ((i + ih) >= size || (i + ih) < 0) ?  0 : mat[ih][i + ih];
-			}
-			accum += carry;
-			ret[i + osize] = accum & lower_half;
-			//std::cout << accum << " ";
-			carry = accum >> 32;
-			if(i == 0)break;
-		}
-		return ret;
-	}
-	
-	inline BigInt<size> div(unsigned int o)const{
-		uint64_t carry = 0;
-		BigInt<size> ret;
-		for(size_t i = 0;i < size;i++){
-			uint64_t res = ((*this)[i] + (carry << 32)) / o;
-			carry = (*this)[i] % o;
-			ret[i] = res;
-		}
-		return ret;
-	}
-	inline unsigned int mod(unsigned int o)const{
-		uint64_t carry = 0;
-		for(size_t i = 0;i < size;i++){
-			carry = (this->operator[](i) + (carry << 32)) % o;
-		}
-		return (unsigned int)(carry & lower_half);
-	}
-	inline std::uint64_t& operator[](size_t i){
-		return bits.data[i];
-	}
-	
-	inline const std::uint64_t& operator[](size_t i)const{
-		return bits.data[i];
-	}
-	inline bool isZero()const{
-		for(size_t i = 0;i < size;i++){
-			if(this->operator[](i))return false;
-		}
-		return true;
-	}
-	
-};
-template<size_t osize>
-inline std::ostream& operator<<(std::ostream& s, const BigInt<osize>& o){
-	std::string a = "";
-	BigInt<osize> dis = o;
-	while(!dis.isZero()){
-		a = std::to_string(dis.mod(10)) + a;
-		dis = dis.div(10);
-	}
-	return s << a;
-}
+#ifndef BIGINT_HPP
+#define BIGINT_HPP
 #endif

+ 0 - 313
semi_bitset.hpp

@@ -1,313 +0,0 @@
-#ifndef SEMI_BITSET_HPP
-#define SEMI_BITSET_HPP
-#include <cstdint>
-#include <vector>
-#include <bitset>
-#include <limits>
-using std::size_t;
-static const size_t dynamic = std::numeric_limits<size_t>::max();
-static const std::uint64_t lower_half = 0x00000000FFFFFFFF;
-template<size_t size_>
-struct semi_bitset{
-	using uint64_t = std::uint64_t;
-	using uint32_t = std::uint32_t;
-	size_t size = size_;
-	inline size_t length(){return size;}
-	uint64_t data[size_] = {0};
-	inline semi_bitset(size_t s){}
-	inline uint64_t chunk(size_t i){
-		return data[i];
-	}
-};
-template<>
-struct semi_bitset<dynamic>{
-	using uint64_t = std::uint64_t;
-	using uint32_t = std::uint32_t;
-	size_t size;
-	std::vector<uint64_t> data;
-	inline semi_bitset(size_t _size) : size(_size), data(size, 0){}
-	inline size_t length(){return size;};
-	inline uint64_t chunk(size_t i){
-		return data[i];
-	}
-};
-/*template<size_t size>
-inline semi_bitset<size> operator<<(const semi_bitset<size>& set, int o)const{
-	if(o < 0){return this->operator>>(-o);}
-	if(o == 0){return *this;}
-	semi_bitset<size> ret;
-	unsigned int jump = o / 32;
-	unsigned int mod = o % 32;
-	unsigned int counter = 32 - (o % 32);
-	for(size_t i = jump;i < size;i++){
-		if(i < (size - 1))
-			ret.data[i - jump] = ((data[i] << mod) & lower_half) | (data[i + 1] >> counter);
-		else
-			ret.data[i - jump] = ((data[i] << mod) & lower_half);
-	}
-	return ret;
-}
-*/
-/*template<size_t size, bool containertype>
-struct semi_bitset{
-	using uint64_t = std::uint64_t;
-	using uint32_t = std::uint32_t;
-	std::vector<uint64_t> data;
-	inline semi_bitset() : data(size, 0){}
-	inline size_t length()const{
-		return size;
-	}
-	inline semi_bitset<size, containertype> operator<<(int o)const{
-		if(o < 0){return this->operator>>(-o);}
-		if(o == 0){return *this;}
-		semi_bitset<size, containertype> ret;
-		unsigned int jump = o / 32;
-		unsigned int mod = o % 32;
-		unsigned int counter = 32 - (o % 32);
-		for(size_t i = jump;i < size;i++){
-			if(i < (size - 1))
-				ret.data[i - jump] = ((data[i] << mod) & lower_half) | (data[i + 1] >> counter);
-			else
-				ret.data[i - jump] = ((data[i] << mod) & lower_half);
-		}
-		return ret;
-	}
-	inline semi_bitset<size, containertype> operator>>(int o)const{
-		if(o < 0){return this->operator<<(-o);}
-		if(o == 0){return *this;}
-		semi_bitset<size, containertype> ret;
-		unsigned int jump = o / 32;
-		unsigned int mod = o % 32;
-		unsigned int counter = 32 - (o % 32);
-		for(size_t i = jump;i < size;i++){
-			if(i > jump)
-				ret.data[i] = (data[i - jump] >> mod) | ((data[i - jump - 1] << counter) & lower_half);
-			else
-				ret.data[i] = (data[i - jump] >> mod);
-		}
-		return ret;
-	}
-	inline semi_bitset<size, containertype>& operator<<=(int o){
-		if(o < 0){return this->operator>>=(-o);}
-		if(o == 0){return *this;}
-		unsigned int jump = o / 32;
-		unsigned int mod = o % 32;
-		unsigned int counter = 32 - (o % 32);
-		for(size_t i = jump;i < size;i++){
-			if(i < (size - 1))
-				data[i - jump] = ((data[i] << mod) & lower_half) | (data[i + 1] >> counter);
-			else
-				data[i - jump] = ((data[i] << mod) & lower_half);
-		}
-		for(size_t i = size - jump;i < size;i++){
-			data[i] = 0;
-		}
-		return *this;
-	}
-	inline semi_bitset<size, containertype>& operator>>=(int o){
-		if(o < 0){return this->operator<<=(-o);}
-		if(o == 0){return *this;}
-		unsigned int jump = o / 32;
-		unsigned int mod = o % 32;
-		unsigned int counter = 32 - (o % 32);
-		for(size_t i = size - 1;i >= jump;i--){
-			if(i > jump)
-				data[i] = (data[i - jump] >> mod) | ((data[i - jump - 1] << counter) & lower_half);
-			else
-				data[i] = (data[i - jump] >> mod);
-		}
-		if(jump > 0)
-		for(size_t i = jump - 1;i >= 0;i--){
-			data[i] = 0;
-			if(i == 0)break;
-		}
-		return *this;
-	}
-	template<size_t osize, bool ocontainertype>
-	inline semi_bitset<size, containertype> operator&(const semi_bitset<osize, ocontainertype>& o)const{
-		static_assert(size == osize);
-		semi_bitset<size, containertype> ret;
-		for(size_t i = 0;i < size;i++){
-			ret.data[i] = (data[i] & o.data[i]);
-		}
-		return ret;
-	}
-	template<size_t osize, bool ocontainertype>
-	inline semi_bitset<size, containertype> operator^(const semi_bitset<osize, ocontainertype>& o)const{
-		static_assert(size == osize);
-		semi_bitset<size, containertype> ret;
-		for(size_t i = 0;i < size;i++){
-			ret.data[i] = (data[i] ^ o.data[i]);
-		}
-		return ret;
-		
-	}
-	template<size_t osize, bool ocontainertype>
-	inline semi_bitset<size, containertype> operator|(const semi_bitset<osize, ocontainertype>& o)const{
-		static_assert(size == osize);
-		semi_bitset<size, containertype> ret;
-		for(size_t i = 0;i < size;i++){
-			ret.data[i] = (data[i] | o.data[i]);
-		}
-		return ret;
-	}
-	template<size_t osize, bool ocontainertype>
-	inline semi_bitset<size, containertype>& operator&=(const semi_bitset<osize, ocontainertype>& o){
-		static_assert(size == osize);
-		for(size_t i = 0;i < size;i++){
-			data &= o.data[i];
-		}
-		return *this;
-	}
-	template<size_t osize, bool ocontainertype>
-	inline semi_bitset<size, containertype>& operator^=(const semi_bitset<osize, ocontainertype>& o){
-		static_assert(size == osize);
-		for(size_t i = 0;i < size;i++){
-			data ^= o.data[i];
-		}
-		return *this;
-	}
-	template<size_t osize, bool ocontainertype>
-	inline semi_bitset<size, containertype>& operator|=(const semi_bitset<osize, ocontainertype>& o){
-		static_assert(size == osize);
-		for(size_t i = 0;i < size;i++){
-			data |= o.data[i];
-		}
-		return *this;
-	}
-	template<typename stream, size_t osize,bool ocontainertype>
-	inline friend stream& operator<<(stream& s, const semi_bitset<osize, ocontainertype>& o){
-		for(size_t i = 0;i < o.data.size();i++)
-			s << std::bitset<32>((uint32_t)(o.data[i] & lower_half));
-		return s;
-	}
-};
-template<std::size_t size>
-struct semi_bitset<size, 0>{
-    using uint64_t = std::uint64_t;
-	using uint32_t = std::uint32_t;
-    uint64_t data[size] = {0};
-	static const bool containertype = 0;
-	inline semi_bitset<size, containertype> operator<<(int o)const{
-		if(o < 0){return this->operator>>(-o);}
-		if(o == 0){return *this;}
-		semi_bitset<size, containertype> ret;
-		unsigned int jump = o / 32;
-		unsigned int mod = o % 32;
-		unsigned int counter = 32 - (o % 32);
-		for(size_t i = jump;i < size;i++){
-			if(i < (size - 1))
-				ret.data[i - jump] = ((data[i] << mod) & lower_half) | (data[i + 1] >> counter);
-			else
-				ret.data[i - jump] = ((data[i] << mod) & lower_half);
-		}
-		return ret;
-	}
-	inline size_t length()const{
-		return size;
-	}
-	inline semi_bitset<size, containertype> operator>>(int o)const{
-		if(o < 0){return this->operator<<(-o);}
-		if(o == 0){return *this;}
-		semi_bitset<size, containertype> ret;
-		unsigned int jump = o / 32;
-		unsigned int mod = o % 32;
-		unsigned int counter = 32 - (o % 32);
-		for(size_t i = jump;i < size;i++){
-			if(i > jump)
-				ret.data[i] = (data[i - jump] >> mod) | ((data[i - jump - 1] << counter) & lower_half);
-			else
-				ret.data[i] = (data[i - jump] >> mod);
-		}
-		return ret;
-	}
-	inline semi_bitset<size, containertype>& operator<<=(int o){
-		if(o < 0){return this->operator>>=(-o);}
-		if(o == 0){return *this;}
-		unsigned int jump = o / 32;
-		unsigned int mod = o % 32;
-		unsigned int counter = 32 - (o % 32);
-		for(size_t i = jump;i < size;i++){
-			if(i < (size - 1))
-				data[i - jump] = ((data[i] << mod) & lower_half) | (data[i + 1] >> counter);
-			else
-				data[i - jump] = ((data[i] << mod) & lower_half);
-		}
-		for(size_t i = size - jump;i < size;i++){
-			data[i] = 0;
-		}
-		return *this;
-	}
-	inline semi_bitset<size, containertype>& operator>>=(int o){
-		if(o < 0){return this->operator<<=(-o);}
-		if(o == 0){return *this;}
-		unsigned int jump = o / 32;
-		unsigned int mod = o % 32;
-		unsigned int counter = 32 - (o % 32);
-		for(size_t i = size - 1;i >= jump;i--){
-			if(i > jump)
-				data[i] = (data[i - jump] >> mod) | ((data[i - jump - 1] << counter) & lower_half);
-			else
-				data[i] = (data[i - jump] >> mod);
-		}
-		if(jump > 0)
-		for(size_t i = jump - 1;i >= 0;i--){
-			data[i] = 0;
-			if(i == 0)break;
-		}
-		return *this;
-	}
-	template<size_t osize, bool ocontainertype>
-	inline semi_bitset<size, containertype | ocontainertype> operator&(const semi_bitset<osize, ocontainertype>& o)const{
-		static_assert(size == osize);
-		semi_bitset<size, containertype | ocontainertype> ret;
-		for(size_t i = 0;i < size;i++){
-			ret.data[i] = (data[i] & o.data[i]);
-		}
-		return ret;
-	}
-	template<size_t osize, bool ocontainertype>
-	inline semi_bitset<size, containertype | ocontainertype> operator^(const semi_bitset<osize, ocontainertype>& o)const{
-		static_assert(size == osize);
-		semi_bitset<size, containertype | ocontainertype> ret;
-		for(size_t i = 0;i < size;i++){
-			ret.data[i] = (data[i] ^ o.data[i]);
-		}
-		return ret;
-		
-	}
-	template<size_t osize, bool ocontainertype>
-	inline semi_bitset<size, containertype | ocontainertype> operator|(const semi_bitset<osize, ocontainertype>& o)const{
-		static_assert(size == osize);
-		semi_bitset<size, containertype | ocontainertype> ret;
-		for(size_t i = 0;i < size;i++){
-			ret.data[i] = (data[i] | o.data[i]);
-		}
-		return ret;
-	}
-	template<size_t osize, bool ocontainertype>
-	inline semi_bitset<size, containertype>& operator&=(const semi_bitset<osize, ocontainertype>& o){
-		static_assert(size == osize);
-		for(size_t i = 0;i < size;i++){
-			data &= o.data[i];
-		}
-		return *this;
-	}
-	template<size_t osize, bool ocontainertype>
-	inline semi_bitset<size, containertype>& operator^=(const semi_bitset<osize, ocontainertype>& o){
-		static_assert(size == osize);
-		for(size_t i = 0;i < size;i++){
-			data ^= o.data[i];
-		}
-		return *this;
-	}
-	template<size_t osize, bool ocontainertype>
-	inline semi_bitset<size, containertype>& operator|=(const semi_bitset<osize, ocontainertype>& o){
-		static_assert(size == osize);
-		for(size_t i = 0;i < size;i++){
-			data |= o.data[i];
-		}
-		return *this;
-	}
-};*/
-#endif

+ 4 - 4
test.cpp

@@ -21,10 +21,10 @@ std::ostream& operator<< <char>(std::ostream& out, std::vector<char> o){
 	return out;
 }
 int main(){
-	BigInt<3> a ((unsigned int)(1e7 + 345345));
-	std::cout << a << std::endl;
-	auto b = a.mult(a);
-	std::cout << b << std::endl;
+	BigInt<dynamic> a((unsigned int)(1e7 + 345345));
+	//std::cout << a << std::endl;
+	//auto b = a.mult(a);
+	//std::cout << b << std::endl;
 }
 int mian(){
 	cppsocket sock("192.168.178.79", 80);