immutable_list 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +83 -0
- data/Rakefile +6 -0
- data/ext/immutable_list/extconf.rb +2 -0
- data/ext/immutable_list/immutable_list.c +228 -0
- data/immutable_list.gemspec +24 -0
- data/lib/immutable_list.rb +6 -0
- data/lib/immutable_list/version.rb +3 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 48ff0eda84f47682b253b849ba400f9b3df3e9d4
|
4
|
+
data.tar.gz: e28fbd7114d89f519415421cb91997b534119280
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 33f3d20d8e7ad1245c788b010029f0727f352bbb15b2cd443ad52b44ec8c48ccaec35146bea8ad416d50f37b2472b9273c05b6b6103e5869860a7504f8b09e02
|
7
|
+
data.tar.gz: 0e433153175673d94d68d129908f5b27257c776ff9a2b3253f5410fabd86761f0f0c852273b10aa535de7d5caa7d0743db40e29984f142fd8dcce287a5bc59d0
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 gam0022
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# ImmutableList
|
2
|
+
|
3
|
+
Immutable Linked List of Ruby implemented in C-Extensions
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'immutable_list'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install immutable_list
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### Basic
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'immutable_list'
|
25
|
+
|
26
|
+
p ImmutableList.new # => ()
|
27
|
+
|
28
|
+
p l1 = ImmutableList.new.cons(1).cons(2).cons(3) # => (3, 2, 1)
|
29
|
+
p l1.head # => 3
|
30
|
+
p l1.tail # => (2, 1)
|
31
|
+
|
32
|
+
p l2 = ImmutableList[1, 2, 3, "a", "b"] #=> (1, 2, 3, "a", "b")
|
33
|
+
|
34
|
+
p l1.rev_append(l2) #=> (1, 2, 3, 1, 2, 3, "a", "b")
|
35
|
+
p l1.rev #=> (1, 2, 3)
|
36
|
+
p l3 = l1.append(l2) #=> (3, 2, 1, 1, 2, 3, "a", "b")
|
37
|
+
|
38
|
+
p l3.length # => 8
|
39
|
+
p ImmutableList[].length #=> 0
|
40
|
+
|
41
|
+
p l3.nth(0) #=> 3
|
42
|
+
p l3[7] #=> "b"
|
43
|
+
p l3[100] #=> nil
|
44
|
+
```
|
45
|
+
|
46
|
+
### QuickSort
|
47
|
+
|
48
|
+
A Example of QuickSort.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
require 'immutable_list'
|
52
|
+
|
53
|
+
def divide(a, l, lt, ge)
|
54
|
+
if l.empty?
|
55
|
+
[lt, ge]
|
56
|
+
elsif l.head < a
|
57
|
+
divide(a, l.tail, lt.cons(l.head), ge)
|
58
|
+
else
|
59
|
+
divide(a, l.tail, lt, ge.cons(l.head))
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def qsort(l)
|
64
|
+
if l.empty?
|
65
|
+
ImmutableList.new
|
66
|
+
else
|
67
|
+
lt, ge = divide(l.head, l.tail, ImmutableList.new, ImmutableList.new)
|
68
|
+
qsort(lt) + qsort(ge).cons(l.head)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
l = ImmutableList[3, 5, 8, 1, 4, 7, 10, -3, 2, 100, 43, 10, 50]
|
73
|
+
p qsort(l) #=> (-3, 1, 2, 3, 4, 5, 7, 8, 10, 10, 43, 50, 100)
|
74
|
+
```
|
75
|
+
|
76
|
+
|
77
|
+
## Contributing
|
78
|
+
|
79
|
+
1. Fork it
|
80
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
81
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
82
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
83
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,228 @@
|
|
1
|
+
#include <stdio.h>
|
2
|
+
#include <ruby.h>
|
3
|
+
|
4
|
+
#define true 1
|
5
|
+
#define false 0
|
6
|
+
|
7
|
+
VALUE cImmutableList;
|
8
|
+
|
9
|
+
struct immutable_list {
|
10
|
+
VALUE value;
|
11
|
+
VALUE next;
|
12
|
+
};
|
13
|
+
|
14
|
+
static void
|
15
|
+
immutable_list_mark(struct immutable_list *ptr)
|
16
|
+
{
|
17
|
+
rb_gc_mark(ptr->value);
|
18
|
+
rb_gc_mark(ptr->next);
|
19
|
+
}
|
20
|
+
|
21
|
+
static VALUE
|
22
|
+
immutable_list_alloc(VALUE klass)
|
23
|
+
{
|
24
|
+
struct immutable_list *ptr = ALLOC(struct immutable_list);
|
25
|
+
return Data_Wrap_Struct(klass, immutable_list_mark, -1, ptr);
|
26
|
+
}
|
27
|
+
|
28
|
+
static VALUE
|
29
|
+
immutable_list_initialize(VALUE self)
|
30
|
+
{
|
31
|
+
struct immutable_list *ptr;
|
32
|
+
|
33
|
+
Data_Get_Struct(self, struct immutable_list, ptr);
|
34
|
+
ptr->value = Qnil;
|
35
|
+
ptr->next = Qnil;
|
36
|
+
return self;
|
37
|
+
}
|
38
|
+
|
39
|
+
static VALUE
|
40
|
+
immutable_list_cons(VALUE self, VALUE a)
|
41
|
+
{
|
42
|
+
struct immutable_list *ptr, *new_ptr;
|
43
|
+
VALUE result;
|
44
|
+
|
45
|
+
Data_Get_Struct(self, struct immutable_list, ptr);
|
46
|
+
result = immutable_list_alloc(cImmutableList);
|
47
|
+
Data_Get_Struct(result, struct immutable_list, new_ptr);
|
48
|
+
|
49
|
+
new_ptr->value = a;
|
50
|
+
new_ptr->next = self;
|
51
|
+
return result;
|
52
|
+
}
|
53
|
+
|
54
|
+
static VALUE
|
55
|
+
immutable_list_s_create_core(VALUE result, int argc, VALUE *argv, int i)
|
56
|
+
{
|
57
|
+
if (i < 0) {
|
58
|
+
return result;
|
59
|
+
} else {
|
60
|
+
return immutable_list_s_create_core( immutable_list_cons(result, argv[i]), argc, argv, i-1);
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
static VALUE
|
65
|
+
immutable_list_s_create(int argc, VALUE *argv, VALUE klass)
|
66
|
+
{
|
67
|
+
VALUE result = immutable_list_initialize(immutable_list_alloc(klass));
|
68
|
+
return immutable_list_s_create_core(result, argc, argv, argc - 1);
|
69
|
+
}
|
70
|
+
|
71
|
+
static VALUE
|
72
|
+
immutable_list_head(VALUE self)
|
73
|
+
{
|
74
|
+
struct immutable_list *ptr;
|
75
|
+
Data_Get_Struct(self, struct immutable_list, ptr);
|
76
|
+
return ptr->value;
|
77
|
+
}
|
78
|
+
|
79
|
+
static VALUE
|
80
|
+
immutable_list_tail(VALUE self)
|
81
|
+
{
|
82
|
+
struct immutable_list *ptr;
|
83
|
+
Data_Get_Struct(self, struct immutable_list, ptr);
|
84
|
+
return ptr->next;
|
85
|
+
}
|
86
|
+
|
87
|
+
static VALUE
|
88
|
+
immutable_list_is_empty(VALUE self)
|
89
|
+
{
|
90
|
+
struct immutable_list *ptr;
|
91
|
+
Data_Get_Struct(self, struct immutable_list, ptr);
|
92
|
+
return ptr->next == Qnil ? Qtrue : Qfalse;
|
93
|
+
}
|
94
|
+
|
95
|
+
static VALUE
|
96
|
+
immutable_list_to_a(VALUE self)
|
97
|
+
{
|
98
|
+
struct immutable_list *ptr;
|
99
|
+
VALUE ary;
|
100
|
+
|
101
|
+
ary = rb_ary_new();
|
102
|
+
Data_Get_Struct(self, struct immutable_list, ptr);
|
103
|
+
while (ptr->next != Qnil) {
|
104
|
+
rb_ary_push(ary, ptr->value);
|
105
|
+
Data_Get_Struct(ptr->next, struct immutable_list, ptr);
|
106
|
+
}
|
107
|
+
return ary;
|
108
|
+
}
|
109
|
+
|
110
|
+
static VALUE
|
111
|
+
immutable_list_inspect(VALUE self)
|
112
|
+
{
|
113
|
+
VALUE str, str2;
|
114
|
+
struct immutable_list *ptr;
|
115
|
+
|
116
|
+
Data_Get_Struct(self, struct immutable_list, ptr);
|
117
|
+
str = rb_str_buf_new2("(");
|
118
|
+
|
119
|
+
if (ptr->next != Qnil) {
|
120
|
+
while (true) {
|
121
|
+
str2 = rb_inspect(ptr->value);
|
122
|
+
rb_str_buf_append(str, str2);
|
123
|
+
Data_Get_Struct(ptr->next, struct immutable_list, ptr);
|
124
|
+
if (ptr->next != Qnil) {
|
125
|
+
rb_str_buf_cat_ascii(str, ", ");
|
126
|
+
} else {
|
127
|
+
break;
|
128
|
+
}
|
129
|
+
}
|
130
|
+
}
|
131
|
+
|
132
|
+
rb_str_buf_cat2(str, ")");
|
133
|
+
OBJ_INFECT(str, self);
|
134
|
+
|
135
|
+
return str;
|
136
|
+
}
|
137
|
+
|
138
|
+
static VALUE
|
139
|
+
immutable_list_rev_append(VALUE l1, VALUE l2)
|
140
|
+
{
|
141
|
+
struct immutable_list *ptr;
|
142
|
+
|
143
|
+
Data_Get_Struct(l1, struct immutable_list, ptr);
|
144
|
+
if (ptr->next == Qnil) {
|
145
|
+
return l2;
|
146
|
+
} else {
|
147
|
+
return immutable_list_rev_append(ptr->next, immutable_list_cons(l2, ptr->value));
|
148
|
+
}
|
149
|
+
}
|
150
|
+
|
151
|
+
static VALUE
|
152
|
+
immutable_list_rev(VALUE self)
|
153
|
+
{
|
154
|
+
return immutable_list_rev_append(self,
|
155
|
+
immutable_list_initialize(immutable_list_alloc(cImmutableList)));
|
156
|
+
}
|
157
|
+
|
158
|
+
static VALUE
|
159
|
+
immutable_list_append(VALUE l1, VALUE l2)
|
160
|
+
{
|
161
|
+
struct immutable_list *ptr;
|
162
|
+
|
163
|
+
Data_Get_Struct(l1, struct immutable_list, ptr);
|
164
|
+
if (ptr->next == Qnil) {
|
165
|
+
return l2;
|
166
|
+
} else {
|
167
|
+
return immutable_list_cons( immutable_list_append(ptr->next, l2), ptr->value);
|
168
|
+
}
|
169
|
+
}
|
170
|
+
|
171
|
+
static int
|
172
|
+
immutable_list_length_core(VALUE self)
|
173
|
+
{
|
174
|
+
struct immutable_list *ptr;
|
175
|
+
|
176
|
+
Data_Get_Struct(self, struct immutable_list, ptr);
|
177
|
+
if (ptr->next == Qnil) {
|
178
|
+
return 0;
|
179
|
+
} else {
|
180
|
+
return 1 + immutable_list_length_core(ptr->next);
|
181
|
+
}
|
182
|
+
}
|
183
|
+
|
184
|
+
static VALUE
|
185
|
+
immutable_list_length(VALUE self)
|
186
|
+
{
|
187
|
+
return INT2FIX(immutable_list_length_core(self));
|
188
|
+
}
|
189
|
+
|
190
|
+
static VALUE
|
191
|
+
immutable_list_nth(VALUE self, VALUE index)
|
192
|
+
{
|
193
|
+
struct immutable_list *ptr;
|
194
|
+
int i, n;
|
195
|
+
|
196
|
+
n = FIX2INT(index);
|
197
|
+
Data_Get_Struct(self, struct immutable_list, ptr);
|
198
|
+
for (i = 0; ptr->next != Qnil; ++i) {
|
199
|
+
if (i == n) {
|
200
|
+
return ptr->value;
|
201
|
+
}
|
202
|
+
Data_Get_Struct(ptr->next, struct immutable_list, ptr);
|
203
|
+
}
|
204
|
+
return Qnil;
|
205
|
+
}
|
206
|
+
|
207
|
+
void Init_immutable_list(void)
|
208
|
+
{
|
209
|
+
cImmutableList = rb_define_class("ImmutableList", rb_cObject);
|
210
|
+
rb_define_alloc_func(cImmutableList, immutable_list_alloc);
|
211
|
+
rb_define_singleton_method(cImmutableList, "[]", immutable_list_s_create, -1);
|
212
|
+
|
213
|
+
rb_define_private_method(cImmutableList, "initialize", immutable_list_initialize, 0);
|
214
|
+
rb_define_method(cImmutableList, "cons", immutable_list_cons, 1);
|
215
|
+
rb_define_method(cImmutableList, "head", immutable_list_head, 0);
|
216
|
+
rb_define_method(cImmutableList, "tail", immutable_list_tail, 0);
|
217
|
+
rb_define_method(cImmutableList, "empty?", immutable_list_is_empty, 0);
|
218
|
+
rb_define_method(cImmutableList, "to_a", immutable_list_to_a, 0);
|
219
|
+
rb_define_method(cImmutableList, "inspect", immutable_list_inspect, 0);
|
220
|
+
rb_define_alias(cImmutableList, "to_s", "inspect");
|
221
|
+
rb_define_method(cImmutableList, "rev_append", immutable_list_rev_append, 1);
|
222
|
+
rb_define_method(cImmutableList, "rev", immutable_list_rev, 0);
|
223
|
+
rb_define_method(cImmutableList, "append", immutable_list_append, 1);
|
224
|
+
rb_define_alias(cImmutableList, "+", "append");
|
225
|
+
rb_define_method(cImmutableList, "length", immutable_list_length, 0);
|
226
|
+
rb_define_method(cImmutableList, "nth", immutable_list_nth, 1);
|
227
|
+
rb_define_alias(cImmutableList, "[]", "nth");
|
228
|
+
}
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'immutable_list/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "immutable_list"
|
8
|
+
spec.version = ImmutableList::VERSION
|
9
|
+
spec.authors = ["gam0022"]
|
10
|
+
spec.email = ["gam0022@gmail.com"]
|
11
|
+
spec.description = %q{Immutable Linked List implemented in C-Extensions}
|
12
|
+
spec.summary = %q{Immutable Linked List implemented in C-Extensions}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.extensions = %w[ext/immutable_list/extconf.rb]
|
16
|
+
|
17
|
+
spec.files = `git ls-files`.split($/)
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: immutable_list
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.5
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- gam0022
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Immutable Linked List implemented in C-Extensions
|
42
|
+
email:
|
43
|
+
- gam0022@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions:
|
46
|
+
- ext/immutable_list/extconf.rb
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- .gitignore
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- ext/immutable_list/extconf.rb
|
55
|
+
- ext/immutable_list/immutable_list.c
|
56
|
+
- immutable_list.gemspec
|
57
|
+
- lib/immutable_list.rb
|
58
|
+
- lib/immutable_list/version.rb
|
59
|
+
homepage: ''
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.1.5
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Immutable Linked List implemented in C-Extensions
|
83
|
+
test_files: []
|