bitmapper 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,6 +4,7 @@
4
4
  #include <math.h>
5
5
 
6
6
  #include "bit_mapper_wrap.h"
7
+ #include "helper.h"
7
8
 
8
9
  /*
9
10
  Creates empty Bitmapper*. Without BitBucket pointers.
@@ -51,23 +52,47 @@ void free_map(Bitmapper* map){
51
52
  if(map->bkts != NULL) free(map->bkts);
52
53
  }
53
54
 
55
+ /*
56
+ Enables filtering on given bucket, this is a whitelist filtering pattern.
57
+ @param BitBucket* the pointer to the bucket
58
+ */
54
59
  void enable_filter(Bitmapper* map){
55
60
  map->filter_enabled = 1;
56
61
  }
57
62
 
63
+ /*
64
+ Disble filtering on given bucket
65
+ @param BitBucket* the pointer to the bucket
66
+ */
58
67
  void disable_filter(Bitmapper* map){
59
68
  map->filter_enabled = 0;
60
69
  }
61
70
 
71
+ /*
72
+ Add the given number to whitelist
73
+ @param BitBucket* the pointer to the bucket
74
+ @param unsigned long long int the number to set
75
+ */
62
76
  int set_in_filter(Bitmapper* map, unsigned long long int number){
63
77
  map->filter_enabled = 1;
64
78
  return bit_bucket_set_bit(map->filter_status, number);
65
79
  }
66
80
 
81
+ /*
82
+ Gives the status
83
+ @param BitBucket* the pointer to the bucket
84
+ @param unsigned long long int the number to set
85
+ */
67
86
  int status_for_filter(Bitmapper* map, unsigned long long int number){
68
87
  return bit_bucket_get_bit(map->filter_status, number);
69
88
  }
70
89
 
90
+
91
+ /*
92
+ Removes the given number from whitelist
93
+ @param BitBucket* the pointer to the bucket
94
+ @param unsigned long long int the number to set
95
+ */
71
96
  int clear_in_filter(Bitmapper* map, unsigned long long int number){
72
97
  return bit_bucket_clear_bit(map->filter_status, number);
73
98
  }
@@ -250,13 +275,10 @@ int dump_all_to_file(Bitmapper* map, FILE* fp){
250
275
  }
251
276
 
252
277
  /*
253
- Dumps the bucket's(if available) bit values into the given file
278
+ Dumps the bucket's(if not available create empty) bit values into the given file
254
279
  @param Bitmapper* the mapepr object to use
255
280
  @param FILE* the file pointer to the opened file
256
281
  @param unsigned long long int index to the bucket to consider
257
-
258
- TODO: If the bucket doesn't exist, dump empty string equal to the
259
- size of the bucket
260
282
  */
