void testMultiplicationSquares(std::size_t noBits, DynamicElementStorageType lhsStorage, DynamicElementStorageType rhsStorage) { Bitset activeBits; for(std::size_t i = 0; i < noBits; ++i) activeBits[std::rand() & MAX_K_VALUE] = true; Element lhs(activeBits, lhsStorage); // get a non-zero value Z2k value; while(value.getValue() == 0) value = Z2k( rand() & activeBits.to_ulong() ); // get a coeff GaloisField coeff( rand() & 255 ); lhs.setCoefficient(value, coeff); lhs.setCoefficient(Z2k(), coeff); Element rhs(lhs, rhsStorage); Element prod; algebra.multiply(lhs, rhs, prod); BOOST_CHECK_EQUAL(prod.isZero(), true); }
TEST(tinybitset_unittest, test_bitset_init) { Bitset bitset; bool res = bitset.empty(); EXPECT_EQ(true, res); vector<int> bitones = bitset.getbitones(); EXPECT_EQ(0,bitones.size()); }
void Checkable::UpdateFlappingStatus(bool stateChange) { Bitset<unsigned long> stateChangeBuf = GetFlappingBuffer(); int oldestIndex = GetFlappingIndex(); stateChangeBuf.Modify(oldestIndex, stateChange); oldestIndex = (oldestIndex + 1) % 20; double stateChanges = 0; /* Iterate over our state array and compute a weighted total */ for (int i = 0; i < 20; i++) { if (stateChangeBuf.Get((oldestIndex + i) % 20)) stateChanges += 0.8 + (0.02 * i); } double flappingValue = 100.0 * stateChanges / 20.0; bool flapping; if (GetFlapping()) flapping = flappingValue > GetFlappingThresholdLow(); else flapping = flappingValue > GetFlappingThresholdHigh(); SetFlappingBuffer(stateChangeBuf.GetValue()); SetFlappingIndex(oldestIndex); SetFlappingCurrent(flappingValue); SetFlapping(flapping, true); if (flapping != GetFlapping()) SetFlappingLastChange(Utility::GetTime()); }
Bitset range(int l, int r) { Bitset ret; for (int i = l; i < r; ++i) { ret.set(i); } return ret; }
TEST(Bitset, flipFirstBitOfSecondNum) { Bitset<100> bs; EXPECT_FALSE(bs.test(0)); bs.flip(64); EXPECT_TRUE(bs.test(64)); // The first bit was flipped because indexFromBits(64) returned 0 instead of 1 earlier! EXPECT_FALSE(bs.test(0)); }
void FileSystemApi::write_bitset(DBC &conf, Bitset &b) { assert(file.is_open()); assert(file.good()); file.seekp(get_bitset_offset(conf), std::ios_base::beg); file.write(b.as_bytes(), b.num_blocks()); file.seekg(get_bitset_offset(conf), std::ios_base::beg); char *buff = new char[get_bitset_size(conf)]; file.read(buff, get_bitset_size(conf)); delete[] buff; }
static void load_inst_constrain(Instance * inst,Bitset * original_bitset) { if(original_bitset){ inst->external_lexicon_match_state.push_back((*original_bitset)); } else{ Bitset bitset; bitset.allsetones(); inst->external_lexicon_match_state.push_back(bitset); } }//end func load_inst_constrain
TEST(tinybitset_unittest, test_bitset_allsetones) { Bitset bitset; bitset.allsetones(); bool res = bitset.empty(); EXPECT_EQ(true, res); vector<int> bitones = bitset.getbitones(); EXPECT_EQ(128,bitones.size()); for(int i=0;i<bitones.size();i++){ EXPECT_EQ(i,bitones[i]); } }
TEST( LetterBitsetFromStringTest, Basic ) { Bitset expected; expected.set( IndexForChar( 'a' ) ); expected.set( IndexForChar( 'o' ) ); expected.set( IndexForChar( 'c' ) ); expected.set( IndexForChar( 'f' ) ); expected.set( IndexForChar( 'b' ) ); std::string text = "abcfoof"; EXPECT_EQ( expected, LetterBitsetFromString( text ) ); }
uint32_t lock(T * keys){ int i; uint32_t unlockVar; Bitset b; b.Resize(1000); for(i=0;i<thread_num;i++) { b.Set(keys[i]); } unlockVar = m.Lock(b); return unlockVar; }
uint32_t lock(int key1,int key2){ int i; uint32_t unlockVar; Bitset b; b.Resize(1000); b.Set(key1); b.Set(key2); unlockVar = m.Lock(b); return unlockVar; }
TEST(tinybitset_unittest, test_bitset_normaltest) { Bitset bitset; bool res; for(int i=0;i<kNormalCaseNum;i++){ bitset.set(kNormalCase[i]); res = bitset.get(kNormalCase[i]); EXPECT_EQ(1,res); res = bitset.empty(); EXPECT_EQ(0,res); } vector<int> bitones = bitset.getbitones(); EXPECT_EQ(kNormalCaseNum,bitones.size()); for(int i=0;i<kNormalCaseNum;i++){ EXPECT_EQ(kNormalCase[i],bitones[i]); } }
inline int ilog2_bitset_trivial(Bitset v) { int ret = 0; for( ; v.any(); v >>= 1 ) ++ret; return ret - 1; }
void FileSystemApi::read_bitset(DBC &conf, Bitset &b) { assert(file.good()); file.seekg(get_bitset_offset(conf), std::ios_base::beg); char *buff = new char[get_bitset_size(conf)]; file.read(buff, get_bitset_size(conf)); b.from_bytes(get_bitset_size(conf), buff); delete[] buff; }
static void load_model_constrain(Model * model , const char * lexicon_file = NULL) { if (NULL != lexicon_file) { std::ifstream lfs(lexicon_file); if (lfs) { std::string buffer; std::vector<std::string> key_values; int key_values_size; std::string key; int value; Bitset * original_bitset; while (std::getline(lfs, buffer)) { buffer = ltp::strutils::chomp(buffer); if (buffer.size() == 0) { continue; } Bitset values; key_values = ltp::strutils::split(buffer); key_values_size = key_values.size(); if(key_values_size == 0 || key_values_size == 1) { continue; } key = ltp::strutils::chartypes::sbc2dbc_x(key_values[0]); for(int i=1;i<key_values_size;i++){ value = model->labels.index(key_values[i]); if (value != -1){ if(!(values.set(value))) { WARNING_LOG("Tag named %s for word %s add external lexicon error.",key_values[i].c_str(),key_values[0].c_str()); } } else { WARNING_LOG("Tag named %s for word %s is not existed in LTP labels set.",key_values[i].c_str(),key_values[0].c_str()); } } if(!values.empty()) { original_bitset = model->external_lexicon.get(key.c_str()); if(original_bitset){ original_bitset->merge(values); } else{ model->external_lexicon.set(key.c_str(),values); } } } } } }//end func load_model_constrain
int find(long long a, long long b) { fibs[0] = 1; fibs[1] = 2; for (int i = 2; i < N; ++ i) { fibs[i] = fibs[i - 1] + fibs[i - 2]; } memory[1] = 0; Bitset mask = solve(b + 1) ^ solve(a); int result = 0; for (int i = N - 1; i >= 0; -- i) { (result *= 2) %= MOD; if (mask.test(i)) { (result += 1) %= MOD; } } return result; }
Bitset* Journal::getBitsetbyKey(unsigned key, uint64_t start_tid, uint64_t end_tid) { DArray<unsigned>* offsets = key_htable.getHashRecords(key, start_tid, end_tid); unsigned start_offset, end_offset, i; if(offsets == NULL) return NULL; #if TID_HASHTABLE == 1 while((start_offset = tidSearchRecord(start_tid)) == -1) // psakse se pio index tou Journal tha ksekinisw na psaxnw start_tid += 1; // an den yparxei to tid pou mou dwse san start psakse to epomeno while((end_offset = tidSearchRecord(end_tid)) == -1) end_tid -= 1; #else while((start_offset = searchRecord(start_tid)) == -1) // psakse se pio index tou Journal tha ksekinisw na psaxnw start_tid += 1; // an den yparxei to tid pou mou dwse san start psakse to epomeno while((end_offset = searchRecord(end_tid)) == -1) end_tid -= 1; #endif int rsize = Records->size(); for (i = end_offset; i < rsize; i++) { uint64_t tid = (Records->get(i))->getTransactionId(); if (tid > end_tid) // an exw kseperasei to end_tid break; } end_offset = i; Bitset* bit = new Bitset((end_offset - start_offset)/8 + 1); for(i = 0; i < offsets->size(); i++) { bit->setBitsetValue(offsets->get(i) - start_offset); } delete offsets; return bit; }
void TestBitset::test () { Bitset <unsigned int> bs; assert (bs.get <0> () == false); assert (bs.get <8 * sizeof (unsigned int) - 1> () == false); bs.toggle <0> (); bs.toggle <8 * sizeof (unsigned int) - 1> (); // bs.toggle <8 * sizeof (unsigned int)> (); // must not compile assert (bs.get <0> ()); assert (bs.get <1> () == false); assert (bs.get <8 * sizeof (unsigned int) - 1> ()); assert (bs.value () == std::pow (2, 8 * sizeof (unsigned int) - 1) + 1); bs.reset (); assert (bs.none ()); bs.set <0> (); bs.set <1> (); bs.set <2> (); assert (bs.none () == false); assert (bs.all <2> ()); assert (bs.all <3> ()); assert (bs.all <4> () == false); assert (bs.value () == 7); }
TEST(Bitset, set) { Bitset<2> bs; ASSERT_EQ(bs.size(), 2); EXPECT_FALSE(bs[0]); EXPECT_FALSE(bs[1]); bs.set(0, true); EXPECT_TRUE(bs[0]); bs.set(0, false); EXPECT_FALSE(bs[0]); bs.set(1, true); EXPECT_TRUE(bs[1]); Bitset<82> bs2; ASSERT_EQ(bs2.size(), 82); EXPECT_FALSE(bs2[70]); bs2.set(70, true); EXPECT_TRUE(bs2[70]); }
TEST(tinybitset_unittest, test_bitset_boundrytest) { Bitset bitset; bool res = bitset.set(-1); EXPECT_EQ(0,res); res = bitset.set(128); EXPECT_EQ(0,res); res = bitset.set(0); EXPECT_EQ(1,res); res = bitset.set(127); EXPECT_EQ(1,res); res = bitset.get(-1); EXPECT_EQ(0,res); res = bitset.get(128); EXPECT_EQ(0,res); res = bitset.get(0); EXPECT_EQ(1,res); res = bitset.get(127); EXPECT_EQ(1,res); }
int main() { map<string, pair<int, int> > var; stack<Bitset> val; char buf[80], *p; int n, x, y; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%s%d%d", buf, &x, &y); var[buf] = make_pair(x, y + 1); } scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%s", buf); x = strtol(buf, &p, 10); if (*p == '\0') { val.push(range(x, x + 1)); } else if (strchr("+-*/", buf[0]) == NULL) { pair<int, int> snd = var[buf]; val.push(range(snd.first, snd.second)); } else { Bitset rhs = val.top(); val.pop(); Bitset lhs = val.top(); val.pop(); if (buf[0] == '/' && rhs[0]) { puts("error"); return 0; } else { Bitset ret; for (int i = 0; i < (int)lhs.size(); ++i) { if (!lhs[i]) { continue; } for (int j = 0; j < (int)rhs.size(); ++j) { if (!rhs[j]) { continue; } ret.set(eval(i, j, buf[0])); } } val.push(ret); } } } puts("correct"); return 0; }
void initializeElement(Element & element, const Bitset & activeBits, std::size_t noElements) { for(std::size_t i = 0; i < noElements; ++i) element.setCoefficient(Z2k( rand() & activeBits.to_ulong() ), GaloisField( rand() & 255 )); }
/** * Append a verbose, readable hit to the given output stream. */ void VerboseHitSink::append( BTString& o, const Hit& h, const vector<string>* refnames, bool fullRef, int partition, int offBase, bool colorSeq, bool colorQual, bool cost, const Bitset& suppress) { bool spill = false; int spillAmt = 0; uint32_t pdiv = 0xffffffff; uint32_t pmod = 0xffffffff; do { bool dospill = false; if(spill) { // The read spilled over a partition boundary and so // needs to be printed more than once spill = false; dospill = true; spillAmt++; } assert(!spill); size_t field = 0; bool firstfield = true; if(partition != 0) { int pospart = abs(partition); if(!suppress.test((uint32_t)field++)) { if(firstfield) firstfield = false; else o << '\t'; // Output a partitioning key // First component of the key is the reference index if(refnames != NULL && h.h.first < refnames->size()) { printUptoWs(o, (*refnames)[h.h.first], !fullRef); } else { o << h.h.first; } } // Next component of the key is the partition id if(!dospill) { pdiv = (h.h.second + offBase) / pospart; pmod = (h.h.second + offBase) % pospart; } assert_neq(0xffffffff, pdiv); assert_neq(0xffffffff, pmod); if(dospill) assert_gt(spillAmt, 0); if(partition > 0 && (pmod + h.length()) >= ((uint32_t)pospart * (spillAmt + 1))) { // Spills into the next partition so we need to // output another alignment for that partition spill = true; } if(!suppress.test((uint32_t)field++)) { if(firstfield) { firstfield = false; } else { o << '\t'; } // Print partition id with leading 0s so that Hadoop // can do lexicographical sort (modern Hadoop versions // seen to support numeric) int padding = 10; uint32_t part = (pdiv + (dospill ? spillAmt : 0)); uint32_t parttmp = part; while(parttmp > 0) { padding--; parttmp /= 10; } assert_geq(padding, 0); for(int i = 0; i < padding; i++) { o << '0'; } o << part; } if(!suppress.test((uint32_t)field++)) { if(firstfield) { firstfield = false; } else { o << '\t'; } // Print offset with leading 0s int padding = 9; uint32_t off = h.h.second + offBase; uint32_t offtmp = off; while(offtmp > 0) { padding--; offtmp /= 10; } assert_geq(padding, 0); for(int i = 0; i < padding; i++) { o << '0'; } o << off; } if(!suppress.test((uint32_t)field++)) { if(firstfield) firstfield = false; else o << '\t'; o << (h.fw? "+":"-"); } // end if(partition != 0) } else { assert(!dospill); if(!suppress.test((uint32_t)field++)) { if(firstfield) firstfield = false; else o << '\t'; for(size_t i = 0; i < seqan::length(h.patName); i++) { o << (char)(h.patName[i]); } } if(!suppress.test((uint32_t)field++)) { if(firstfield) firstfield = false; else o << '\t'; o << (h.fw? '+' : '-'); } if(!suppress.test((uint32_t)field++)) { if(firstfield) firstfield = false; else o << '\t'; // .first is text id, .second is offset if(refnames != NULL && h.h.first < refnames->size()) { printUptoWs(o, (*refnames)[h.h.first], !fullRef); } else { o << h.h.first; } } if(!suppress.test((uint32_t)field++)) { if(firstfield) firstfield = false; else o << '\t'; o << (h.h.second + offBase); } // end else clause of if(partition != 0) } if(!suppress.test((uint32_t)field++)) { if(firstfield) firstfield = false; else o << '\t'; const String<Dna5>* pat = &h.patSeq; if(h.color && colorSeq) pat = &h.colSeq; for(size_t i = 0; i < seqan::length(*pat); i++) { o << (char)((*pat)[i]); } } if(!suppress.test((uint32_t)field++)) { if(firstfield) firstfield = false; else o << '\t'; const String<char>* qual = &h.quals; if(h.color && colorQual) qual = &h.colQuals; for(size_t i = 0; i < seqan::length(*qual); i++) { o << (char)((*qual)[i]); } } if(!suppress.test((uint32_t)field++)) { if(firstfield) firstfield = false; else o << '\t'; o << h.oms; } if(!suppress.test((uint32_t)field++)) { if(firstfield) firstfield = false; else o << '\t'; const size_t len = length(h.patSeq); // Output mismatch column bool firstmm = true; for (unsigned int i = 0; i < len; ++ i) { if(h.mms.test(i)) { // There's a mismatch at this position if (!firstmm) { o << ","; } o << i; // position assert_gt(h.refcs.size(), i); char refChar = toupper(h.refcs[i]); char qryChar = (h.fw ? h.patSeq[i] : h.patSeq[length(h.patSeq)-i-1]); assert_neq(refChar, qryChar); o << ":" << refChar << ">" << qryChar; firstmm = false; } } if(partition != 0 && firstmm) o << '-'; } if(partition != 0) { // Fields addded as of Crossbow 0.1.4 if(!suppress.test((uint32_t)field++)) { if(firstfield) firstfield = false; else o << '\t'; o << (int)h.mate; } // Print label, or whole read name if label isn't found if(!suppress.test((uint32_t)field++)) { if(firstfield) firstfield = false; else o << '\t'; int labelOff = -1; // If LB: field is present, print its value for(int i = 0; i < (int)seqan::length(h.patName)-3; i++) { if(h.patName[i] == 'L' && h.patName[i+1] == 'B' && h.patName[i+2] == ':' && ((i == 0) || h.patName[i-1] == ';')) { labelOff = i+3; for(int j = labelOff; j < (int)seqan::length(h.patName); j++) { if(h.patName[j] != ';') { o << h.patName[j]; } else { break; } } } } // Otherwise, print the whole read name if(labelOff == -1) { for(size_t i = 0; i < seqan::length(h.patName); i++) { o << (char)(h.patName[i]); } } } } if(cost) { // Stratum if(!suppress.test((uint32_t)field++)) { if(firstfield) firstfield = false; else o << '\t'; o << (int)h.stratum; } // Cost if(!suppress.test((uint32_t)field++)) { if(firstfield) firstfield = false; else o << '\t'; o << (int)h.cost; } } if(showSeed) { // Seed if(!suppress.test((uint32_t)field++)) { if(firstfield) firstfield = false; else o << '\t'; o << h.seed; } } o << '\n'; } while(spill); }
static String getCharSetErrorDisplay(Bitset const & set) { return set.toString(escape<Char>); }
TEST(Bitset, size) { Bitset<4> bs; EXPECT_EQ(bs.size(), 4); }
static void print_resources(const Compiler &compiler, const char *tag, const SmallVector<Resource> &resources) { fprintf(stderr, "%s\n", tag); fprintf(stderr, "=============\n\n"); bool print_ssbo = !strcmp(tag, "ssbos"); for (auto &res : resources) { auto &type = compiler.get_type(res.type_id); if (print_ssbo && compiler.buffer_is_hlsl_counter_buffer(res.id)) continue; // If we don't have a name, use the fallback for the type instead of the variable // for SSBOs and UBOs since those are the only meaningful names to use externally. // Push constant blocks are still accessed by name and not block name, even though they are technically Blocks. bool is_push_constant = compiler.get_storage_class(res.id) == StorageClassPushConstant; bool is_block = compiler.get_decoration_bitset(type.self).get(DecorationBlock) || compiler.get_decoration_bitset(type.self).get(DecorationBufferBlock); bool is_sized_block = is_block && (compiler.get_storage_class(res.id) == StorageClassUniform || compiler.get_storage_class(res.id) == StorageClassUniformConstant); uint32_t fallback_id = !is_push_constant && is_block ? res.base_type_id : res.id; uint32_t block_size = 0; uint32_t runtime_array_stride = 0; if (is_sized_block) { auto &base_type = compiler.get_type(res.base_type_id); block_size = uint32_t(compiler.get_declared_struct_size(base_type)); runtime_array_stride = uint32_t(compiler.get_declared_struct_size_runtime_array(base_type, 1) - compiler.get_declared_struct_size_runtime_array(base_type, 0)); } Bitset mask; if (print_ssbo) mask = compiler.get_buffer_block_flags(res.id); else mask = compiler.get_decoration_bitset(res.id); string array; for (auto arr : type.array) array = join("[", arr ? convert_to_string(arr) : "", "]") + array; fprintf(stderr, " ID %03u : %s%s", res.id, !res.name.empty() ? res.name.c_str() : compiler.get_fallback_name(fallback_id).c_str(), array.c_str()); if (mask.get(DecorationLocation)) fprintf(stderr, " (Location : %u)", compiler.get_decoration(res.id, DecorationLocation)); if (mask.get(DecorationDescriptorSet)) fprintf(stderr, " (Set : %u)", compiler.get_decoration(res.id, DecorationDescriptorSet)); if (mask.get(DecorationBinding)) fprintf(stderr, " (Binding : %u)", compiler.get_decoration(res.id, DecorationBinding)); if (mask.get(DecorationInputAttachmentIndex)) fprintf(stderr, " (Attachment : %u)", compiler.get_decoration(res.id, DecorationInputAttachmentIndex)); if (mask.get(DecorationNonReadable)) fprintf(stderr, " writeonly"); if (mask.get(DecorationNonWritable)) fprintf(stderr, " readonly"); if (is_sized_block) { fprintf(stderr, " (BlockSize : %u bytes)", block_size); if (runtime_array_stride) fprintf(stderr, " (Unsized array stride: %u bytes)", runtime_array_stride); } uint32_t counter_id = 0; if (print_ssbo && compiler.buffer_get_hlsl_counter_buffer(res.id, counter_id)) fprintf(stderr, " (HLSL counter buffer ID: %u)", counter_id); fprintf(stderr, "\n"); } fprintf(stderr, "=============\n\n"); }