swiftiply 0.6.1.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CONTRIBUTORS +2 -0
- data/README.md +62 -0
- data/bin/{mongrel_rails → evented_mongrel_rails} +6 -14
- data/bin/swiftiplied_mongrel_rails +246 -0
- data/bin/swiftiply +136 -116
- data/bin/swiftiply_mongrel_rails +2 -2
- data/bin/swiftiplyctl +283 -0
- data/cleanup.sh +5 -0
- data/ext/deque/extconf.rb +162 -0
- data/ext/deque/swiftcore/rubymain.cpp +435 -0
- data/ext/fastfilereader/extconf.rb +2 -2
- data/ext/fastfilereader/mapper.cpp +2 -0
- data/ext/map/extconf.rb +161 -0
- data/ext/map/rubymain.cpp +500 -0
- data/ext/splaytree/extconf.rb +161 -0
- data/ext/splaytree/swiftcore/rubymain.cpp +580 -0
- data/ext/splaytree/swiftcore/splay_map.h +635 -0
- data/ext/splaytree/swiftcore/splay_set.h +575 -0
- data/ext/splaytree/swiftcore/splay_tree.h +1127 -0
- data/external/httpclient.rb +231 -0
- data/external/package.rb +13 -13
- data/setup.rb +18 -2
- data/src/swiftcore/Swiftiply.rb +417 -773
- data/src/swiftcore/Swiftiply/backend_protocol.rb +213 -0
- data/src/swiftcore/Swiftiply/cache_base.rb +49 -0
- data/src/swiftcore/Swiftiply/cache_base_mixin.rb +52 -0
- data/src/swiftcore/Swiftiply/cluster_managers/rest_based_cluster_manager.rb +9 -0
- data/src/swiftcore/Swiftiply/cluster_protocol.rb +70 -0
- data/src/swiftcore/Swiftiply/config.rb +370 -0
- data/src/swiftcore/Swiftiply/config/rest_updater.rb +26 -0
- data/src/swiftcore/Swiftiply/constants.rb +101 -0
- data/src/swiftcore/Swiftiply/content_cache_entry.rb +44 -0
- data/src/swiftcore/Swiftiply/content_response.rb +45 -0
- data/src/swiftcore/Swiftiply/control_protocol.rb +49 -0
- data/src/swiftcore/Swiftiply/dynamic_request_cache.rb +41 -0
- data/src/swiftcore/Swiftiply/etag_cache.rb +64 -0
- data/src/swiftcore/Swiftiply/file_cache.rb +46 -0
- data/src/swiftcore/Swiftiply/hash_cache_base.rb +22 -0
- data/src/swiftcore/Swiftiply/http_recognizer.rb +267 -0
- data/src/swiftcore/Swiftiply/loggers/Analogger.rb +21 -0
- data/src/swiftcore/Swiftiply/loggers/stderror.rb +13 -0
- data/src/swiftcore/Swiftiply/mocklog.rb +10 -0
- data/src/swiftcore/Swiftiply/proxy.rb +15 -0
- data/src/swiftcore/Swiftiply/proxy_backends/keepalive.rb +286 -0
- data/src/swiftcore/Swiftiply/proxy_backends/traditional.rb +286 -0
- data/src/swiftcore/Swiftiply/proxy_backends/traditional/redis_directory.rb +87 -0
- data/src/swiftcore/Swiftiply/proxy_backends/traditional/static_directory.rb +69 -0
- data/src/swiftcore/Swiftiply/proxy_bag.rb +716 -0
- data/src/swiftcore/Swiftiply/rest_based_cluster_manager.rb +15 -0
- data/src/swiftcore/Swiftiply/splay_cache_base.rb +21 -0
- data/src/swiftcore/Swiftiply/support_pagecache.rb +6 -3
- data/src/swiftcore/Swiftiply/swiftiply_2_http_proxy.rb +7 -0
- data/src/swiftcore/Swiftiply/swiftiply_client.rb +20 -5
- data/src/swiftcore/Swiftiply/version.rb +5 -0
- data/src/swiftcore/evented_mongrel.rb +26 -8
- data/src/swiftcore/hash.rb +43 -0
- data/src/swiftcore/method_builder.rb +28 -0
- data/src/swiftcore/streamer.rb +46 -0
- data/src/swiftcore/swiftiplied_mongrel.rb +91 -23
- data/src/swiftcore/types.rb +20 -3
- data/swiftiply.gemspec +14 -8
- data/test/TC_Deque.rb +152 -0
- data/test/TC_ProxyBag.rb +147 -166
- data/test/TC_Swiftiply.rb +576 -169
- data/test/TC_Swiftiply/mongrel/evented_hello.rb +1 -1
- data/test/TC_Swiftiply/mongrel/swiftiplied_hello.rb +1 -1
- data/test/TC_Swiftiply/test_serve_static_file_xsendfile/sendfile_client.rb +27 -0
- data/test/TC_Swiftiply/test_ssl/bin/validate_ssl_capability.rb +21 -0
- data/test/TC_Swiftiply/test_ssl/test.cert +16 -0
- data/test/TC_Swiftiply/test_ssl/test.key +15 -0
- data/{bin → test/bin}/echo_client +0 -0
- metadata +136 -94
- data/README +0 -126
- data/ext/swiftiply_parse/parse.rl +0 -90
@@ -0,0 +1,161 @@
|
|
1
|
+
# $Id: extconf.rb 4526 2007-07-03 18:04:34Z francis $
|
2
|
+
#
|
3
|
+
#----------------------------------------------------------------------------
|
4
|
+
#
|
5
|
+
# Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
6
|
+
# Gmail: garbagecat10
|
7
|
+
#
|
8
|
+
# This program is free software; you can redistribute it and/or modify
|
9
|
+
# it under the terms of either: 1) the GNU General Public License
|
10
|
+
# as published by the Free Software Foundation; either version 2 of the
|
11
|
+
# License, or (at your option) any later version; or 2) Ruby's License.
|
12
|
+
#
|
13
|
+
# See the file COPYING for complete licensing information.
|
14
|
+
#
|
15
|
+
#---------------------------------------------------------------------------
|
16
|
+
#
|
17
|
+
# extconf.rb for Fast File Reader
|
18
|
+
# We have to munge LDSHARED because this code needs a C++ link.
|
19
|
+
#
|
20
|
+
|
21
|
+
require 'mkmf'
|
22
|
+
|
23
|
+
flags = []
|
24
|
+
|
25
|
+
case RUBY_PLATFORM.split('-',2)[1]
|
26
|
+
when 'mswin32', 'mingw32', 'bccwin32'
|
27
|
+
unless have_header('windows.h') and
|
28
|
+
have_header('winsock.h') and
|
29
|
+
have_library('kernel32') and
|
30
|
+
have_library('rpcrt4') and
|
31
|
+
have_library('gdi32')
|
32
|
+
exit
|
33
|
+
end
|
34
|
+
|
35
|
+
flags << "-D OS_WIN32"
|
36
|
+
flags << '-D BUILD_FOR_RUBY'
|
37
|
+
flags << "-EHs"
|
38
|
+
flags << "-GR"
|
39
|
+
|
40
|
+
# dir_config('ssl')
|
41
|
+
# if have_library('ssleay32') and
|
42
|
+
# have_library('libeay32') and
|
43
|
+
# have_header('openssl/ssl.h') and
|
44
|
+
# have_header('openssl/err.h')
|
45
|
+
# flags << '-D WITH_SSL'
|
46
|
+
# else
|
47
|
+
# flags << '-D WITHOUT_SSL'
|
48
|
+
# end
|
49
|
+
|
50
|
+
when /solaris/
|
51
|
+
# unless have_library('pthread') and
|
52
|
+
# have_library('nsl') and
|
53
|
+
# have_library('socket')
|
54
|
+
# exit
|
55
|
+
# end
|
56
|
+
|
57
|
+
flags << '-D OS_UNIX'
|
58
|
+
flags << '-D OS_SOLARIS8'
|
59
|
+
flags << '-D BUILD_FOR_RUBY'
|
60
|
+
|
61
|
+
# dir_config('ssl')
|
62
|
+
# if have_library('ssl') and
|
63
|
+
# have_library('crypto') and
|
64
|
+
# have_header('openssl/ssl.h') and
|
65
|
+
# have_header('openssl/err.h')
|
66
|
+
# flags << '-D WITH_SSL'
|
67
|
+
# else
|
68
|
+
# flags << '-D WITHOUT_SSL'
|
69
|
+
# end
|
70
|
+
|
71
|
+
# on Unix we need a g++ link, not gcc.
|
72
|
+
CONFIG['LDSHARED'] = "$(CXX) -shared"
|
73
|
+
|
74
|
+
when /darwin/
|
75
|
+
flags << '-DOS_UNIX'
|
76
|
+
flags << '-DBUILD_FOR_RUBY'
|
77
|
+
|
78
|
+
# dir_config('ssl')
|
79
|
+
# if have_library('ssl') and
|
80
|
+
# have_library('crypto') and
|
81
|
+
# have_library('C') and
|
82
|
+
# have_header('openssl/ssl.h') and
|
83
|
+
# have_header('openssl/err.h')
|
84
|
+
# flags << '-DWITH_SSL'
|
85
|
+
# else
|
86
|
+
# flags << '-DWITHOUT_SSL'
|
87
|
+
# end
|
88
|
+
# on Unix we need a g++ link, not gcc.
|
89
|
+
# Ff line contributed by Daniel Harple.
|
90
|
+
CONFIG['LDSHARED'] = "$(CXX) " + CONFIG['LDSHARED'].split[1..-1].join(' ')
|
91
|
+
|
92
|
+
when /linux/
|
93
|
+
unless have_library('pthread')
|
94
|
+
exit
|
95
|
+
end
|
96
|
+
|
97
|
+
flags << '-DOS_UNIX'
|
98
|
+
flags << '-DBUILD_FOR_RUBY'
|
99
|
+
|
100
|
+
# Original epoll test is inadequate because 2.4 kernels have the header
|
101
|
+
# but not the code.
|
102
|
+
# #flags << '-DHAVE_EPOLL' if have_header('sys/epoll.h')
|
103
|
+
# if have_header('sys/epoll.h')
|
104
|
+
# File.open("hasEpollTest.c", "w") {|f|
|
105
|
+
# f.puts "#include <sys/epoll.h>"
|
106
|
+
# f.puts "int main() { epoll_create(1024); return 0;}"
|
107
|
+
# }
|
108
|
+
# (e = system( "gcc hasEpollTest.c -o hasEpollTest " )) and (e = $?.to_i)
|
109
|
+
# `rm -f hasEpollTest.c hasEpollTest`
|
110
|
+
# flags << '-DHAVE_EPOLL' if e == 0
|
111
|
+
# end
|
112
|
+
#
|
113
|
+
# dir_config('ssl')
|
114
|
+
# if have_library('ssl') and
|
115
|
+
# have_library('crypto') and
|
116
|
+
# have_header('openssl/ssl.h') and
|
117
|
+
# have_header('openssl/err.h')
|
118
|
+
# flags << '-DWITH_SSL'
|
119
|
+
# else
|
120
|
+
# flags << '-DWITHOUT_SSL'
|
121
|
+
# end
|
122
|
+
# on Unix we need a g++ link, not gcc.
|
123
|
+
CONFIG['LDSHARED'] = "$(CXX) -shared"
|
124
|
+
|
125
|
+
# Modify the mkmf constant LINK_SO so the generated shared object is stripped.
|
126
|
+
# You might think modifying CONFIG['LINK_SO'] would be a better way to do this,
|
127
|
+
# but it doesn't work because mkmf doesn't look at CONFIG['LINK_SO'] again after
|
128
|
+
# it initializes.
|
129
|
+
#linkso = Object.send :remove_const, "LINK_SO"
|
130
|
+
#LINK_SO = linkso + "; strip $@"
|
131
|
+
|
132
|
+
else
|
133
|
+
# unless have_library('pthread')
|
134
|
+
# exit
|
135
|
+
# end
|
136
|
+
|
137
|
+
flags << '-DOS_UNIX'
|
138
|
+
flags << '-DBUILD_FOR_RUBY'
|
139
|
+
|
140
|
+
# dir_config('ssl')
|
141
|
+
# if have_library('ssl') and
|
142
|
+
# have_library('crypto') and
|
143
|
+
# have_header('openssl/ssl.h') and
|
144
|
+
# have_header('openssl/err.h')
|
145
|
+
# flags << '-DWITH_SSL'
|
146
|
+
# else
|
147
|
+
# flags << '-DWITHOUT_SSL'
|
148
|
+
# end
|
149
|
+
# on Unix we need a g++ link, not gcc.
|
150
|
+
CONFIG['LDSHARED'] = "$(CXX) -shared"
|
151
|
+
|
152
|
+
end
|
153
|
+
|
154
|
+
if $CPPFLAGS
|
155
|
+
$CPPFLAGS += ' ' + flags.join(' ')
|
156
|
+
else
|
157
|
+
$CFLAGS += ' ' + flags.join(' ')
|
158
|
+
end
|
159
|
+
|
160
|
+
|
161
|
+
create_makefile("swiftcore/splaytreemap","swiftcore")
|
@@ -0,0 +1,580 @@
|
|
1
|
+
#include "splay_map.h"
|
2
|
+
#include <iostream>
|
3
|
+
#include <string>
|
4
|
+
#include "string.h"
|
5
|
+
using namespace std;
|
6
|
+
|
7
|
+
#include <ruby.h>
|
8
|
+
|
9
|
+
static VALUE SwiftcoreModule;
|
10
|
+
static VALUE SplayTreeMapClass;
|
11
|
+
|
12
|
+
struct classcomp {
|
13
|
+
bool operator() (const char *s1, const char *s2) const
|
14
|
+
{
|
15
|
+
if ( strcmp(s1,s2) < 0) {
|
16
|
+
return true;
|
17
|
+
} else {
|
18
|
+
return false;
|
19
|
+
}
|
20
|
+
}
|
21
|
+
};
|
22
|
+
|
23
|
+
typedef swiftcore::splay_map<char *,VALUE,classcomp> rb_splaymap;
|
24
|
+
typedef swiftcore::splay_map<char *,VALUE,classcomp>::iterator rb_splaymap_iterator;
|
25
|
+
|
26
|
+
static void sptm_mark (rb_splaymap* st)
|
27
|
+
{
|
28
|
+
rb_splaymap_iterator q_iterator;
|
29
|
+
if (st) {
|
30
|
+
for ( q_iterator = (*st).begin(); q_iterator != (*st).end(); q_iterator++ ) {
|
31
|
+
rb_gc_mark(q_iterator->second);
|
32
|
+
}
|
33
|
+
}
|
34
|
+
}
|
35
|
+
|
36
|
+
static void sptm_free (rb_splaymap* st)
|
37
|
+
{
|
38
|
+
if (st)
|
39
|
+
delete st;
|
40
|
+
}
|
41
|
+
|
42
|
+
static VALUE sptm_alloc (VALUE klass)
|
43
|
+
{
|
44
|
+
rb_splaymap* st = new rb_splaymap;
|
45
|
+
VALUE v = Data_Wrap_Struct (klass, sptm_mark, sptm_free, st);
|
46
|
+
return v;
|
47
|
+
}
|
48
|
+
|
49
|
+
static VALUE sptm_initialize (VALUE self)
|
50
|
+
{
|
51
|
+
return Qnil;
|
52
|
+
}
|
53
|
+
|
54
|
+
static VALUE sptm_parent (VALUE self)
|
55
|
+
{
|
56
|
+
rb_splaymap* st = NULL;
|
57
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
58
|
+
if (!st)
|
59
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
60
|
+
|
61
|
+
if ((*st).empty()) {
|
62
|
+
return Qnil;
|
63
|
+
} else {
|
64
|
+
rb_splaymap_iterator parent = (*st).parent();
|
65
|
+
VALUE r = rb_ary_new();
|
66
|
+
|
67
|
+
rb_ary_push(r,rb_str_new2(parent->first));
|
68
|
+
rb_ary_push(r,parent->second);
|
69
|
+
return r;
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
73
|
+
static VALUE sptm_pop_front (VALUE self)
|
74
|
+
{
|
75
|
+
rb_splaymap* st = NULL;
|
76
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
77
|
+
if (!st)
|
78
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
79
|
+
|
80
|
+
if ((*st).empty()) {
|
81
|
+
return Qnil;
|
82
|
+
} else {
|
83
|
+
rb_splaymap_iterator front = (*st).begin();
|
84
|
+
VALUE r = rb_ary_new();
|
85
|
+
|
86
|
+
rb_ary_push(r,rb_str_new2(front->first));
|
87
|
+
rb_ary_push(r,front->second);
|
88
|
+
(*st).erase(front);
|
89
|
+
return r;
|
90
|
+
}
|
91
|
+
}
|
92
|
+
|
93
|
+
static VALUE sptm_pop_back (VALUE self)
|
94
|
+
{
|
95
|
+
rb_splaymap* st = NULL;
|
96
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
97
|
+
if (!st)
|
98
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
99
|
+
|
100
|
+
if ((*st).empty()) {
|
101
|
+
return Qnil;
|
102
|
+
} else {
|
103
|
+
rb_splaymap_iterator back = (*st).end();
|
104
|
+
back--;
|
105
|
+
VALUE r = rb_ary_new();
|
106
|
+
rb_ary_push(r,rb_str_new2(back->first));
|
107
|
+
rb_ary_push(r,back->second);
|
108
|
+
(*st).erase(back);
|
109
|
+
return r;
|
110
|
+
}
|
111
|
+
}
|
112
|
+
|
113
|
+
|
114
|
+
static VALUE sptm_size (VALUE self)
|
115
|
+
{
|
116
|
+
rb_splaymap* st = NULL;
|
117
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
118
|
+
if (!st)
|
119
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
120
|
+
|
121
|
+
return INT2NUM((*st).size());
|
122
|
+
}
|
123
|
+
|
124
|
+
|
125
|
+
static VALUE sptm_max_capacity (VALUE self)
|
126
|
+
{
|
127
|
+
rb_splaymap* st = NULL;
|
128
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
129
|
+
if (!st)
|
130
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
131
|
+
|
132
|
+
return INT2NUM((*st).max_size());
|
133
|
+
}
|
134
|
+
|
135
|
+
static VALUE sptm_clear (VALUE self)
|
136
|
+
{
|
137
|
+
rb_splaymap* st = NULL;
|
138
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
139
|
+
if (!st)
|
140
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
141
|
+
|
142
|
+
(*st).clear();
|
143
|
+
return self;
|
144
|
+
}
|
145
|
+
|
146
|
+
static VALUE sptm_empty (VALUE self)
|
147
|
+
{
|
148
|
+
rb_splaymap* st = NULL;
|
149
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
150
|
+
if (!st)
|
151
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
152
|
+
|
153
|
+
if ((*st).empty()) {
|
154
|
+
return Qtrue;
|
155
|
+
} else {
|
156
|
+
return Qfalse;
|
157
|
+
}
|
158
|
+
}
|
159
|
+
|
160
|
+
static VALUE sptm_to_s (VALUE self)
|
161
|
+
{
|
162
|
+
VALUE s = rb_str_new2("");
|
163
|
+
|
164
|
+
rb_splaymap* st = NULL;
|
165
|
+
rb_splaymap_iterator q_iterator;
|
166
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
167
|
+
if (!st)
|
168
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
169
|
+
|
170
|
+
for ( q_iterator = (*st).begin(); q_iterator != (*st).end(); q_iterator++ ) {
|
171
|
+
rb_str_concat(s,rb_str_new2(q_iterator->first));
|
172
|
+
rb_str_concat(s,rb_convert_type(q_iterator->second, T_STRING, "String", "to_str"));
|
173
|
+
}
|
174
|
+
|
175
|
+
return rb_str_to_str(s);
|
176
|
+
}
|
177
|
+
|
178
|
+
static VALUE sptm_to_a (VALUE self)
|
179
|
+
{
|
180
|
+
VALUE ary = rb_ary_new();
|
181
|
+
|
182
|
+
rb_splaymap* st = NULL;
|
183
|
+
rb_splaymap_iterator q_iterator;
|
184
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
185
|
+
if (!st)
|
186
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
187
|
+
|
188
|
+
for ( q_iterator = (*st).begin(); q_iterator != (*st).end(); q_iterator++ ) {
|
189
|
+
rb_ary_push(ary,rb_str_new2(q_iterator->first));
|
190
|
+
rb_ary_push(ary,q_iterator->second);
|
191
|
+
}
|
192
|
+
|
193
|
+
return ary;
|
194
|
+
}
|
195
|
+
|
196
|
+
static VALUE sptm_inspect (VALUE self)
|
197
|
+
{
|
198
|
+
VALUE s = rb_str_new2("{");
|
199
|
+
VALUE arrow = rb_str_new2("=>");
|
200
|
+
VALUE comma = rb_str_new2(",");
|
201
|
+
|
202
|
+
rb_splaymap* st = NULL;
|
203
|
+
rb_splaymap_iterator q_iterator;
|
204
|
+
rb_splaymap_iterator last_q_iterator;
|
205
|
+
rb_splaymap_iterator before_last_q_iterator;
|
206
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
207
|
+
if (!st)
|
208
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
209
|
+
|
210
|
+
before_last_q_iterator = last_q_iterator = (*st).end();
|
211
|
+
before_last_q_iterator--;
|
212
|
+
|
213
|
+
for ( q_iterator = (*st).begin(); q_iterator != last_q_iterator; q_iterator++ ) {
|
214
|
+
rb_str_concat(s,rb_inspect(rb_str_new2(q_iterator->first)));
|
215
|
+
rb_str_concat(s,arrow);
|
216
|
+
rb_str_concat(s,rb_inspect(q_iterator->second));
|
217
|
+
if (q_iterator != before_last_q_iterator)
|
218
|
+
rb_str_concat(s,comma);
|
219
|
+
}
|
220
|
+
rb_str_concat(s,rb_str_new2("}"));
|
221
|
+
|
222
|
+
return rb_str_to_str(s);
|
223
|
+
}
|
224
|
+
|
225
|
+
static VALUE sptm_first (VALUE self)
|
226
|
+
{
|
227
|
+
rb_splaymap* st = NULL;
|
228
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
229
|
+
if (!st)
|
230
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
231
|
+
|
232
|
+
if ((*st).empty()) {
|
233
|
+
return Qnil;
|
234
|
+
} else {
|
235
|
+
rb_splaymap_iterator front = (*st).begin();
|
236
|
+
VALUE r = rb_ary_new();
|
237
|
+
rb_ary_push(r,rb_str_new2(front->first));
|
238
|
+
rb_ary_push(r,front->second);
|
239
|
+
return r;
|
240
|
+
}
|
241
|
+
}
|
242
|
+
|
243
|
+
static VALUE sptm_last (VALUE self)
|
244
|
+
{
|
245
|
+
rb_splaymap* st = NULL;
|
246
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
247
|
+
if (!st)
|
248
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
249
|
+
|
250
|
+
if ((*st).empty()) {
|
251
|
+
return Qnil;
|
252
|
+
} else {
|
253
|
+
rb_splaymap_iterator back = (*st).end();
|
254
|
+
back--;
|
255
|
+
VALUE r = rb_ary_new();
|
256
|
+
rb_ary_push(r,rb_str_new2(back->first));
|
257
|
+
rb_ary_push(r,back->second);
|
258
|
+
return r;
|
259
|
+
}
|
260
|
+
}
|
261
|
+
|
262
|
+
static VALUE sptm_replace (VALUE self, VALUE new_st)
|
263
|
+
{
|
264
|
+
rb_splaymap* st = NULL;
|
265
|
+
rb_splaymap* nst = NULL;
|
266
|
+
rb_splaymap_iterator q_iterator;
|
267
|
+
rb_splaymap_iterator last_q_iterator;
|
268
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
269
|
+
Data_Get_Struct(new_st, rb_splaymap, nst);
|
270
|
+
if (!st)
|
271
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
272
|
+
|
273
|
+
if (!nst)
|
274
|
+
rb_raise (rb_eException, "No SplayTreeMap object to copy");
|
275
|
+
|
276
|
+
(*st).clear();
|
277
|
+
last_q_iterator = (*st).end();
|
278
|
+
for ( q_iterator = (*nst).begin(); q_iterator != last_q_iterator; q_iterator++ ) {
|
279
|
+
(*st).insert(*q_iterator);
|
280
|
+
}
|
281
|
+
|
282
|
+
return self;
|
283
|
+
}
|
284
|
+
|
285
|
+
static VALUE sptm_insert (VALUE self, VALUE key, VALUE val)
|
286
|
+
{
|
287
|
+
rb_splaymap* st = NULL;
|
288
|
+
std::pair<rb_splaymap_iterator, bool> rv;
|
289
|
+
std::pair<char *,VALUE> sp;
|
290
|
+
char * skey;
|
291
|
+
char * svp;
|
292
|
+
|
293
|
+
svp = StringValuePtr(key);
|
294
|
+
skey = new char[strlen(svp)+1];
|
295
|
+
strcpy(skey,svp);
|
296
|
+
|
297
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
298
|
+
|
299
|
+
if (!st)
|
300
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
301
|
+
|
302
|
+
sp = make_pair(skey,val);
|
303
|
+
rv = (*st).insert(sp);
|
304
|
+
if (!rv.second) {
|
305
|
+
(*st).erase(rv.first);
|
306
|
+
(*st).insert(sp);
|
307
|
+
}
|
308
|
+
|
309
|
+
return self;
|
310
|
+
}
|
311
|
+
|
312
|
+
static VALUE sptm_at (VALUE self, VALUE key)
|
313
|
+
{
|
314
|
+
rb_splaymap* st = NULL;
|
315
|
+
rb_splaymap_iterator q_iterator;
|
316
|
+
|
317
|
+
char * skey = StringValuePtr(key);
|
318
|
+
|
319
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
320
|
+
|
321
|
+
if (!st)
|
322
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
323
|
+
|
324
|
+
q_iterator = (*st).find(skey);
|
325
|
+
if (q_iterator == (*st).end()) {
|
326
|
+
return Qnil;
|
327
|
+
} else {
|
328
|
+
return q_iterator->second;
|
329
|
+
}
|
330
|
+
}
|
331
|
+
|
332
|
+
static VALUE sptm_has_key (VALUE self, VALUE key)
|
333
|
+
{
|
334
|
+
rb_splaymap* st = NULL;
|
335
|
+
rb_splaymap_iterator q_iterator;
|
336
|
+
|
337
|
+
char * skey = StringValuePtr(key);
|
338
|
+
|
339
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
340
|
+
|
341
|
+
if (!st)
|
342
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
343
|
+
|
344
|
+
q_iterator = (*st).find(skey);
|
345
|
+
if (q_iterator == (*st).end()) {
|
346
|
+
return Qnil;
|
347
|
+
} else {
|
348
|
+
return key;
|
349
|
+
}
|
350
|
+
}
|
351
|
+
|
352
|
+
static VALUE sptm_delete (VALUE self, VALUE key)
|
353
|
+
{
|
354
|
+
rb_splaymap* st = NULL;
|
355
|
+
rb_splaymap_iterator q_iterator;
|
356
|
+
|
357
|
+
char * skey = StringValuePtr(key);
|
358
|
+
|
359
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
360
|
+
|
361
|
+
if (!st)
|
362
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
363
|
+
|
364
|
+
q_iterator = (*st).find(skey);
|
365
|
+
if (q_iterator == (*st).end()) {
|
366
|
+
return Qnil;
|
367
|
+
} else {
|
368
|
+
(*st).erase(q_iterator);
|
369
|
+
}
|
370
|
+
|
371
|
+
return self;
|
372
|
+
}
|
373
|
+
|
374
|
+
/*
|
375
|
+
static VALUE sptm_delete_value (VALUE self, VALUE obj)
|
376
|
+
{
|
377
|
+
rb_splaymap* st = NULL;
|
378
|
+
rb_splaymap_iterator q_iterator;
|
379
|
+
rb_splaymap_iterator last_q_iterator;
|
380
|
+
|
381
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
382
|
+
|
383
|
+
if (!st)
|
384
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
385
|
+
|
386
|
+
last_q_iterator = (*st).end();
|
387
|
+
for ( q_iterator = (*st).begin(); q_iterator != last_q_iterator; q_iterator++ ) {
|
388
|
+
if (rb_equal(q_iterator->second, obj)) {
|
389
|
+
(*st).erase(q_iterator);
|
390
|
+
break;
|
391
|
+
}
|
392
|
+
}
|
393
|
+
|
394
|
+
return self;
|
395
|
+
}
|
396
|
+
*/
|
397
|
+
|
398
|
+
static VALUE sptm_each (VALUE self)
|
399
|
+
{
|
400
|
+
rb_splaymap* st = NULL;
|
401
|
+
rb_splaymap_iterator q_iterator;
|
402
|
+
rb_splaymap_iterator last_q_iterator;
|
403
|
+
|
404
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
405
|
+
|
406
|
+
if (!st)
|
407
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
408
|
+
|
409
|
+
last_q_iterator = (*st).end();
|
410
|
+
for ( q_iterator = (*st).begin(); q_iterator != last_q_iterator; q_iterator++ ) {
|
411
|
+
rb_yield_values(2,rb_str_new2(q_iterator->first),q_iterator->second);
|
412
|
+
}
|
413
|
+
|
414
|
+
return self;
|
415
|
+
}
|
416
|
+
|
417
|
+
static VALUE sptm_index (VALUE self, VALUE match_obj)
|
418
|
+
{
|
419
|
+
rb_splaymap* st = NULL;
|
420
|
+
rb_splaymap_iterator q_iterator;
|
421
|
+
rb_splaymap_iterator last_q_iterator;
|
422
|
+
|
423
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
424
|
+
|
425
|
+
if (!st)
|
426
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
427
|
+
|
428
|
+
last_q_iterator = (*st).end();
|
429
|
+
for ( q_iterator = (*st).begin(); q_iterator != last_q_iterator; q_iterator++ ) {
|
430
|
+
if (rb_equal(q_iterator->second, match_obj)) {
|
431
|
+
return rb_str_new2(q_iterator->first);
|
432
|
+
}
|
433
|
+
}
|
434
|
+
return Qnil;
|
435
|
+
}
|
436
|
+
|
437
|
+
static VALUE sptm_keys (VALUE self)
|
438
|
+
{
|
439
|
+
rb_splaymap* st = NULL;
|
440
|
+
rb_splaymap_iterator q_iterator;
|
441
|
+
rb_splaymap_iterator last_q_iterator;
|
442
|
+
|
443
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
444
|
+
|
445
|
+
if (!st)
|
446
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
447
|
+
|
448
|
+
last_q_iterator = (*st).end();
|
449
|
+
VALUE r = rb_ary_new();
|
450
|
+
for ( q_iterator = (*st).begin(); q_iterator != last_q_iterator; q_iterator++ ) {
|
451
|
+
rb_ary_push(r,rb_str_new2(q_iterator->first));
|
452
|
+
}
|
453
|
+
|
454
|
+
return r;
|
455
|
+
}
|
456
|
+
|
457
|
+
static VALUE sptm_values (VALUE self)
|
458
|
+
{
|
459
|
+
rb_splaymap* st = NULL;
|
460
|
+
rb_splaymap_iterator q_iterator;
|
461
|
+
rb_splaymap_iterator last_q_iterator;
|
462
|
+
|
463
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
464
|
+
|
465
|
+
if (!st)
|
466
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
467
|
+
|
468
|
+
last_q_iterator = (*st).end();
|
469
|
+
VALUE r = rb_ary_new();
|
470
|
+
for ( q_iterator = (*st).begin(); q_iterator != last_q_iterator; q_iterator++ ) {
|
471
|
+
rb_ary_push(r,q_iterator->second);
|
472
|
+
}
|
473
|
+
|
474
|
+
return r;
|
475
|
+
}
|
476
|
+
|
477
|
+
static VALUE sptm_erase_childfree(VALUE self)
|
478
|
+
{
|
479
|
+
rb_splaymap* st = NULL;
|
480
|
+
|
481
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
482
|
+
|
483
|
+
if (!st)
|
484
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
485
|
+
|
486
|
+
(*st).erase_childfree_nodes();
|
487
|
+
|
488
|
+
return Qnil;
|
489
|
+
}
|
490
|
+
|
491
|
+
static VALUE sptm_get_max_size(VALUE self) {
|
492
|
+
rb_splaymap* st = NULL;
|
493
|
+
|
494
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
495
|
+
|
496
|
+
if (!st)
|
497
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
498
|
+
|
499
|
+
return INT2FIX((*st).get_max_permitted_size());
|
500
|
+
}
|
501
|
+
|
502
|
+
static VALUE sptm_set_max_size(VALUE self, VALUE sz) {
|
503
|
+
int n = FIX2INT(sz);
|
504
|
+
rb_splaymap* st = NULL;
|
505
|
+
|
506
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
507
|
+
|
508
|
+
if (n < 0)
|
509
|
+
rb_raise (rb_eException, "Size can not be less than zero.");
|
510
|
+
|
511
|
+
if (!st)
|
512
|
+
rb_raise (rb_eException, "No SplayTreeMap Object");
|
513
|
+
|
514
|
+
(*st).set_max_permitted_size(n);
|
515
|
+
|
516
|
+
while ((*st).size() > n) {
|
517
|
+
(*st).erase_childfree_nodes();
|
518
|
+
}
|
519
|
+
|
520
|
+
return sz;
|
521
|
+
}
|
522
|
+
|
523
|
+
/**********************
|
524
|
+
Init_sptm
|
525
|
+
**********************/
|
526
|
+
|
527
|
+
extern "C" void Init_splaytreemap()
|
528
|
+
{
|
529
|
+
SwiftcoreModule = rb_define_module ("Swiftcore");
|
530
|
+
SplayTreeMapClass = rb_define_class_under (SwiftcoreModule, "SplayTreeMap", rb_cObject);
|
531
|
+
|
532
|
+
rb_define_alloc_func (SplayTreeMapClass, sptm_alloc);
|
533
|
+
rb_define_method (SplayTreeMapClass, "initialize", (VALUE(*)(...))sptm_initialize,0);
|
534
|
+
// ==
|
535
|
+
rb_define_method (SplayTreeMapClass, "[]", (VALUE(*)(...))sptm_at,1);
|
536
|
+
rb_define_method (SplayTreeMapClass, "[]=", (VALUE(*)(...))sptm_insert,2);
|
537
|
+
rb_define_method (SplayTreeMapClass, "at", (VALUE(*)(...))sptm_at,1);
|
538
|
+
rb_define_method (SplayTreeMapClass, "clear", (VALUE(*)(...))sptm_clear,0);
|
539
|
+
rb_define_method (SplayTreeMapClass, "clear_childfree", (VALUE(*)(...))sptm_erase_childfree,0);
|
540
|
+
rb_define_method (SplayTreeMapClass, "delete", (VALUE(*)(...))sptm_delete,1);
|
541
|
+
// delete_if
|
542
|
+
// delete_values
|
543
|
+
rb_define_method (SplayTreeMapClass, "each", (VALUE(*)(...))sptm_each,0);
|
544
|
+
// each_key
|
545
|
+
rb_define_method (SplayTreeMapClass, "each_pair", (VALUE(*)(...))sptm_each,0);
|
546
|
+
// each_value
|
547
|
+
rb_define_method (SplayTreeMapClass, "empty?", (VALUE(*)(...))sptm_empty,0);
|
548
|
+
rb_define_method (SplayTreeMapClass, "fetch", (VALUE(*)(...))sptm_at,1);
|
549
|
+
rb_define_method (SplayTreeMapClass, "first", (VALUE(*)(...))sptm_first,0);
|
550
|
+
rb_define_method (SplayTreeMapClass, "has_key?", (VALUE(*)(...))sptm_has_key,1);
|
551
|
+
// has_value?
|
552
|
+
rb_define_method (SplayTreeMapClass, "include?", (VALUE(*)(...))sptm_has_key,1);
|
553
|
+
rb_define_method (SplayTreeMapClass, "index", (VALUE(*)(...))sptm_index,1);
|
554
|
+
// indices
|
555
|
+
rb_define_method (SplayTreeMapClass, "insert", (VALUE(*)(...))sptm_insert,2);
|
556
|
+
rb_define_method (SplayTreeMapClass, "inspect", (VALUE(*)(...))sptm_inspect,0);
|
557
|
+
// invert
|
558
|
+
rb_define_method (SplayTreeMapClass, "key?", (VALUE(*)(...))sptm_has_key,1);
|
559
|
+
rb_define_method (SplayTreeMapClass, "keys", (VALUE(*)(...))sptm_keys,0);
|
560
|
+
rb_define_method (SplayTreeMapClass, "last", (VALUE(*)(...))sptm_last,0);
|
561
|
+
rb_define_method (SplayTreeMapClass, "length", (VALUE(*)(...))sptm_size,0);
|
562
|
+
rb_define_method (SplayTreeMapClass, "max_capacity", (VALUE(*)(...))sptm_max_capacity,0);
|
563
|
+
rb_define_method (SplayTreeMapClass, "max_size", (VALUE(*)(...))sptm_get_max_size,0);
|
564
|
+
rb_define_method (SplayTreeMapClass, "max_size=", (VALUE(*)(...))sptm_set_max_size,1);
|
565
|
+
rb_define_method (SplayTreeMapClass, "member?", (VALUE(*)(...))sptm_has_key,1);
|
566
|
+
rb_define_method (SplayTreeMapClass, "parent", (VALUE(*)(...))sptm_parent,0);
|
567
|
+
rb_define_method (SplayTreeMapClass, "pop", (VALUE(*)(...))sptm_pop_back,0);
|
568
|
+
rb_define_method (SplayTreeMapClass, "replace", (VALUE(*)(...))sptm_replace,1);
|
569
|
+
rb_define_method (SplayTreeMapClass, "shift", (VALUE(*)(...))sptm_pop_front,0);
|
570
|
+
rb_define_method (SplayTreeMapClass, "size", (VALUE(*)(...))sptm_size,0);
|
571
|
+
rb_define_method (SplayTreeMapClass, "to_a", (VALUE(*)(...))sptm_to_a,0);
|
572
|
+
rb_define_method (SplayTreeMapClass, "to_s", (VALUE(*)(...))sptm_to_s,0);
|
573
|
+
rb_define_method (SplayTreeMapClass, "values", (VALUE(*)(...))sptm_values,0);
|
574
|
+
// values_at
|
575
|
+
|
576
|
+
rb_include_module (SplayTreeMapClass, rb_mEnumerable);
|
577
|
+
|
578
|
+
/* == [] [] []= clear default default= default_proc delete delete_if each each_key each_pair each_value empty? fetch has_key? has_value? include? index indexes indices initialize_copy inspect invert key? keys length member? merge merge! new pretty_print pretty_print_cycle rehash reject reject! replace select shift size sort store to_a to_hash to_s to_yaml update value? values values_at */
|
579
|
+
// rb_include_module (SplayTreeMapClass, rb_mEnumerable);
|
580
|
+
}
|