Class: ImmutableList

Inherits:
Object
  • Object
show all
Defined in:
lib/immutable_list.rb,
lib/immutable_list/version.rb,
ext/immutable_list/immutable_list.c

Constant Summary collapse

VERSION =
"0.0.5"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](*args) ⇒ Object



64
65
66
67
68
69
# File 'ext/immutable_list/immutable_list.c', line 64

static VALUE
immutable_list_s_create(int argc, VALUE *argv, VALUE klass)
{
  VALUE result = immutable_list_initialize(immutable_list_alloc(klass));
  return immutable_list_s_create_core(result, argc, argv, argc - 1);
}

Instance Method Details

#append(l2) ⇒ Object Also known as: +



158
159
160
161
162
163
164
165
166
167
168
169
# File 'ext/immutable_list/immutable_list.c', line 158

static VALUE
immutable_list_append(VALUE l1, VALUE l2)
{
  struct immutable_list *ptr;

  Data_Get_Struct(l1, struct immutable_list, ptr);
  if (ptr->next == Qnil) {
    return l2;
  } else {
    return immutable_list_cons( immutable_list_append(ptr->next, l2), ptr->value);
  }
}

#cons(a) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'ext/immutable_list/immutable_list.c', line 39

static VALUE
immutable_list_cons(VALUE self, VALUE a)
{
  struct immutable_list *ptr, *new_ptr;
  VALUE result;

  Data_Get_Struct(self, struct immutable_list, ptr);
  result = immutable_list_alloc(cImmutableList);
  Data_Get_Struct(result, struct immutable_list, new_ptr);

  new_ptr->value = a;
  new_ptr->next = self;
  return result;
}

#empty?Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
# File 'ext/immutable_list/immutable_list.c', line 87

static VALUE
immutable_list_is_empty(VALUE self)
{
  struct immutable_list *ptr;
  Data_Get_Struct(self, struct immutable_list, ptr);
  return ptr->next == Qnil ? Qtrue : Qfalse;
}

#headObject



71
72
73
74
75
76
77
# File 'ext/immutable_list/immutable_list.c', line 71

static VALUE
immutable_list_head(VALUE self)
{
  struct immutable_list *ptr;
  Data_Get_Struct(self, struct immutable_list, ptr);
  return ptr->value;
}

#inspectObject Also known as: to_s



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'ext/immutable_list/immutable_list.c', line 110

static VALUE
immutable_list_inspect(VALUE self)
{
  VALUE str, str2;
  struct immutable_list *ptr;

  Data_Get_Struct(self, struct immutable_list, ptr);
  str = rb_str_buf_new2("(");

  if (ptr->next != Qnil) {
    while (true) {
      str2 = rb_inspect(ptr->value);
      rb_str_buf_append(str, str2);
      Data_Get_Struct(ptr->next, struct immutable_list, ptr);
      if (ptr->next != Qnil) {
        rb_str_buf_cat_ascii(str, ", ");
      } else {
        break;
      }
    }
  }

  rb_str_buf_cat2(str, ")");
  OBJ_INFECT(str, self);

  return str;
}

#lengthObject



184
185
186
187
188
# File 'ext/immutable_list/immutable_list.c', line 184

static VALUE
immutable_list_length(VALUE self)
{
  return INT2FIX(immutable_list_length_core(self));
}

#nth(index) ⇒ Object Also known as: []



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'ext/immutable_list/immutable_list.c', line 190

static VALUE
immutable_list_nth(VALUE self, VALUE index)
{
  struct immutable_list *ptr;
  int i, n;
  
  n = FIX2INT(index);
  Data_Get_Struct(self, struct immutable_list, ptr);
  for (i = 0; ptr->next != Qnil; ++i) {
    if (i == n) {
      return ptr->value;
    }
    Data_Get_Struct(ptr->next, struct immutable_list, ptr);
  }
  return Qnil;
}

#revObject



151
152
153
154
155
156
# File 'ext/immutable_list/immutable_list.c', line 151

static VALUE
immutable_list_rev(VALUE self)
{
  return immutable_list_rev_append(self,
      immutable_list_initialize(immutable_list_alloc(cImmutableList)));
}

#rev_append(l2) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
# File 'ext/immutable_list/immutable_list.c', line 138

static VALUE
immutable_list_rev_append(VALUE l1, VALUE l2)
{
  struct immutable_list *ptr;

  Data_Get_Struct(l1, struct immutable_list, ptr);
  if (ptr->next == Qnil) {
    return l2;
  } else {
    return immutable_list_rev_append(ptr->next, immutable_list_cons(l2, ptr->value));
  }
}

#tailObject



79
80
81
82
83
84
85
# File 'ext/immutable_list/immutable_list.c', line 79

static VALUE
immutable_list_tail(VALUE self)
{
  struct immutable_list *ptr;
  Data_Get_Struct(self, struct immutable_list, ptr);
  return ptr->next;
}

#to_aObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'ext/immutable_list/immutable_list.c', line 95

static VALUE
immutable_list_to_a(VALUE self)
{
  struct immutable_list *ptr;
  VALUE ary;
  
  ary = rb_ary_new();
  Data_Get_Struct(self, struct immutable_list, ptr);
  while (ptr->next != Qnil) {
    rb_ary_push(ary, ptr->value);
    Data_Get_Struct(ptr->next, struct immutable_list, ptr);
  }
  return ary;
}