261
283
  int dump_bucket_str_to_file(Bitmapper* map, FILE* fp,unsigned long long int bkt_index){
262
284
  unsigned char* raw = (unsigned char*)malloc(map->bkt_size/8+1);
@@ -273,12 +295,47 @@ int dump_bucket_str_to_file(Bitmapper* map, FILE* fp,unsigned long long int bkt_
273
295
  if(raw != NULL) free(raw);
274
296
  return 0;
275
297
  }
298
+ /*
299
+ Dumps a range of bucket's(in sequence) bit values into the given file
300
+ @param Bitmapper* the mapepr object to use
301
+ @param FILE* the file pointer to the opened file
302
+ @param unsigned long long int start index to the bucket to consider(inclusive)
303
+ @param unsigned long long int end index to the bucket to consider(inclusive)
304
+ */
305
+ int dump_bucket_range_str_to_file(Bitmapper* map, FILE* fp,
306
+ unsigned long long int start,
307
+ unsigned long long int end){
308
+ BitBucket* tmp;
309
+ unsigned long long int i;
310
+ FileBuffer buf;
311
+ if(map->bkt_count<=end) return 1;
312
+ buf.buffer = '\0';
313
+ buf.buff_fill_count = 0;
314
+ buf.string = (unsigned char*)malloc(map->bkt_size/8+1);
315
+ buf.str_length = map->bkt_size;
316
+
317
+ for(i=start; i<=end ;i++){
318
+ if(map->bkts[i] == NULL)
319
+ tmp = bit_bucket_create(map->bkt_size);
320
+ else
321
+ tmp = map->bkts[i];
322
+
323
+ bit_bucket_string(tmp, buf.string);
324
+ append_to_file(fp, buf);
325
+
326
+ if(map->bkts[i] == NULL)
327
+ bit_bucket_free(tmp);
328
+ }
329
+ if(buf.string != NULL) free(buf.string);
330
+ buf.str_length = 0;
331
+ append_to_file(fp, buf);
332
+ return 0;
333
+ }
276
334
 
277
335
  /*
278
336
  Dumps all bucket's bit values into the given file
279
337
  @param Bitmapper* the mapepr object to use
280
338
  @param FILE* the file pointer to the opened file
281
- @param unsigned long long int the length of index
282
339
  */
283
340
  int dump_all_str_to_file(Bitmapper* map, FILE* fp){
284
341
  unsigned long long int i;
@@ -1,4 +1,5 @@
1
1
  #include "bit_bucket.h"
2
+ #include "helper.h"
2
3
 
3
4
  #ifndef BIT_MAPPER_WRAP
4
5
  #define BIT_MAPPER_WRAP
@@ -24,7 +25,7 @@ BitBucket* create_bucket_for(BitBucket*, unsigned long long int);
24
25
 
25
26
  void enable_filter(Bitmapper*);
26
27
  void disable_filter(Bitmapper*);
27
- int set_for_filter(Bitmapper* , unsigned long long int);
28
+ int set_in_filter(Bitmapper* , unsigned long long int);
28
29
  int status_for_filter(Bitmapper*, unsigned long long int );
29
30
  int clear_in_filter(Bitmapper*, unsigned long long int);
30
31
 
@@ -41,7 +42,10 @@ int dump_bucket_to_file(Bitmapper*, FILE* , unsigned long long int);
41
42
  int dump_all_to_file(Bitmapper* , FILE* );
42
43
 
43
44
  int dump_bucket_str_to_file(Bitmapper* , FILE* ,unsigned long long int );
45
+ int dump_bucket_range_str_to_file(Bitmapper*, FILE* , unsigned long long int ,
46
+ unsigned long long int );
44
47
  int dump_all_str_to_file(Bitmapper* , FILE*);
48
+
45
49
  int load_str_file_to_bucket(Bitmapper* , FILE* ,unsigned long long int);
46
50
  int load_str_file(Bitmapper* , FILE*);
47
51
 
@@ -115,6 +115,17 @@ VALUE bm_load_str_to_bkt(VALUE self, VALUE file_str, VALUE index){
115
115
  return self;
116
116
  }
117
117
 
118
+ VALUE bm_dump_range_bkt_str(VALUE self, VALUE file_str, VALUE start, VALUE end){
119
+ Bitmapper* map;
120
+ FILE *fp;
121
+ Data_Get_Struct(self, Bitmapper, map);
122
+ fp = fopen(RSTRING_PTR(StringValue(file_str)), "w");
123
+ if(fp == NULL) rb_raise(rb_eIOError, "file counld not be opened");
124
+ dump_bucket_range_str_to_file(map, fp, NUM2LL(start), NUM2LL(end));
125
+ fclose(fp);
126
+ return self;
127
+ }
128
+
118
129
  VALUE bm_dump_bkt_str(VALUE self, VALUE file_str, VALUE index){
119
130
  Bitmapper* map;
120
131
  FILE *fp;
@@ -252,6 +263,7 @@ void Init_bitmapper(){
252
263
  rb_define_method(rb_cBitmapper, "clear_filters", bm_clear_filters, 1);
253
264
 
254
265
  rb_define_method(rb_cBitmapper, "load_from_str", bm_load_str_to_bkt, 2);
266
+ rb_define_method(rb_cBitmapper, "dump_range_to_str", bm_dump_range_bkt_str, 3);
255
267
  rb_define_method(rb_cBitmapper, "dump_to_str", bm_dump_bkt_str, 2);
256
268
 
257
269
  rb_define_method(rb_cBitmapper, "status?", bm_num_status, 1);
@@ -0,0 +1,60 @@
1
+ #include <stdio.h>
2
+ #include <stdlib.h>
3
+
4
+ #include "helper.h"
5
+
6
+ /*
7
+ Takes a string
8
+ if buffer.len > 0
9
+ * fills the buffer from string
10
+ * shift's the string to left
11
+ * write the buffer to file
12
+ end
13
+ * write the string to file with byte boundry
14
+ * copy rest of string bits to buffer if present
15
+ */
16
+ FileBuffer append_to_file(FILE* fp, FileBuffer buf){
17
+ int shift, nxt_bits, final_bits;
18
+ unsigned long long int str_byte_len, str_bytes_to_write, i;
19
+
20
+ nxt_bits = buf.str_length % 8;
21
+ /* no shift if string or buffer is zero */
22
+ if(buf.buff_fill_count == 0 || buf.str_length == 0) shift = 0;
23
+ else shift = (8 - buf.buff_fill_count);
24
+ final_bits = (shift <= nxt_bits) ? nxt_bits - shift : 8 - (shift - nxt_bits);
25
+ str_byte_len = buf.str_length / 8;
26
+ str_bytes_to_write = str_byte_len - ((shift <= nxt_bits) ? 0 : 1);
27
+
28
+ if(buf.buff_fill_count > 0){
29
+ /* fills the buffer from string */
30
+ shift_bits(&(buf.buffer), buf.string, shift);
31
+ /* shift's the string to left */
32
+ for(i=0;i< str_byte_len ; i++){
33
+ shift_bits(&(buf.string[i]), &(buf.string[i+1]), shift);
34
+ }
35
+ /* write the buffer to file */
36
+ fwrite(&(buf.buffer), 1, 1, fp);
37
+ }
38
+ /* write the string to file with byte boundry */
39
+ if(str_bytes_to_write > 0) fwrite(buf.string, 1, str_bytes_to_write, fp);
40
+ if(nxt_bits != 0){
41
+ buf.buffer = buf.string[str_byte_len];
42
+ buf.buff_fill_count = final_bits;
43
+ printf("new buffer - %X - %d\n", buf.buffer, buf.buff_fill_count);
44
+ }
45
+ return buf;
46
+ }
47
+
48
+ /*
49
+ copies 'num_bits' from src head to dest tail,
50
+ and left shift src by 'num_bits'.
51
+ */
52
+ int shift_bits(char* dest, char* src, int num_bits){
53
+ char tmp;
54
+ char mask = '\xFF';
55
+ if(num_bits == 0) return 0;
56
+ tmp = *src;
57
+ *dest = ((*dest) & mask<<num_bits) | ((unsigned char)tmp)>>(8-num_bits);
58
+ *src = (*src)<<num_bits;
59
+ return 0;
60
+ }
@@ -0,0 +1,14 @@
1
+ #ifndef BIT_WRAP_HELPER
2
+ #define BIT_WRAP_HELPER
3
+
4
+ typedef struct FileBuffer{
5
+ unsigned char buffer;
6
+ int buff_fill_count;
7
+ unsigned char* string;
8
+ unsigned long long int str_length; /* number of bits */
9
+ }FileBuffer;
10
+
11
+ FileBuffer append_to_file(FILE*, FileBuffer);
12
+ int shift_bits(char* , char* , int);
13
+
14
+ #endif /* BIT_WRAP_HELPER */
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitmapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-16 00:00:00.000000000 Z
12
+ date: 2012-07-19 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A fast, in-memory, bit map filter!
15
15
  email: gouthamvel@gmail.com
@@ -22,8 +22,10 @@ files:
22
22
  - ext/bitmapper/bitmapper.c
23
23
  - ext/bitmapper/bit_bucket.c
24
24
  - ext/bitmapper/bit_mapper_wrap.c
25
+ - ext/bitmapper/helper.c
25
26
  - ext/bitmapper/bit_bucket.h
26
27
  - ext/bitmapper/bit_mapper_wrap.h
28
+ - ext/bitmapper/helper.h
27
29
  - ext/bitmapper/extconf.rb
28
30
  homepage: ''
29
31
  licenses: []