//inline void write(ostream & out, const bool savesizeinbits=true) const; static JSVAL putbs(JSARGS args) { HandleScope scope; String::Utf8Value dbname(args[0]->ToString()); String::Utf8Value key(args[1]->ToString()); EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[2]); if (!bs) { return v8::ThrowException(v8::String::New("In putbs bitset is Null")); } leveldb::Status s; leveldb::DB* db = getDb(gcontext->dbl, dbname, s); if (!db) { return v8::ThrowException(v8::String::New(s.ToString().c_str())); } std::stringstream ss; bs->write(ss, /*const bool savesizeinbits=*/ true); //cout<< "putbs:" << ss.str().size() << " "<< ss.str() << endl; s = db->Put(leveldb::WriteOptions(), *key, ss.str()); if (!s.ok()) { return v8::ThrowException(v8::String::New(s.ToString().c_str())); } return scope.Close(Integer::New(1)); }
//inline void read(istream & in, const bool savesizeinbits=true); static JSVAL getbs(JSARGS args) { HandleScope scope; String::Utf8Value dbname(args[0]->ToString()); String::Utf8Value key(args[1]->ToString()); EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[2]); if (!bs) { return v8::ThrowException(v8::String::New("In getbs bitset is Null")); } std::string val; leveldb::Status s; leveldb::DB* db = getDb(gcontext->dbl, dbname,s); if (!db) { return v8::ThrowException(v8::String::New(s.ToString().c_str())); } s = db->Get(leveldb::ReadOptions(), *key, &val); if (!s.ok()) { //if not found we return Null otherwise we throw exception if (s.IsNotFound()) return scope.Close(Null()); else return v8::ThrowException(v8::String::New(s.ToString().c_str())); } //cout<< "getbs:" << val.size() << " " << val << endl; std::stringstream ss(val); bs->read(ss, /*const bool savesizeinbits=*/true); return scope.Close(Integer::New(1)); }
//bitset1.reset(); static JSVAL bs_reset(JSARGS args) { HandleScope scope; EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[0]); if (!bs) { return v8::ThrowException(v8::String::New("In reset bitset is Null")); } bs->reset(); return scope.Close(Null()); }
//bitset1.set(1); static JSVAL bs_set(JSARGS args) { HandleScope scope; EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[0]); if (!bs) { return v8::ThrowException(v8::String::New("In set bitset is Null")); } int v = args[1]->IntegerValue(); bs->set(v); return scope.Close(Null()); }
//EWAHBoolArray<LENGTH>::const_iterator i = bitset1.begin(); i!=bitset1.end(); ++i static JSVAL bs_it_isend(JSARGS args) { HandleScope scope; EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[0]); if (!bs) { return v8::ThrowException(v8::String::New("In it_isend bitset is Null")); } EWAHBoolArray<LENGTH>::const_iterator* it = (EWAHBoolArray<LENGTH>::const_iterator*) External::Unwrap(args[1]); if (!it) { return v8::ThrowException(v8::String::New("In it_isend it is Null")); } int ret = ( (*it)==(bs->end()) ); return scope.Close(Integer::New(ret)); }
bool testJoergBukowski() { cout << "[testing JoergBukowski]" << endl; bool isOk(true); vector<uint32_t> positions; positions.push_back(0); positions.push_back(36778); positions.push_back(51863); positions.push_back(134946); positions.push_back(137330); positions.push_back(147726); positions.push_back(147990); positions.push_back(151884); positions.push_back(156404); positions.push_back(158486); positions.push_back(159622); positions.push_back(163159); positions.push_back(164599); string indexfile("testingewahboolarray.bin"); ::remove(indexfile.c_str()); EWAHBoolArray<uword> myarray; for (vector<uint32_t>::const_iterator i = positions.begin(); i != positions.end(); ++i) { myarray.set(*i); ofstream out(indexfile.c_str(), ios::out | ios::binary); myarray.write(out); out.close(); EWAHBoolArray<uword> recovered; ifstream in(indexfile.c_str(), ios::binary); recovered.read(in); in.close(); vector < size_t > vals; recovered.appendSetBits(vals); if (vals.size() != static_cast<size_t> (i - positions.begin() + 1)) { cout << "failed to recover right number" << endl; isOk = false; } if (!equal(vals.begin(), vals.end(), positions.begin())) { cout << "failed to recover" << endl; isOk = false; } vals.clear(); for (typename EWAHBoolArray<uword>::const_iterator j = recovered.begin(); j != recovered.end(); ++j) vals.push_back(static_cast<uint32_t> (*j)); if (vals.size() != static_cast<size_t> (i - positions.begin() + 1)) { cout << "failed to recover right number -- iterator" << endl; isOk = false; } if (!equal(vals.begin(), vals.end(), positions.begin())) { cout << "failed to recover -- iterator" << endl; isOk = false; } } if (isOk) ::remove(indexfile.c_str()); if (!isOk) cout << testfailed << endl; return isOk; }
//bitset2.logicalnot(notbitset); static JSVAL bs_logicalnot(JSARGS args) { HandleScope scope; EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[0]); if (!bs) { return v8::ThrowException(v8::String::New("In logicalnot bitset 1 is Null")); } EWAHBoolArray<LENGTH>* bs2 = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[1]); if (!bs2) { return v8::ThrowException(v8::String::New("In logicalnot bitset 2 is Null")); } bs->logicalnot(*bs2); return scope.Close(Null()); }
static JSVAL bs_tostring(JSARGS args) { HandleScope scope; EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[0]); if (!bs) { return v8::ThrowException(v8::String::New("In tostring bitset is Null")); } std::stringstream ss; //cout<<"first bitset : "<<endl; for(EWAHBoolArray<LENGTH>::const_iterator i = bs->begin(); i!=bs->end(); ++i) ss<<*i<<","; Handle<String> res = String::New(ss.str().c_str()); return scope.Close(res); }
bool testNot() { cout << "[testing Not]" << endl; bool isOk = true; EWAHBoolArray<uword> bitset; for (int i = 0; i <= 184; i++) { bitset.set(i); } if (bitset.numberOfOnes() != 185) { isOk = false; } bitset.inplace_logicalnot(); if (bitset.numberOfOnes() != 0) { isOk = false; } return isOk; }
bool testCardinalityEWAHBoolArray() { cout << "[testing CardinalityEWAHBoolArray] sizeof(uword)=" << sizeof(uword) << endl; EWAHBoolArray<uword> b1 = EWAHBoolArray<uword>::bitmapOf(1,1); if (b1.numberOfOnes() != 1) { return false; } b1.inplace_logicalnot(); if (b1.numberOfOnes() != 1) { cout<<"b1 "<<b1<<endl; return false; } EWAHBoolArray<uword> b = EWAHBoolArray<uword>::bitmapOf(2,1,100); if(b.numberOfOnes() != 2) { cout<<"b "<<b<<endl; return false; } EWAHBoolArray<uword> bout; b.logicalnot(bout); if(bout.numberOfOnes() != 99) { cout<<"bout "<<bout<<endl; return false; } b.inplace_logicalnot(); if(b.numberOfOnes() != 99) { cout<<"b neg "<<b<<endl; return false; } return true; }
bool testSetGet() { cout << "[testing EWAH set/get] sizeof(uword)="<<sizeof(uword)<<endl; EWAHBoolArray<uword> ewcb; uint32_t val[] = { 5, 4400, 44600, 55400, 1000000 }; for (int k = 0; k < 5; ++k) { ewcb.set(val[k]); } size_t counter = 0; bool isOk = true; for (typename EWAHBoolArray<uword>::const_iterator i = ewcb.begin(); i != ewcb.end(); ++i) { if(val[counter++]!=*i) { cout<<"Failed test set/get"<<endl; isOk = false; } } return isOk; }
bool testNanJiang() { cout << "[testing NanJiang] sizeof(uword)=" << sizeof(uword) << endl; EWAHBoolArray<uword> b; b.set(5); if (b.numberOfOnes() != 1) return false; b.inplace_logicalnot(); if (b.numberOfOnes() != 5) return false; BoolArray<uword> b2; b2.set(5); if (b2.numberOfOnes() != 1) return false; b2.inplace_logicalnot(); if (b2.numberOfOnes() != 5) return false; return true; }
//bitset1.sparselogicaland(notbitset,andbitset); static JSVAL bs_sparselogicaland(JSARGS args) { HandleScope scope; EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[0]); if (!bs) { return v8::ThrowException(v8::String::New("In sparselogicaland bitset 1 is Null")); } EWAHBoolArray<LENGTH>* bs2 = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[1]); if (!bs2) { return v8::ThrowException(v8::String::New("In sparselogicaland bitset 2 is Null")); } EWAHBoolArray<LENGTH>* bs3 = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[2]); if (!bs3) { return v8::ThrowException(v8::String::New("In sparselogicaland bitset 3 is Null")); } bs->sparselogicaland(*bs2, *bs3); return scope.Close(Null()); }
static EWAHBoolArray<uint64_t>* readOneBitmap(ifstream* inputStream) { EWAHBoolArray<uint64_t>* ewah = new EWAHBoolArray<uint64_t>; uint32_t sizeInBits = 0; inputStream->read((char *)&sizeInBits, 4); sizeInBits = swapBytesIfNecessary(sizeInBits); uint32_t numberOfOnes = 0; inputStream->read((char *)&numberOfOnes, 4); numberOfOnes = swapBytesIfNecessary(numberOfOnes); uint32_t tmp = 0; for (unsigned long i = 0; i<numberOfOnes; ++i) { inputStream->read((char *)&tmp, 4); tmp = swapBytesIfNecessary(tmp); ewah->set(tmp); } ewah->setSizeInBits(sizeInBits); return ewah; }
bool testPhongTran2() { cout << "[testing PhongTran2]" << endl; bool isOk(true); uword iTotal = static_cast<uword> (1000); // when 1000 does not fit in uword, then it will be casted EWAHBoolArray<uword> myarray; for (uword x = static_cast<uword> (100); x < iTotal; x++) { myarray.addWord(x); } string indexfile("testingewahboolarray.bin"); ::remove(indexfile.c_str()); ofstream out(indexfile.c_str(), ios::out | ios::binary); myarray.write(out); out.close(); EWAHBoolArray<uword> lmyarray; ifstream in(indexfile.c_str(), ios::binary); lmyarray.read(in); in.close(); if (!(myarray == lmyarray)) { cout << "bad news, they are not equal" << endl; cout << "size in bits: " << myarray.sizeInBits() << " vs. " << lmyarray.sizeInBits() << endl; isOk = false; } EWAHBoolArrayIterator<uword> i = myarray.uncompress(); EWAHBoolArrayIterator<uword> j = lmyarray.uncompress(); while (i.hasNext()) { if (!j.hasNext()) { cout << "the two arrays don't have the same size?" << endl; isOk = false; break; } uword val = i.next(); uword val2 = j.next(); if (val != val2) { cout << "the two arrays differ " << endl; isOk = false; } } if (isOk) ::remove(indexfile.c_str()); if (!isOk) cout << testfailed << endl; return isOk; }
//bitset1.logicalor(bitset2,orbitset); static JSVAL bs_logicalor(JSARGS args) { HandleScope scope; EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[0]); if (!bs) { return v8::ThrowException(v8::String::New("In logicalor bitset 1 is Null")); } EWAHBoolArray<LENGTH>* bs2 = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[1]); if (!bs2) { return v8::ThrowException(v8::String::New("In logicalor bitset 2 is Null")); } EWAHBoolArray<LENGTH>* bs3 = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[2]); if (!bs3) { return v8::ThrowException(v8::String::New("In logicalor bitset 3 is Null")); } try { bs->logicalor(*bs2, *bs3); } catch(std::out_of_range e) { return v8::ThrowException(v8::String::New("In logicalor std::out_of_range")); } return scope.Close(Null()); }
//EWAHBoolArray<LENGTH>::const_iterator i = bitset1.begin(); i!=bitset1.end(); ++i static JSVAL bs_it_end(JSARGS args) { HandleScope scope; EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[0]); if (!bs) { return v8::ThrowException(v8::String::New("In it_end bitset is Null")); } EWAHBoolArray<LENGTH>::const_iterator* it = new EWAHBoolArray<LENGTH>::const_iterator(bs->end()); //*it = bitset1.end(); //add iterator for auto del add_obj((void*)it, auto_del_bs_it); return scope.Close(External::Wrap(it)); }
bool testGet() { cout << "[testing Get] sizeof(uword)=" << sizeof(uword) << endl; bool isOk = true; for (size_t gap = 29; gap < 10000; gap *= 10) { EWAHBoolArray<uword> x; for (uint32_t k = 0; k < 100; ++k) x.set(k * gap); for (size_t k = 0; k < 100 * gap; ++k) if (x.get(k)) { if (k % gap != 0) { cout << "spotted an extra set bit at " << k << " gap = " << gap << endl; return false; } } else if (k % gap == 0) { cout<< "missed a set bit " << k << " gap = " << gap<<endl; return false; } } return isOk; }
bool testSerialization() { cout << "[testing Serialization] word size = " << sizeof(uword)<< endl; EWAHBoolArray<uword> bitmap; for(int i = 0; i < 1<<31; i= 2*i +3) { bitmap.set(static_cast<size_t>(i)); } stringstream ss; EWAHBoolArray<uword> lmyarray; for (int k = 0; k < 10; ++k) { bitmap.write(ss); lmyarray.read(ss); if (lmyarray != bitmap) { return false; } typename EWAHBoolArray<uword>::const_iterator i = bitmap.begin(); typename EWAHBoolArray<uword>::const_iterator j = lmyarray.begin(); for (; i != bitmap.end(); ++i, ++j) { if (*i != *j) return false; } } return true; }
// unit test contributed by Phong Tran bool testPhongTran() { cout << "[testing PhongTran]" << endl; bool isOk(true); EWAHBoolArray<uint32_t> myarray; for (uint32_t x = 0; x < 10000; x++) { myarray.addWord(x); } string indexfile("testingewahboolarray.bin"); ::remove(indexfile.c_str()); ofstream out(indexfile.c_str(), ios::out | ios::binary); myarray.write(out); out.close(); EWAHBoolArray<uint32_t> lmyarray; ifstream in(indexfile.c_str(), ios::binary); lmyarray.read(in); in.close(); EWAHBoolArrayIterator<uint32_t> i = myarray.uncompress(); EWAHBoolArrayIterator<uint32_t> j = lmyarray.uncompress(); while (i.hasNext() or j.hasNext()) { if ((!j.hasNext()) or (!i.hasNext())) { cout << "the two arrays don't have the same size?" << endl; isOk = false; break; } uint32_t val = i.next(); uint32_t val2 = j.next(); if (val != val2) { cout << "the two arrays differ" << endl; isOk = false; break; } } if (isOk) ::remove(indexfile.c_str()); if (!isOk) cout << testfailed << endl; return isOk; }
bool testEWAHBoolArray() { cout << "[testing EWAHBoolArray]" << endl; bool isOk(true); EWAHBoolArray<uword> myarray; BoolArray<uword> ba(10 * sizeof(uword) * 8); uword zero = 0; uword notzero = static_cast<uword> (~zero); myarray.addWord(zero); ba.setWord(0, zero); myarray.addWord(zero); ba.setWord(1, zero); myarray.addWord(zero); ba.setWord(2, zero); uword specialval = 1UL + (1UL << 4) + (static_cast<uword> (1) << (sizeof(uword) * 8 - 1)); myarray.addWord(specialval); ba.setWord(3, specialval); myarray.addWord(notzero); ba.setWord(4, notzero); myarray.addWord(notzero); ba.setWord(5, notzero); myarray.addWord(notzero); ba.setWord(6, notzero); myarray.addWord(notzero); ba.setWord(7, notzero); myarray.addWord(specialval); ba.setWord(8, specialval); myarray.addWord(zero); ba.setWord(9, zero); if (myarray.sizeInBits() != 10 * sizeof(uword) * 8) { cout << "expected " << 10 * sizeof(uword) * 8 << " bits but found " << myarray.sizeInBits() << endl; isOk = false; } string indexfile("testingewahboolarray.bin"); ::remove(indexfile.c_str()); ofstream out(indexfile.c_str(), ios::out | ios::binary); myarray.write(out); out.close(); EWAHBoolArray<uword> lmyarray; ifstream in(indexfile.c_str(), ios::binary); lmyarray.read(in); in.close(); if (!(myarray == lmyarray)) { cout << "bad news, they are not equal" << endl; cout << "size in bits: " << myarray.sizeInBits() << " vs. " << lmyarray.sizeInBits() << endl; isOk = false; } EWAHBoolArrayIterator<uword> i = myarray.uncompress(); EWAHBoolArrayIterator<uword> j = lmyarray.uncompress(); uint32_t k = 0; while (i.hasNext()) { if (!j.hasNext()) { cout << "the two arrays don't have the same size?" << endl; isOk = false; break; } uword val = i.next(); uword val2 = j.next(); uword valref = ba.getWord(k++); if (val != valref) { cout << "the two arrays differ from uncompressed array at " << k << " " << val << " " << val2 << " " << valref << endl; isOk = false; } if (val != val2) { cout << "the two arrays differ at " << k << " " << val << " " << val2 << " " << valref << endl; isOk = false; } } if (isOk) ::remove(indexfile.c_str()); if (!isOk) cout << testfailed << endl; return isOk; }
bool testEWAHBoolArrayLogical() { cout << "[testing EWAHBoolArrayLogical] word size = "<< sizeof(uword) << endl; bool isOk(true); EWAHBoolArray<uword> myarray1; EWAHBoolArray<uword> myarray2; uword allones = static_cast<uword> (~0LL); const uint32_t N = 16; uword x1[N] = { 1, 0, 54, 24, 145, 0, 0, 0, allones, allones,allones, 43, 0, 0, 0, 1 }; uword x2[N] = { allones, 1, 0, 0, 0, 0, 0, 0, 0, allones, allones, allones, 0,4, 0, 0 }; uword xand[N]; uword xxor[N]; size_t usedN = 10; if(sizeof(uword)>2) return true; for (uint32_t k = 0; k < usedN; ++k) { myarray1.addWord(x1[k]); myarray2.addWord(x2[k]); xand[k] = static_cast<uword>(x1[k] & x2[k]); xxor[k] = static_cast<uword>(x1[k] | x2[k]); } EWAHBoolArray<uword> myand; EWAHBoolArray<uword> myor; EWAHBoolArray<uword> myxor; EWAHBoolArray<uword> myxoralt; myarray1.logicaland(myarray2, myand); myarray1.logicalor(myarray2, myor); myarray1.logicalxor(myarray2, myxor); EWAHBoolArray<uword> tmp(myand); tmp.inplace_logicalnot(); myor.logicaland(tmp, myxoralt); if(myxoralt != myxor) { isOk = false; if (!isOk) cout << testfailed << endl; return isOk; } EWAHBoolArrayIterator<uword> i = myand.uncompress(); EWAHBoolArrayIterator<uword> j = myor.uncompress(); EWAHBoolArrayIterator<uword> it1 = myarray1.uncompress(); EWAHBoolArrayIterator<uword> it2 = myarray2.uncompress(); for (uint32_t k = 0; k < usedN; ++k) { const uword m1 = it1.next(); const uword m2 = it2.next(); if (!i.hasNext()) { if((m1 & m2) != 0) { cout << "type 1 error" << endl; isOk = false; break; } } else { const uword inter = i.next(); if (inter != xand[k]) { cout << "type 4 error" << endl; isOk = false; break; } } if (!j.hasNext()) { if((m1 | m2) != 0) { cout << "type 3 error" << endl; isOk = false; break; } } else { const uword jor = j.next(); if (jor != xxor[k]) { cout << "type 6 error OR" << endl; isOk = false; break; } } } if (!isOk) cout << testfailed << endl; return isOk; }
void init( EWAHBoolArray<uword>& ba, size_t N, size_t x[] ) { for ( size_t ix= 0; ix < N; ++ix ) ba.set( x[ix] ); }