sparse_array 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/ext/sp_ar.c +262 -41
- data/lib/sparse_array/version.rb +1 -1
- metadata +19 -31
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 47d4908850c44898a7c73e8faa28501500b12ec1
|
4
|
+
data.tar.gz: cada5c68ec9064080013d6071bd4f72bfec2bbb3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b345ff6ff95ec91173e7412ca54f63b96714d9e610145f1cb25ca09d810ffed38ab384021d552cfcbfc5269783335a16f616fe5721ef6f949e1f05e603d6c4a4
|
7
|
+
data.tar.gz: 42aa16e2fe7c96f6b5742e4ec70c1ff099c2db65de31be46e73c6b4f91331b10b8efa02c6882e0ec4c76daeedadb17ba1cf2b888da645ec0871101372a594917
|
data/ext/sp_ar.c
CHANGED
@@ -87,6 +87,7 @@ static inline spar_index_t
|
|
87
87
|
calc_pos(register spar_table* table, spar_index_t key)
|
88
88
|
{
|
89
89
|
uint64_t res = (uint64_t)key * 0x85ebca6bull;
|
90
|
+
res = (res >> 29) * 0x85ebca6bull;
|
90
91
|
return ((spar_index_t)res ^ (spar_index_t)(res >> 32)) % table->num_bins;
|
91
92
|
}
|
92
93
|
|
@@ -527,21 +528,14 @@ static VALUE
|
|
527
528
|
sparse_array_each(VALUE self)
|
528
529
|
{
|
529
530
|
spar_table *table;
|
530
|
-
VALUE
|
531
|
-
long i, size;
|
532
|
-
VALUE *p;
|
531
|
+
VALUE y[2] = {Qnil, Qnil};
|
533
532
|
RETURN_ENUMERATOR(self, 0, 0);
|
534
533
|
GetSparseArray(self, table);
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
st_data_t v = Qnil;
|
541
|
-
if (spar_lookup(table, k, &v))
|
542
|
-
rb_yield(rb_assoc_new(p[i], (VALUE)v));
|
543
|
-
}
|
544
|
-
RB_GC_GUARD(keys);
|
534
|
+
SPAR_FOREACH_START(table);
|
535
|
+
y[0] = UINT2NUM(entry->key);
|
536
|
+
y[1] = (VALUE)value;
|
537
|
+
rb_yield_values2(2, y);
|
538
|
+
SPAR_FOREACH_END();
|
545
539
|
return self;
|
546
540
|
}
|
547
541
|
|
@@ -549,21 +543,12 @@ static VALUE
|
|
549
543
|
sparse_array_each_key(VALUE self)
|
550
544
|
{
|
551
545
|
spar_table *table;
|
552
|
-
VALUE keys;
|
553
|
-
long i, size;
|
554
|
-
VALUE *p;
|
555
546
|
RETURN_ENUMERATOR(self, 0, 0);
|
556
547
|
GetSparseArray(self, table);
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
spar_index_t k = NUM2UINT(p[i]);
|
562
|
-
st_data_t v = Qnil;
|
563
|
-
if (spar_lookup(table, k, &v))
|
564
|
-
rb_yield(p[i]);
|
565
|
-
}
|
566
|
-
RB_GC_GUARD(keys);
|
548
|
+
SPAR_FOREACH_START(table);
|
549
|
+
(void)value;
|
550
|
+
rb_yield(UINT2NUM(entry->key));
|
551
|
+
SPAR_FOREACH_END();
|
567
552
|
return self;
|
568
553
|
}
|
569
554
|
|
@@ -571,21 +556,11 @@ static VALUE
|
|
571
556
|
sparse_array_each_value(VALUE self)
|
572
557
|
{
|
573
558
|
spar_table *table;
|
574
|
-
VALUE keys;
|
575
|
-
long i, size;
|
576
|
-
VALUE *p;
|
577
559
|
RETURN_ENUMERATOR(self, 0, 0);
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
for(i=0; i<size; i++) {
|
583
|
-
spar_index_t k = NUM2UINT(p[i]);
|
584
|
-
st_data_t v = Qnil;
|
585
|
-
if (spar_lookup(table, k, &v))
|
586
|
-
rb_yield(v);
|
587
|
-
}
|
588
|
-
RB_GC_GUARD(keys);
|
560
|
+
GetSparseArrayInt(self, table);
|
561
|
+
SPAR_FOREACH_START(table);
|
562
|
+
rb_yield((VALUE)(value));
|
563
|
+
SPAR_FOREACH_END();
|
589
564
|
return self;
|
590
565
|
}
|
591
566
|
|
@@ -632,9 +607,233 @@ sparse_array_inspect(VALUE self)
|
|
632
607
|
return rb_exec_recursive(sparse_array_inspect_rec, self, 0);
|
633
608
|
}
|
634
609
|
|
610
|
+
/******** Ruby Sparse Int Array binding ********/
|
611
|
+
|
612
|
+
static const rb_data_type_t SparseArrayInt_data_type = {
|
613
|
+
"SparseArrayIntC",
|
614
|
+
{NULL, sparse_array_delete, sparse_array_memsize}
|
615
|
+
};
|
616
|
+
|
617
|
+
#define GetSparseArrayInt(value, pointer) \
|
618
|
+
TypedData_Get_Struct((value), spar_table, &SparseArrayInt_data_type, (pointer))
|
619
|
+
|
620
|
+
static VALUE
|
621
|
+
sparse_array_int_alloc(VALUE klass)
|
622
|
+
{
|
623
|
+
spar_table* table = spar_new_table();
|
624
|
+
return TypedData_Wrap_Struct(klass, &SparseArrayInt_data_type, table);
|
625
|
+
}
|
626
|
+
|
627
|
+
#if SIZEOF_LONG == SIZEOF_VOIDP
|
628
|
+
#define st_data2num(res) LONG2NUM((long)res)
|
629
|
+
#define num2st_data(val) NUM2LONG(val)
|
630
|
+
#else
|
631
|
+
#define st_data2num(res) LL2NUM((LONG_LONG)res)
|
632
|
+
#define num2st_data(val) NUM2LL(val)
|
633
|
+
#endif
|
634
|
+
|
635
|
+
static VALUE
|
636
|
+
sparse_array_int_get(VALUE self, VALUE ri)
|
637
|
+
{
|
638
|
+
spar_table *table;
|
639
|
+
spar_index_t i = NUM2UINT(ri);
|
640
|
+
st_data_t res = 0;
|
641
|
+
GetSparseArrayInt(self, table);
|
642
|
+
if (spar_lookup(table, i, &res)) {
|
643
|
+
return st_data2num(res);
|
644
|
+
}
|
645
|
+
return Qnil;
|
646
|
+
}
|
647
|
+
|
648
|
+
static VALUE
|
649
|
+
sparse_array_int_include(VALUE self, VALUE ri)
|
650
|
+
{
|
651
|
+
spar_table *table;
|
652
|
+
spar_index_t i = NUM2UINT(ri);
|
653
|
+
st_data_t res = Qnil;
|
654
|
+
GetSparseArrayInt(self, table);
|
655
|
+
if (spar_lookup(table, i, &res))
|
656
|
+
return Qtrue;
|
657
|
+
else
|
658
|
+
return Qfalse;
|
659
|
+
}
|
660
|
+
|
661
|
+
static VALUE
|
662
|
+
sparse_array_int_fetch(VALUE self, VALUE ri, VALUE def)
|
663
|
+
{
|
664
|
+
spar_table *table;
|
665
|
+
spar_index_t i = NUM2UINT(ri);
|
666
|
+
st_data_t res = 0;
|
667
|
+
GetSparseArrayInt(self, table);
|
668
|
+
if (spar_lookup(table, i, &res))
|
669
|
+
return st_data2num(res);
|
670
|
+
else
|
671
|
+
return def;
|
672
|
+
}
|
673
|
+
|
674
|
+
static VALUE
|
675
|
+
sparse_array_int_set(VALUE self, VALUE ri, VALUE val)
|
676
|
+
{
|
677
|
+
spar_table *table;
|
678
|
+
spar_index_t i = NUM2UINT(ri);
|
679
|
+
st_data_t val_ = num2st_data(val);
|
680
|
+
GetSparseArrayInt(self, table);
|
681
|
+
spar_insert(table, i, val_);
|
682
|
+
return val;
|
683
|
+
}
|
684
|
+
|
685
|
+
static VALUE
|
686
|
+
sparse_array_int_del(VALUE self, VALUE ri)
|
687
|
+
{
|
688
|
+
spar_table *table;
|
689
|
+
spar_index_t i = NUM2UINT(ri);
|
690
|
+
st_data_t res = 0;
|
691
|
+
GetSparseArrayInt(self, table);
|
692
|
+
if (spar_delete(table, i, &res))
|
693
|
+
return st_data2num(res);
|
694
|
+
return Qnil;
|
695
|
+
}
|
696
|
+
|
697
|
+
static VALUE
|
698
|
+
sparse_array_int_size(VALUE self)
|
699
|
+
{
|
700
|
+
spar_table *table;
|
701
|
+
GetSparseArrayInt(self, table);
|
702
|
+
return UINT2NUM(table->num_entries);
|
703
|
+
}
|
704
|
+
|
705
|
+
static VALUE
|
706
|
+
sparse_array_int_empty_p(VALUE self)
|
707
|
+
{
|
708
|
+
spar_table *table;
|
709
|
+
GetSparseArrayInt(self, table);
|
710
|
+
return table->num_entries ? Qfalse : Qtrue;
|
711
|
+
}
|
712
|
+
|
713
|
+
static VALUE
|
714
|
+
sparse_array_int_clear(VALUE self)
|
715
|
+
{
|
716
|
+
spar_table *table;
|
717
|
+
GetSparseArrayInt(self, table);
|
718
|
+
spar_clear(table);
|
719
|
+
return self;
|
720
|
+
}
|
721
|
+
|
722
|
+
static VALUE
|
723
|
+
sparse_array_int_keys(VALUE self)
|
724
|
+
{
|
725
|
+
spar_table *table;
|
726
|
+
VALUE res;
|
727
|
+
GetSparseArrayInt(self, table);
|
728
|
+
res = rb_ary_new2(table->num_entries);
|
729
|
+
SPAR_FOREACH_START(table);
|
730
|
+
(void)value;
|
731
|
+
rb_ary_push(res, UINT2NUM(entry->key));
|
732
|
+
SPAR_FOREACH_END();
|
733
|
+
return res;
|
734
|
+
}
|
735
|
+
|
736
|
+
static VALUE
|
737
|
+
sparse_array_int_values(VALUE self)
|
738
|
+
{
|
739
|
+
spar_table *table;
|
740
|
+
VALUE res;
|
741
|
+
GetSparseArrayInt(self, table);
|
742
|
+
res = rb_ary_new2(table->num_entries);
|
743
|
+
SPAR_FOREACH_START(table);
|
744
|
+
rb_ary_push(res, st_data2num((VALUE)value));
|
745
|
+
SPAR_FOREACH_END();
|
746
|
+
return res;
|
747
|
+
}
|
748
|
+
|
749
|
+
static VALUE
|
750
|
+
sparse_array_int_each(VALUE self)
|
751
|
+
{
|
752
|
+
spar_table *table;
|
753
|
+
VALUE y[2] = {Qnil, Qnil};
|
754
|
+
RETURN_ENUMERATOR(self, 0, 0);
|
755
|
+
GetSparseArrayInt(self, table);
|
756
|
+
SPAR_FOREACH_START(table);
|
757
|
+
y[0] = UINT2NUM(entry->key);
|
758
|
+
y[1] = st_data2num((VALUE)value);
|
759
|
+
rb_yield_values2(2, y);
|
760
|
+
SPAR_FOREACH_END();
|
761
|
+
return self;
|
762
|
+
}
|
763
|
+
|
764
|
+
static VALUE
|
765
|
+
sparse_array_int_each_key(VALUE self)
|
766
|
+
{
|
767
|
+
spar_table *table;
|
768
|
+
RETURN_ENUMERATOR(self, 0, 0);
|
769
|
+
GetSparseArrayInt(self, table);
|
770
|
+
SPAR_FOREACH_START(table);
|
771
|
+
(void)value;
|
772
|
+
rb_yield(UINT2NUM(entry->key));
|
773
|
+
SPAR_FOREACH_END();
|
774
|
+
return self;
|
775
|
+
}
|
776
|
+
|
777
|
+
static VALUE
|
778
|
+
sparse_array_int_each_value(VALUE self)
|
779
|
+
{
|
780
|
+
spar_table *table;
|
781
|
+
RETURN_ENUMERATOR(self, 0, 0);
|
782
|
+
GetSparseArrayInt(self, table);
|
783
|
+
SPAR_FOREACH_START(table);
|
784
|
+
rb_yield(st_data2num(value));
|
785
|
+
SPAR_FOREACH_END();
|
786
|
+
return self;
|
787
|
+
}
|
788
|
+
|
789
|
+
static VALUE
|
790
|
+
sparse_array_int_init_copy(VALUE self, VALUE copy)
|
791
|
+
{
|
792
|
+
spar_table *table;
|
793
|
+
spar_table *copied;
|
794
|
+
GetSparseArrayInt(self, table);
|
795
|
+
GetSparseArrayInt(copy, copied);
|
796
|
+
rb_obj_init_copy(self, copy);
|
797
|
+
spar_copy_to(table, copied);
|
798
|
+
return copy;
|
799
|
+
}
|
800
|
+
|
801
|
+
static VALUE
|
802
|
+
sparse_array_int_inspect_rec(VALUE self, VALUE dummy, int recur)
|
803
|
+
{
|
804
|
+
VALUE str;
|
805
|
+
spar_table *table;
|
806
|
+
GetSparseArrayInt(self, table);
|
807
|
+
|
808
|
+
if (recur) return rb_usascii_str_new2("<SparseArrayInt ...>");
|
809
|
+
str = rb_str_buf_new2("<SparseArrayInt");
|
810
|
+
SPAR_FOREACH_START(table);
|
811
|
+
#if SIZEOF_LONG == SIZEOF_VOIDP
|
812
|
+
rb_str_catf(str, " %u=>%lu", entry->key, value);
|
813
|
+
#else
|
814
|
+
rb_str_catf(str, " %u=>%llu", entry->key, value);
|
815
|
+
#endif
|
816
|
+
SPAR_FOREACH_END();
|
817
|
+
rb_str_buf_cat2(str, ">");
|
818
|
+
OBJ_INFECT(str, self);
|
819
|
+
|
820
|
+
return str;
|
821
|
+
}
|
822
|
+
|
823
|
+
static VALUE
|
824
|
+
sparse_array_int_inspect(VALUE self)
|
825
|
+
{
|
826
|
+
spar_table *table;
|
827
|
+
GetSparseArrayInt(self, table);
|
828
|
+
if (table->num_entries == 0)
|
829
|
+
return rb_usascii_str_new2("<SparseArrayInt>");
|
830
|
+
return rb_exec_recursive(sparse_array_int_inspect_rec, self, 0);
|
831
|
+
}
|
832
|
+
|
635
833
|
void
|
636
834
|
Init_sparse_array() {
|
637
|
-
VALUE cls_sparse_array
|
835
|
+
VALUE cls_sparse_array, cls_sparse_array_int;
|
836
|
+
cls_sparse_array = rb_define_class("SparseArray", rb_cObject);
|
638
837
|
rb_define_alloc_func(cls_sparse_array, sparse_array_alloc);
|
639
838
|
rb_define_method(cls_sparse_array, "[]", sparse_array_get, 1);
|
640
839
|
rb_define_method(cls_sparse_array, "fetch", sparse_array_fetch, 2);
|
@@ -655,4 +854,26 @@ Init_sparse_array() {
|
|
655
854
|
rb_define_method(cls_sparse_array, "inspect", sparse_array_inspect, 0);
|
656
855
|
rb_define_method(cls_sparse_array, "initialize_copy", sparse_array_init_copy, 1);
|
657
856
|
rb_include_module(cls_sparse_array, rb_mEnumerable);
|
857
|
+
|
858
|
+
cls_sparse_array_int = rb_define_class("SparseArrayInt", rb_cObject);
|
859
|
+
rb_define_alloc_func(cls_sparse_array_int, sparse_array_int_alloc);
|
860
|
+
rb_define_method(cls_sparse_array_int, "[]", sparse_array_int_get, 1);
|
861
|
+
rb_define_method(cls_sparse_array_int, "fetch", sparse_array_int_fetch, 2);
|
862
|
+
rb_define_method(cls_sparse_array_int, "[]=", sparse_array_int_set, 2);
|
863
|
+
rb_define_method(cls_sparse_array_int, "delete", sparse_array_int_del, 1);
|
864
|
+
rb_define_method(cls_sparse_array_int, "clear", sparse_array_int_clear, 0);
|
865
|
+
rb_define_method(cls_sparse_array_int, "empty?", sparse_array_int_empty_p, 0);
|
866
|
+
rb_define_method(cls_sparse_array_int, "size", sparse_array_int_size, 1);
|
867
|
+
rb_define_method(cls_sparse_array_int, "count", sparse_array_int_size, 1);
|
868
|
+
rb_define_method(cls_sparse_array_int, "include?", sparse_array_int_include, 1);
|
869
|
+
rb_define_method(cls_sparse_array_int, "has_key?", sparse_array_int_include, 1);
|
870
|
+
rb_define_method(cls_sparse_array_int, "keys", sparse_array_int_keys, 0);
|
871
|
+
rb_define_method(cls_sparse_array_int, "values", sparse_array_int_values, 0);
|
872
|
+
rb_define_method(cls_sparse_array_int, "each", sparse_array_int_each, 0);
|
873
|
+
rb_define_method(cls_sparse_array_int, "each_pair", sparse_array_int_each, 0);
|
874
|
+
rb_define_method(cls_sparse_array_int, "each_key", sparse_array_int_each_key, 0);
|
875
|
+
rb_define_method(cls_sparse_array_int, "each_value", sparse_array_int_each_value, 0);
|
876
|
+
rb_define_method(cls_sparse_array_int, "inspect", sparse_array_int_inspect, 0);
|
877
|
+
rb_define_method(cls_sparse_array_int, "initialize_copy", sparse_array_int_init_copy, 1);
|
878
|
+
rb_include_module(cls_sparse_array_int, rb_mEnumerable);
|
658
879
|
}
|
data/lib/sparse_array/version.rb
CHANGED
metadata
CHANGED
@@ -1,48 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sparse_array
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.4
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Sokolov Yura aka funny_falcon
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2015-05-26 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.3'
|
20
|
-
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
21
22
|
version_requirements: !ruby/object:Gem::Requirement
|
22
23
|
requirements:
|
23
|
-
- - ~>
|
24
|
+
- - "~>"
|
24
25
|
- !ruby/object:Gem::Version
|
25
26
|
version: '1.3'
|
26
|
-
none: false
|
27
|
-
prerelease: false
|
28
|
-
name: bundler
|
29
|
-
type: :development
|
30
27
|
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
31
29
|
requirement: !ruby/object:Gem::Requirement
|
32
30
|
requirements:
|
33
|
-
- -
|
31
|
+
- - ">="
|
34
32
|
- !ruby/object:Gem::Version
|
35
33
|
version: '0'
|
36
|
-
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
37
36
|
version_requirements: !ruby/object:Gem::Requirement
|
38
37
|
requirements:
|
39
|
-
- -
|
38
|
+
- - ">="
|
40
39
|
- !ruby/object:Gem::Version
|
41
40
|
version: '0'
|
42
|
-
none: false
|
43
|
-
prerelease: false
|
44
|
-
name: rake
|
45
|
-
type: :development
|
46
41
|
description: Sparse Array - map from integers (0..2**32-1) to objects
|
47
42
|
email:
|
48
43
|
- funny.falcon@gmail.com
|
@@ -51,15 +46,16 @@ extensions:
|
|
51
46
|
- ext/extconf.rb
|
52
47
|
extra_rdoc_files: []
|
53
48
|
files:
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
54
51
|
- ext/extconf.rb
|
55
52
|
- ext/sp_ar.c
|
56
53
|
- lib/sparse_array/version.rb
|
57
|
-
- Gemfile
|
58
|
-
- LICENSE.txt
|
59
54
|
- sparse_array.gemspec
|
60
55
|
homepage: https://github.com/funny-falcon/ruby_sparse_array
|
61
56
|
licenses:
|
62
57
|
- MIT
|
58
|
+
metadata: {}
|
63
59
|
post_install_message:
|
64
60
|
rdoc_options: []
|
65
61
|
require_paths:
|
@@ -67,26 +63,18 @@ require_paths:
|
|
67
63
|
- ext
|
68
64
|
required_ruby_version: !ruby/object:Gem::Requirement
|
69
65
|
requirements:
|
70
|
-
- -
|
66
|
+
- - ">="
|
71
67
|
- !ruby/object:Gem::Version
|
72
|
-
hash: -4038731035297289892
|
73
68
|
version: '0'
|
74
|
-
segments:
|
75
|
-
- 0
|
76
|
-
none: false
|
77
69
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
70
|
requirements:
|
79
|
-
- -
|
71
|
+
- - ">="
|
80
72
|
- !ruby/object:Gem::Version
|
81
|
-
hash: -4038731035297289892
|
82
73
|
version: '0'
|
83
|
-
segments:
|
84
|
-
- 0
|
85
|
-
none: false
|
86
74
|
requirements: []
|
87
75
|
rubyforge_project:
|
88
|
-
rubygems_version:
|
76
|
+
rubygems_version: 2.4.4
|
89
77
|
signing_key:
|
90
|
-
specification_version:
|
78
|
+
specification_version: 4
|
91
79
|
summary: lightweight map from integers to objects
|
92
80
|
test_files: []
|