int main() { size_t N; scanf ("%zu", &N); void* bitset = create_bitset (N + 1); set_bitset (bitset, 0, 1); set_bitset (bitset, 1, 1); for (size_t i = 2; i*i <= N; ++i) { if (!get_bitset (bitset, i)) { for (size_t j = i*i; j <= N; j += i) { set_bitset (bitset, j, 1); } } } size_t prime_count = 0; for (size_t i = 0; i <= N; ++i) { if (!get_bitset (bitset, i)) { ++prime_count; } } destroy_bitset (bitset); printf ("%zu\n", prime_count); }
static VALUE rb_bitset_aset(VALUE self, VALUE index, VALUE value) { Bitset * bs = get_bitset(self); int idx = NUM2INT(index); validate_index(bs, idx); assign_bit(bs, idx, value); return Qtrue; }
int main() { size_t infected, count; scanf ("%zu %zu", &infected, &count); void* bitset = create_bitset (count); for (size_t i = 0; i < infected; ++i) { size_t nr; scanf ("%zu", &nr); size_t delta = nr; for (size_t j = nr; j < count; j += delta++) { set_bitset (bitset, j, 1); } } size_t infected_count = 0; for (size_t i = 0; i < count; ++i) { if (get_bitset (bitset, i)) { ++infected_count; } } destroy_bitset (bitset); printf ("%zu\n", infected_count); }
static VALUE rb_bitset_hamming(VALUE self, VALUE other) { Bitset * bs = get_bitset(self); Bitset * other_bs = get_bitset(other); int max = ((bs->len-1) >> 6) + 1; int other_max = ((other_bs->len-1) >> 6) + 1; max = MIN(max, other_max); int count = 0; int i; for(i = 0; i < max; i++) { uint64_t segment = bs->data[i]; uint64_t other_segment = other_bs->data[i]; count += __builtin_popcountll(segment ^ other_segment); } return INT2NUM(count); }
static VALUE rb_bitset_each(VALUE self) { Bitset * bs = get_bitset(self); int i; for(i = 0; i < bs->len; i++) { rb_yield(get_bit(bs, i) > 0 ? Qtrue : Qfalse); } return self; }
static VALUE rb_bitset_marshall_dump(VALUE self) { Bitset * bs = get_bitset(self); VALUE hash = rb_hash_new(); VALUE data = rb_str_new(bs->data, BYTES(bs)); rb_hash_aset(hash, ID2SYM(rb_intern("len")), UINT2NUM(bs->len)); rb_hash_aset(hash, ID2SYM(rb_intern("data")), data); return hash; }
int main(void) { int x, i, j; scanf("%d", &x); void* bitset = init_bitset(x + 1); for(i = 2; i * i <= x; i++) if(!get_bitset(bitset, i)) for(j = i * i; j <= x; j += i) set_bitset(bitset, j, 1); int count = 0; for(i = 2; i <= x; i++) if(!get_bitset(bitset, i)) count++; printf("%d", count); destroy_bitset(bitset); return 0; }
static VALUE rb_bitset_to_binary_array(VALUE self) { Bitset * bs = get_bitset(self); int i; VALUE array = rb_ary_new2(bs->len / 2); for(i = 0; i < bs->len; i++) { rb_ary_push(array, INT2NUM(get_bit(bs, i) > 0 ? 1 : 0)); } return array; }
static VALUE rb_bitset_clear(int argc, VALUE * argv, VALUE self) { int i; Bitset * bs = get_bitset(self); for(i = 0; i < argc; i++) { VALUE index = argv[i]; int idx = NUM2INT(index); validate_index(bs, idx); clear_bit(bs, idx); } return Qtrue; }
static VALUE rb_bitset_xor(VALUE self, VALUE other) { Bitset * bs = get_bitset(self); Bitset * other_bs = get_bitset(other); Bitset * new_bs = bitset_new(); bitset_setup(new_bs, MIN(bs->len, other_bs->len)); int max = ((bs->len-1) >> 6) + 1; int other_max = ((other_bs->len-1) >> 6) + 1; max = MIN(max, other_max); int i; for(i = 0; i < max; i++) { uint64_t segment = bs->data[i]; uint64_t other_segment = other_bs->data[i]; new_bs->data[i] = segment ^ other_segment; } return Data_Wrap_Struct(cBitset, 0, bitset_free, new_bs); }
static VALUE rb_bitset_to_s(VALUE self) { Bitset * bs = get_bitset(self); int i; char * data = malloc(bs->len + 1); for(i = 0; i < bs->len; i++) { data[i] = get_bit(bs, i) ? '1' : '0'; } data[bs->len] = 0; return rb_str_new2(data); }
static VALUE rb_bitset_marshall_load(VALUE self, VALUE hash) { Bitset * bs = get_bitset(self); int len = NUM2INT(rb_hash_aref(hash, ID2SYM(rb_intern("len")))); VALUE data = rb_hash_aref(hash, ID2SYM(rb_intern("data"))); bitset_setup(bs, len); bs->data = (uint64_t *) calloc(INTS(bs), sizeof(uint64_t)); memcpy(bs->data, RSTRING_PTR(data), BYTES(bs)); return Qnil; }
static VALUE rb_bitset_cardinality(VALUE self) { Bitset * bs = get_bitset(self); int i; int max = ((bs->len-1) >> 6) + 1; int count = 0; for(i = 0; i < max; i++) { uint64_t segment = bs->data[i]; if(i+1 == max && (bs->len & 0x3F)) segment &= ((((uint64_t) 1) << (bs->len & 0x3F)) - 1); count += __builtin_popcountll(segment); } return INT2NUM(count); }
static VALUE rb_bitset_not(VALUE self) { Bitset * bs = get_bitset(self); Bitset * new_bs = bitset_new(); bitset_setup(new_bs, bs->len); int max = ((bs->len-1) >> 6) + 1; int i; for(i = 0; i < max; i++) { uint64_t segment = bs->data[i]; new_bs->data[i] = ~segment; } return Data_Wrap_Struct(cBitset, 0, bitset_free, new_bs); }
//void v_ctl_note(struct MidiTraceNote v) void v_ctl_note(int status, int ch, int note, int vel) { int xl, n, c; unsigned int onoff=0, prev_onoff, check; Bitset *bitset; if( !mac_TraceWindow.show || ch >= 16) return; if( /*ctl_ncurs_mode != NCURS_MODE_TRACE ||*/ selected_channel == ch) return; scr_modified_flag = 1; if(display_velocity_flag) n = '0' + (10 * vel) / 128; else n = note_name_char[note % 12]; c = (COLS - 24) / 12 * 12; if(c <= 0) c = 1; xl=note % c; if( note>=MAX_NOTE_NUM ) return; onoff= UpdateNote( status, ch, note, vel); //draw at first bitset = channel_program_flags + ch; get_bitset(bitset, &prev_onoff, note, 1); onoff <<= (8 * sizeof(onoff) - 1); set_bitset(bitset, &onoff, note, 1); check = has_bitset(bitset); if( prev_onoff && !onoff ) current_voices--; if( !prev_onoff && onoff ){ current_voices++; mac_trc_update_voices(); } }
static VALUE rb_bitset_aref(VALUE self, VALUE index) { Bitset * bs = get_bitset(self); int idx = NUM2INT(index); validate_index(bs, idx); return get_bit(bs, idx) > 0 ? Qtrue : Qfalse; }
static VALUE rb_bitset_size(VALUE self, VALUE len) { Bitset * bs = get_bitset(self); return INT2NUM(bs->len); }
static VALUE rb_bitset_initialize(VALUE self, VALUE len) { Bitset * bs = get_bitset(self); bitset_setup(bs, NUM2INT(len)); return self; }