Class: Usamin::Hash
- Inherits:
-
Value
- Object
- Value
- Usamin::Hash
show all
- Includes:
- Enumerable
- Defined in:
- ext/usamin/usamin.cpp
Instance Method Summary
collapse
Methods inherited from Value
#==, #===, #array?, #eql?, #eval, #eval_r, #frozen?, #hash, #hash?, #object?, #root
Instance Method Details
#[](key) ⇒ Object | nil
Note:
This method has linear time complexity.
778
779
780
781
782
783
784
785
|
# File 'ext/usamin/usamin.cpp', line 778
static VALUE w_hash_operator_indexer(const VALUE self, const VALUE key) {
UsaminValue *value = get_value(self);
check_object(value);
for (auto &m : value->value->GetObject())
if (str_compare_xx(key, m.name))
return eval(m.value, value->root_document);
return Qnil;
}
|
#assoc(key) ⇒ ::Array | nil
Note:
This method has linear time complexity.
792
793
794
795
796
797
798
799
|
# File 'ext/usamin/usamin.cpp', line 792
static VALUE w_hash_assoc(const VALUE self, const VALUE key) {
UsaminValue *value = get_value(self);
check_object(value);
for (auto &m : value->value->GetObject())
if (str_compare_xx(key, m.name))
return rb_assoc_new(eval_str(m.name), eval(m.value, value->root_document));
return Qnil;
}
|
#compact ⇒ ::Hash
804
805
806
807
808
809
810
811
812
|
# File 'ext/usamin/usamin.cpp', line 804
static VALUE w_hash_compact(const VALUE self) {
UsaminValue *value = get_value(self);
check_object(value);
VALUE hash = rb_hash_new();
for (auto &m : value->value->GetObject())
if (!m.value.IsNull())
rb_hash_aset(hash, eval(m.name, value->root_document), eval(m.value, value->root_document));
return hash;
}
|
#dig(*args) ⇒ Object | nil
817
818
819
820
821
822
823
824
825
826
|
# File 'ext/usamin/usamin.cpp', line 817
static VALUE w_hash_dig(const int argc, const VALUE *argv, const VALUE self) {
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
VALUE value = w_hash_operator_indexer(self, argv[0]);
if (argc == 1)
return value;
else if (value == Qnil)
return Qnil;
else
return rb_funcall3(value, id_dig, argc - 1, argv + 1);
}
|
#each {|key, value| ... } ⇒ Enumerator | self
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
|
# File 'ext/usamin/usamin.cpp', line 838
static VALUE w_hash_each(const VALUE self) {
UsaminValue *value = get_value(self);
check_object(value);
RETURN_SIZED_ENUMERATOR(self, 0, nullptr, hash_enum_size);
if (rb_proc_arity(rb_block_proc()) > 1) {
for (auto &m : value->value->GetObject()) {
VALUE args[] = { eval_str(m.name), eval(m.value, value->root_document) };
rb_yield_values2(2, args);
}
} else {
for (auto &m : value->value->GetObject())
rb_yield(rb_assoc_new(eval_str(m.name), eval(m.value, value->root_document)));
}
return self;
}
|
#each_key {|key| ... } ⇒ Enumerator | self
859
860
861
862
863
864
865
866
|
# File 'ext/usamin/usamin.cpp', line 859
static VALUE w_hash_each_key(const VALUE self) {
UsaminValue *value = get_value(self);
check_object(value);
RETURN_SIZED_ENUMERATOR(self, 0, nullptr, hash_enum_size);
for (auto &m : value->value->GetObject())
rb_yield(eval_str(m.name));
return self;
}
|
#each_pair {|key, value| ... } ⇒ Enumerator | self
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
|
# File 'ext/usamin/usamin.cpp', line 838
static VALUE w_hash_each(const VALUE self) {
UsaminValue *value = get_value(self);
check_object(value);
RETURN_SIZED_ENUMERATOR(self, 0, nullptr, hash_enum_size);
if (rb_proc_arity(rb_block_proc()) > 1) {
for (auto &m : value->value->GetObject()) {
VALUE args[] = { eval_str(m.name), eval(m.value, value->root_document) };
rb_yield_values2(2, args);
}
} else {
for (auto &m : value->value->GetObject())
rb_yield(rb_assoc_new(eval_str(m.name), eval(m.value, value->root_document)));
}
return self;
}
|
#each_value {|value| ... } ⇒ Enumerator | self
872
873
874
875
876
877
878
879
|
# File 'ext/usamin/usamin.cpp', line 872
static VALUE w_hash_each_value(const VALUE self) {
UsaminValue *value = get_value(self);
check_object(value);
RETURN_SIZED_ENUMERATOR(self, 0, nullptr, hash_enum_size);
for (auto &m : value->value->GetObject())
rb_yield(eval(m.value, value->root_document));
return self;
}
|
#empty? ⇒ Boolean
881
882
883
884
885
|
# File 'ext/usamin/usamin.cpp', line 881
static VALUE w_hash_isempty(const VALUE self) {
UsaminValue *value = get_value(self);
check_object(value);
return value->value->MemberCount() == 0 ? Qtrue : Qfalse;
}
|
#fetch(key, default = nil) ⇒ Object
#fetch(key) {|key| ... } ⇒ Object
898
899
900
901
902
903
904
905
906
|
# File 'ext/usamin/usamin.cpp', line 898
static VALUE w_hash_fetch(const int argc, const VALUE *argv, const VALUE self) {
rb_check_arity(argc, 1, 2);
UsaminValue *value = get_value(self);
check_object(value);
for (auto &m : value->value->GetObject())
if (str_compare_xx(argv[0], m.name))
return eval(m.value, value->root_document);
return argc == 2 ? argv[1] : rb_block_given_p() ? rb_yield(argv[0]) : Qnil;
}
|
#fetch_values(*args) ⇒ ::Array<Object>
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
|
# File 'ext/usamin/usamin.cpp', line 912
static VALUE w_hash_fetch_values(const int argc, VALUE *argv, const VALUE self) {
UsaminValue *value = get_value(self);
check_object(value);
VALUE ret = rb_ary_new2(argc);
for (int i = 0; i < argc; i++) {
bool found = false;
for (auto &m : value->value->GetObject()) {
if (str_compare_xx(argv[i], m.name)) {
rb_ary_push(ret, eval(m.value, value->root_document));
found = true;
break;
}
}
if (!found) {
if (rb_block_given_p()) {
rb_ary_push(ret, rb_yield(argv[i]));
} else {
rb_ary_free(ret);
rb_raise(rb_eKeyError, "key not found: \"%s\"", StringValueCStr(argv[i]));
return Qnil;
}
}
}
return ret;
}
|
#flatten(level = 1) ⇒ ::Array<Object>
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
|
# File 'ext/usamin/usamin.cpp', line 944
static VALUE w_hash_flatten(const int argc, const VALUE *argv, const VALUE self) {
rb_check_arity(argc, 0, 1);
UsaminValue *value = get_value(self);
check_object(value);
int level = 1;
if (argc == 1 && !NIL_P(argv[0])) {
level = NUM2INT(argv[0]);
if (level < 0)
level = -1;
}
if (level == 0) {
VALUE ret = rb_ary_new2(value->value->MemberCount());
for (auto &m : value->value->GetObject())
rb_ary_push(ret, rb_ary_new3(2, eval_str(m.name), eval(m.value, value->root_document)));
return ret;
}
VALUE ret = rb_ary_new2(value->value->MemberCount() * 2);
if (level == 1) {
for (auto &m : value->value->GetObject()) {
rb_ary_push(ret, eval_str(m.name));
rb_ary_push(ret, eval(m.value, value->root_document));
}
} else {
for (auto &m : value->value->GetObject()) {
rb_ary_push(ret, eval_str(m.name));
if (m.value.IsArray())
flatten_array(m.value, ret, level > 0 ? level - 2 : level, value->root_document);
else
rb_ary_push(ret, eval(m.value, value->root_document));
}
}
return ret;
}
|
#has_key?(key) ⇒ Boolean
Note:
This method has linear time complexity.
984
985
986
987
988
989
990
991
|
# File 'ext/usamin/usamin.cpp', line 984
static VALUE w_hash_haskey(const VALUE self, const VALUE key) {
UsaminValue *value = get_value(self);
check_object(value);
for (auto &m : value->value->GetObject())
if (str_compare_xx(key, m.name))
return Qtrue;
return Qfalse;
}
|
#has_value?(val) ⇒ Boolean
993
994
995
996
997
998
999
1000
|
# File 'ext/usamin/usamin.cpp', line 993
static VALUE w_hash_hasvalue(const VALUE self, const VALUE val) {
UsaminValue *value = get_value(self);
check_object(value);
for (auto &m : value->value->GetObject())
if (rb_equal(val, eval_r(m.value)))
return Qtrue;
return Qfalse;
}
|
#include?(key) ⇒ Boolean
Note:
This method has linear time complexity.
984
985
986
987
988
989
990
991
|
# File 'ext/usamin/usamin.cpp', line 984
static VALUE w_hash_haskey(const VALUE self, const VALUE key) {
UsaminValue *value = get_value(self);
check_object(value);
for (auto &m : value->value->GetObject())
if (str_compare_xx(key, m.name))
return Qtrue;
return Qfalse;
}
|
#index(val) ⇒ String | nil
1005
1006
1007
1008
1009
1010
1011
1012
|
# File 'ext/usamin/usamin.cpp', line 1005
static VALUE w_hash_key(const VALUE self, const VALUE val) {
UsaminValue *value = get_value(self);
check_object(value);
for (auto &m : value->value->GetObject())
if (rb_equal(val, eval_r(m.value)))
return eval_str(m.name);
return Qnil;
}
|
#inspect ⇒ String
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
|
# File 'ext/usamin/usamin.cpp', line 1017
static VALUE w_hash_inspect(const VALUE self) {
UsaminValue *value = get_value(self);
check_object(value);
VALUE ret = rb_str_new2("{");
bool first = true;
for (auto &m : value->value->GetObject()) {
if (!first)
ret = rb_str_cat2(ret, ", ");
ret = rb_str_append(ret, rb_inspect(eval_str(m.name)));
ret = rb_str_cat2(ret, "=>");
switch (m.value.GetType()) {
case rapidjson::kObjectType:
ret = rb_str_cat2(ret, "{...}");
break;
case rapidjson::kArrayType:
ret = rb_str_cat2(ret, "[...]");
break;
default:
ret = rb_str_append(ret, rb_inspect(eval(m.value, value->root_document)));
break;
}
first = false;
}
ret = rb_str_cat2(ret, "}");
return ret;
}
|
#key(val) ⇒ String | nil
1005
1006
1007
1008
1009
1010
1011
1012
|
# File 'ext/usamin/usamin.cpp', line 1005
static VALUE w_hash_key(const VALUE self, const VALUE val) {
UsaminValue *value = get_value(self);
check_object(value);
for (auto &m : value->value->GetObject())
if (rb_equal(val, eval_r(m.value)))
return eval_str(m.name);
return Qnil;
}
|
#key?(key) ⇒ Boolean
Note:
This method has linear time complexity.
984
985
986
987
988
989
990
991
|
# File 'ext/usamin/usamin.cpp', line 984
static VALUE w_hash_haskey(const VALUE self, const VALUE key) {
UsaminValue *value = get_value(self);
check_object(value);
for (auto &m : value->value->GetObject())
if (str_compare_xx(key, m.name))
return Qtrue;
return Qfalse;
}
|
#keys ⇒ ::Array<String>
1047
1048
1049
1050
1051
1052
1053
1054
|
# File 'ext/usamin/usamin.cpp', line 1047
static VALUE w_hash_keys(const VALUE self) {
UsaminValue *value = get_value(self);
check_object(value);
VALUE ret = rb_ary_new2(value->value->MemberCount());
for (auto &m : value->value->GetObject())
rb_ary_push(ret, eval_str(m.name));
return ret;
}
|
#length ⇒ Integer
1059
1060
1061
1062
1063
|
# File 'ext/usamin/usamin.cpp', line 1059
static VALUE w_hash_length(const VALUE self) {
UsaminValue *value = get_value(self);
check_object(value);
return UINT2NUM(value->value->MemberCount());
}
|
#marshal_dump ⇒ String
740
741
742
743
744
745
746
747
|
# File 'ext/usamin/usamin.cpp', line 740
static VALUE w_value_marshal_dump(const VALUE self) {
UsaminValue *value = get_value(self);
check_value(value);
rapidjson::StringBuffer buf;
rapidjson::Writer<rapidjson::StringBuffer> writer(buf);
write_value(writer, *value->value);
return rb_str_new(buf.GetString(), buf.GetSize());
}
|
#marshal_load(source) ⇒ Object
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
|
# File 'ext/usamin/usamin.cpp', line 752
static VALUE w_value_marshal_load(const VALUE self, const VALUE source) {
Check_Type(source, T_STRING);
RubynizedDocument *doc = new RubynizedDocument();
rapidjson::ParseResult result = doc->Parse<rapidjson::kParseFullPrecisionFlag | rapidjson::kParseNanAndInfFlag>(RSTRING_PTR(source), RSTRING_LEN(source));
if (!result) {
delete doc;
rb_raise(rb_eParserError, "%s Offset: %lu", GetParseError_En(result.Code()), result.Offset());
}
if (doc->IsObject() || doc->IsArray()) {
UsaminValue *value = new UsaminValue(doc, true);
set_value(self, value);
value->root_document = self;
} else {
auto type = doc->GetType();
delete doc;
rb_raise(rb_eUsaminError, "Invalid Value Type for marshal_load: %d", type);
}
return Qnil;
}
|
#member?(key) ⇒ Boolean
Note:
This method has linear time complexity.
984
985
986
987
988
989
990
991
|
# File 'ext/usamin/usamin.cpp', line 984
static VALUE w_hash_haskey(const VALUE self, const VALUE key) {
UsaminValue *value = get_value(self);
check_object(value);
for (auto &m : value->value->GetObject())
if (str_compare_xx(key, m.name))
return Qtrue;
return Qfalse;
}
|
#merge(*args) ⇒ ::Hash
1164
1165
1166
1167
|
# File 'ext/usamin/usamin.cpp', line 1164
static VALUE w_hash_merge(const int argc, const VALUE *argv, const VALUE self) {
static ID s = rb_intern("merge");
return rb_funcall2(w_hash_eval(self), s, argc, argv);
}
|
#rassoc(val) ⇒ ::Array | nil
1068
1069
1070
1071
1072
1073
1074
1075
|
# File 'ext/usamin/usamin.cpp', line 1068
static VALUE w_hash_rassoc(const VALUE self, const VALUE val) {
UsaminValue *value = get_value(self);
check_object(value);
for (auto &m : value->value->GetObject())
if (rb_funcall(val, rb_intern("=="), 1, eval_r(m.value)))
return rb_assoc_new(eval_str(m.name), eval(m.value, value->root_document));
return Qnil;
}
|
#reject {|key, value| ... } ⇒ Enumerator | ::Hash
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
|
# File 'ext/usamin/usamin.cpp', line 1111
static VALUE w_hash_reject(const VALUE self) {
UsaminValue *value = get_value(self);
check_object(value);
RETURN_SIZED_ENUMERATOR(self, 0, nullptr, hash_enum_size);
VALUE hash = rb_hash_new();
if (rb_proc_arity(rb_block_proc()) > 1) {
for (auto &m : value->value->GetObject()) {
VALUE args[] = { eval_str(m.name), eval(m.value, value->root_document) };
if (!RTEST(rb_yield_values2(2, args)))
rb_hash_aset(hash, args[0], args[1]);
}
} else {
for (auto &m : value->value->GetObject()) {
VALUE key = eval_str(m.name);
VALUE val = eval(m.value, value->root_document);
if (!RTEST(rb_yield(rb_assoc_new(key, val))))
rb_hash_aset(hash, key, val);
}
}
return hash;
}
|
#select {|key, value| ... } ⇒ Enumerator | ::Hash
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
|
# File 'ext/usamin/usamin.cpp', line 1083
static VALUE w_hash_select(const VALUE self) {
UsaminValue *value = get_value(self);
check_object(value);
RETURN_SIZED_ENUMERATOR(self, 0, nullptr, hash_enum_size);
VALUE hash = rb_hash_new();
if (rb_proc_arity(rb_block_proc()) > 1) {
for (auto &m : value->value->GetObject()) {
VALUE args[] = { eval_str(m.name), eval(m.value, value->root_document) };
if (RTEST(rb_yield_values2(2, args)))
rb_hash_aset(hash, args[0], args[1]);
}
} else {
for (auto &m : value->value->GetObject()) {
VALUE key = eval_str(m.name);
VALUE val = eval(m.value, value->root_document);
if (RTEST(rb_yield(rb_assoc_new(key, val))))
rb_hash_aset(hash, key, val);
}
}
return hash;
}
|
#size ⇒ Integer
1059
1060
1061
1062
1063
|
# File 'ext/usamin/usamin.cpp', line 1059
static VALUE w_hash_length(const VALUE self) {
UsaminValue *value = get_value(self);
check_object(value);
return UINT2NUM(value->value->MemberCount());
}
|
#slice(*args) {|key, value| ... } ⇒ Enumerator | ::Hash
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
|
# File 'ext/usamin/usamin.cpp', line 1139
static VALUE w_hash_slice(const int argc, const VALUE *argv, const VALUE self) {
UsaminValue *value = get_value(self);
check_object(value);
VALUE hash = rb_hash_new();
for (int i = 0; i < argc; i++)
for (auto &m : value->value->GetObject())
if (str_compare_xx(argv[i], m.name))
rb_hash_aset(hash, eval_str(m.name), eval(m.value, value->root_document));
return hash;
}
|
#to_hash ⇒ ::Hash
1155
1156
1157
1158
1159
|
# File 'ext/usamin/usamin.cpp', line 1155
static VALUE w_hash_eval(const VALUE self) {
UsaminValue *value = get_value(self);
check_object(value);
return eval_object(*(value->value), value->root_document);
}
|
#to_s ⇒ String
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
|
# File 'ext/usamin/usamin.cpp', line 1017
static VALUE w_hash_inspect(const VALUE self) {
UsaminValue *value = get_value(self);
check_object(value);
VALUE ret = rb_str_new2("{");
bool first = true;
for (auto &m : value->value->GetObject()) {
if (!first)
ret = rb_str_cat2(ret, ", ");
ret = rb_str_append(ret, rb_inspect(eval_str(m.name)));
ret = rb_str_cat2(ret, "=>");
switch (m.value.GetType()) {
case rapidjson::kObjectType:
ret = rb_str_cat2(ret, "{...}");
break;
case rapidjson::kArrayType:
ret = rb_str_cat2(ret, "[...]");
break;
default:
ret = rb_str_append(ret, rb_inspect(eval(m.value, value->root_document)));
break;
}
first = false;
}
ret = rb_str_cat2(ret, "}");
return ret;
}
|
1186
1187
1188
1189
1190
1191
1192
1193
1194
|
# File 'ext/usamin/usamin.cpp', line 1186
static VALUE w_hash_transform_keys(const VALUE self) {
UsaminValue *value = get_value(self);
check_object(value);
RETURN_SIZED_ENUMERATOR(self, 0, nullptr, hash_enum_size);
VALUE hash = rb_hash_new();
for (auto &m : value->value->GetObject())
rb_hash_aset(hash, rb_yield(eval_str(m.name)), eval(m.value, value->root_document));
return hash;
}
|
1201
1202
1203
1204
1205
1206
1207
1208
1209
|
# File 'ext/usamin/usamin.cpp', line 1201
static VALUE w_hash_transform_values(const VALUE self) {
UsaminValue *value = get_value(self);
check_object(value);
RETURN_SIZED_ENUMERATOR(self, 0, nullptr, hash_enum_size);
VALUE hash = rb_hash_new();
for (auto &m : value->value->GetObject())
rb_hash_aset(hash, eval_str(m.name), rb_yield(eval(m.value, value->root_document)));
return hash;
}
|
#value?(val) ⇒ Boolean
993
994
995
996
997
998
999
1000
|
# File 'ext/usamin/usamin.cpp', line 993
static VALUE w_hash_hasvalue(const VALUE self, const VALUE val) {
UsaminValue *value = get_value(self);
check_object(value);
for (auto &m : value->value->GetObject())
if (rb_equal(val, eval_r(m.value)))
return Qtrue;
return Qfalse;
}
|
#values ⇒ ::Array<Object>
1172
1173
1174
1175
1176
1177
1178
1179
|
# File 'ext/usamin/usamin.cpp', line 1172
static VALUE w_hash_values(const VALUE self) {
UsaminValue *value = get_value(self);
check_object(value);
VALUE ret = rb_ary_new2(value->value->MemberCount());
for (auto &m : value->value->GetObject())
rb_ary_push(ret, eval(m.value, value->root_document));
return ret;
}
|
#values_at(*args) ⇒ ::Array<Object>
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
|
# File 'ext/usamin/usamin.cpp', line 1215
static VALUE w_hash_values_at(const int argc, const VALUE *argv, const VALUE self) {
UsaminValue *value = get_value(self);
check_object(value);
VALUE ret = rb_ary_new2(argc);
for (int i = 0; i < argc; i++) {
VALUE data = Qnil;
for (auto &m : value->value->GetObject()) {
if (str_compare_xx(argv[i], m.name)) {
data = eval(m.value, value->root_document);
break;
}
}
rb_ary_push(ret, data);
}
return ret;
}
|