Selaa lähdekoodia

Heapify Bigint

mawinkle 6 vuotta sitten
vanhempi
commit
936b7cafa4
2 muutettua tiedostoa jossa 54 lisäystä ja 15 poistoa
  1. 9 7
      BigInt.hpp
  2. 45 8
      semi_bitset.hpp

+ 9 - 7
BigInt.hpp

@@ -4,28 +4,30 @@
 #include <cstdlib>
 #include <string>
 #include <iostream>
-template<size_t size>
+#include <cassert>
+template<size_t size = dynamic>
 class BigInt{
 private:
 	int signum = 1;
-	semi_bitset<size, size >= 1000> bits;
+	semi_bitset<size> bits;
 public:
-	inline BigInt(){
+	inline BigInt() : bits(1){}
+	inline BigInt(size_t _size, bool dummy) : bits(_size){
 		
 	}
-	inline BigInt(int o){
+	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){
+	inline BigInt(unsigned int o) : BigInt(2,false){
 		bits.data[bits.length() - 1] = o;
 	}
-	inline BigInt(long long 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){
+	inline BigInt(unsigned long long o) : BigInt(3,false){
 		bits.data[bits.length() - 1] = (o & lower_half);
 		bits.data[bits.length() - 2] = (o >> 32);
 	}

+ 45 - 8
semi_bitset.hpp

@@ -3,9 +3,52 @@
 #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, bool containertype>
+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;
@@ -266,11 +309,5 @@ struct semi_bitset<size, 0>{
 		}
 		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 < osize;i++)
-			s << std::bitset<32>((uint32_t)(o.data[i] & lower_half));
-		return s;
-	}*/
-};
+};*/
 #endif