Debian Codegraph @

lib/mrb/mrb_vector.c

raw symbols SCIP
1 /*
2 Copyright(C) 2019-2020 Sutou Kouhei <[email protected]>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "../grn_ctx_impl.h"
20
21 #ifdef GRN_WITH_MRUBY
22 #include <mruby.h>
23 #include <mruby/array.h>
24 #include <mruby/class.h>
25 #include <mruby/data.h>
26
27 #include "../grn_db.h"
28 #include "mrb_bulk.h"
29 #include "mrb_object.h"
30
31 static struct mrb_data_type mrb_grn_vector_type = {
32 "Groonga::Vector",
33 NULL
34 };
35
36 static mrb_value
37 mrb_grn_vector_initialize(mrb_state *mrb, mrb_value self)
38 {
39 mrb_value mrb_vector_ptr;
40
41 mrb_get_args(mrb, "o", &mrb_vector_ptr);
42 DATA_TYPE(self) = &mrb_grn_vector_type;
43 DATA_PTR(self) = mrb_cptr(mrb_vector_ptr);
44 return self;
45 }
46
47 mrb_value
48 grn_mrb_value_from_vector(mrb_state *mrb, grn_obj *vector)
49 {
50 if (!vector) {
51 return mrb_nil_value();
52 }
53
54 grn_ctx *ctx = (grn_ctx *)mrb->ud;
55 unsigned int n = grn_vector_size(ctx, vector);
56 mrb_value mrb_vector = mrb_ary_new_capa(mrb, n);
57 grn_obj element;
58 GRN_TEXT_INIT(&element, GRN_OBJ_DO_SHALLOW_COPY);
59 for (unsigned int i = 0; i < n; i++) {
60 const char *content;
61 unsigned int content_length;
62 content_length = grn_vector_get_element(ctx,
63 vector,
64 i,
65 &content,
66 NULL,
67 &(element.header.domain));
68 GRN_TEXT_SET(ctx, &element, content, content_length);
69 mrb_ary_push(mrb, mrb_vector, grn_mrb_value_from_bulk(mrb, &element));
70 element.header.domain = GRN_DB_TEXT;
71 }
72 GRN_OBJ_FIN(ctx, &element);
73
74 return mrb_vector;
75 }
76
77 static mrb_value
78 mrb_grn_vector_get_value(mrb_state *mrb, mrb_value self)
79 {
80 return grn_mrb_value_from_vector(mrb, DATA_PTR(self));
81 }
82
83 static mrb_value
84 mrb_grn_vector_equal(mrb_state *mrb, mrb_value self)
85 {
86 mrb_value mrb_other;
87
88 mrb_get_args(mrb, "o", &mrb_other);
89
90 if (!mrb_obj_is_kind_of(mrb, mrb_other, mrb_class(mrb, self))) {
91 return mrb_false_value();
92 }
93
94 return mrb_bool_value(DATA_PTR(self) == DATA_PTR(mrb_other));
95 }
96
97 void
98 grn_mrb_vector_init(grn_ctx *ctx)
99 {
100 grn_mrb_data *data = &(ctx->impl->mrb);
101 mrb_state *mrb = data->state;
102 struct RClass *module = data->module;
103 struct RClass *klass;
104
105 klass = mrb_define_class_under(mrb, module, "Vector", mrb->object_class);
106 MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA);
107
108 mrb_define_method(mrb, klass, "initialize",
109 mrb_grn_vector_initialize, MRB_ARGS_REQ(1));
110 mrb_define_method(mrb, klass, "value",
111 mrb_grn_vector_get_value, MRB_ARGS_NONE());
112 mrb_define_method(mrb, klass, "true?",
113 grn_mrb_object_is_true, MRB_ARGS_NONE());
114 mrb_define_method(mrb, klass, "==",
115 mrb_grn_vector_equal, MRB_ARGS_REQ(1));
116 mrb_define_method(mrb, klass, "inspect",
117 grn_mrb_object_inspect, MRB_ARGS_NONE());
118 mrb_define_method(mrb, klass, "close",
119 grn_mrb_object_close, MRB_ARGS_NONE());
120 mrb_define_method(mrb, klass, "closed?",
121 grn_mrb_object_is_closed, MRB_ARGS_NONE());
122 }
123 #endif