splaytreemap 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +52 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/ext/splaytreemap/extconf.rb +3 -0
- data/ext/splaytreemap/rubymain.cpp +576 -0
- data/ext/splaytreemap/splay_map.h +635 -0
- data/ext/splaytreemap/splay_set.h +575 -0
- data/ext/splaytreemap/splay_tree.h +1127 -0
- data/lib/splaytreemap/version.rb +3 -0
- data/splaytreemap.gemspec +27 -0
- metadata +122 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ef7502890eaa305981bea1ec2741dd4e27c5b3d5
|
4
|
+
data.tar.gz: 80a433489f405869ac326e30aec1c5bda9d7d2e0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cbd0ca129ef852befe205f32689bba4017567a3d0fc13afe24f8dd8f4b48232ae6cd0b214441a5ffbb02b6445bde28bedcfcd64f6c07133c0a95e502e6406503
|
7
|
+
data.tar.gz: 0f51bdfea3cd3d405541f1ac5e9ffad6019c426db2b11ff8c8f68ef4ad68e50f6f1ae9f3dbe7b2980ba198b3c0092bf938dcc03bcbd4763695b6c4283cb4c013
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at wyhaines@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Kirk Haines
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Splaytreemap
|
2
|
+
|
3
|
+
A Splay Tree is a self adjusting binary search tree with the additional property that recently accessed elements are faster to access than less recently accessed elements. This makes them quite useful for the implementation of caches.
|
4
|
+
|
5
|
+
This particular implementation also implements a twist to the Splay Tree such that if it is given a maximum size, once it's element count exceeds that maximum size, it will purge all elemements which are stored in nodes at the terminal ends of the data structure. This should generally result in a bit more than half of the nodes being purged, though the exact number will depend on how the tree is balanced at the time, which is in turn affected by which nodes have been accessed recently.
|
6
|
+
|
7
|
+
This implementation is written in C++, with a Ruby interface that seeks to provide a Hash-like API. This data structure has been in my toolbox for a long time, but it can almost certainly use some improvement of the API to be even more transparently Hash-like.
|
8
|
+
|
9
|
+
The current implementation has a limitation that the key for the splay tree map has to be a String value.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'splaytreemap'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install splaytreemap
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'splaytreemap'
|
31
|
+
|
32
|
+
s = Splaytreemap.new
|
33
|
+
|
34
|
+
s.max_size = 1000
|
35
|
+
|
36
|
+
s['this'] = 'that'
|
37
|
+
|
38
|
+
## Development
|
39
|
+
|
40
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
41
|
+
|
42
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/splaytreemap. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
47
|
+
|
48
|
+
|
49
|
+
## License
|
50
|
+
|
51
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
52
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "splaytreemap"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,576 @@
|
|
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 SplayTreeMapClass;
|
10
|
+
|
11
|
+
struct classcomp {
|
12
|
+
bool operator() (const char *s1, const char *s2) const
|
13
|
+
{
|
14
|
+
if ( strcmp(s1,s2) < 0) {
|
15
|
+
return true;
|
16
|
+
} else {
|
17
|
+
return false;
|
18
|
+
}
|
19
|
+
}
|
20
|
+
};
|
21
|
+
|
22
|
+
typedef swiftcore::splay_map<char *,VALUE,classcomp> rb_splaymap;
|
23
|
+
typedef swiftcore::splay_map<char *,VALUE,classcomp>::iterator rb_splaymap_iterator;
|
24
|
+
|
25
|
+
static void sptm_mark (rb_splaymap* st)
|
26
|
+
{
|
27
|
+
rb_splaymap_iterator q_iterator;
|
28
|
+
if (st) {
|
29
|
+
for ( q_iterator = (*st).begin(); q_iterator != (*st).end(); q_iterator++ ) {
|
30
|
+
rb_gc_mark(q_iterator->second);
|
31
|
+
}
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
static void sptm_free (rb_splaymap* st)
|
36
|
+
{
|
37
|
+
if (st)
|
38
|
+
delete st;
|
39
|
+
}
|
40
|
+
|
41
|
+
static VALUE sptm_alloc (VALUE klass)
|
42
|
+
{
|
43
|
+
rb_splaymap* st = new rb_splaymap;
|
44
|
+
VALUE v = Data_Wrap_Struct (klass, sptm_mark, sptm_free, st);
|
45
|
+
return v;
|
46
|
+
}
|
47
|
+
|
48
|
+
static VALUE sptm_initialize (VALUE self)
|
49
|
+
{
|
50
|
+
return Qnil;
|
51
|
+
}
|
52
|
+
|
53
|
+
static VALUE sptm_parent (VALUE self)
|
54
|
+
{
|
55
|
+
rb_splaymap* st = NULL;
|
56
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
57
|
+
if (!st)
|
58
|
+
rb_raise (rb_eException, "No Splaytreemap Object");
|
59
|
+
|
60
|
+
if ((*st).empty()) {
|
61
|
+
return Qnil;
|
62
|
+
} else {
|
63
|
+
rb_splaymap_iterator parent = (*st).parent();
|
64
|
+
VALUE r = rb_ary_new();
|
65
|
+
|
66
|
+
rb_ary_push(r,rb_str_new2(parent->first));
|
67
|
+
rb_ary_push(r,parent->second);
|
68
|
+
return r;
|
69
|
+
}
|
70
|
+
}
|
71
|
+
|
72
|
+
static VALUE sptm_pop_front (VALUE self)
|
73
|
+
{
|
74
|
+
rb_splaymap* st = NULL;
|
75
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
76
|
+
if (!st)
|
77
|
+
rb_raise (rb_eException, "No Splaytreemap Object");
|
78
|
+
|
79
|
+
if ((*st).empty()) {
|
80
|
+
return Qnil;
|
81
|
+
} else {
|
82
|
+
rb_splaymap_iterator front = (*st).begin();
|
83
|
+
VALUE r = rb_ary_new();
|
84
|
+
|
85
|
+
rb_ary_push(r,rb_str_new2(front->first));
|
86
|
+
rb_ary_push(r,front->second);
|
87
|
+
(*st).erase(front);
|
88
|
+
return r;
|
89
|
+
}
|
90
|
+
}
|
91
|
+
|
92
|
+
static VALUE sptm_pop_back (VALUE self)
|
93
|
+
{
|
94
|
+
rb_splaymap* st = NULL;
|
95
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
96
|
+
if (!st)
|
97
|
+
rb_raise (rb_eException, "No Splaytreemap Object");
|
98
|
+
|
99
|
+
if ((*st).empty()) {
|
100
|
+
return Qnil;
|
101
|
+
} else {
|
102
|
+
rb_splaymap_iterator back = (*st).end();
|
103
|
+
back--;
|
104
|
+
VALUE r = rb_ary_new();
|
105
|
+
rb_ary_push(r,rb_str_new2(back->first));
|
106
|
+
rb_ary_push(r,back->second);
|
107
|
+
(*st).erase(back);
|
108
|
+
return r;
|
109
|
+
}
|
110
|
+
}
|
111
|
+
|
112
|
+
|
113
|
+
static VALUE sptm_size (VALUE self)
|
114
|
+
{
|
115
|
+
rb_splaymap* st = NULL;
|
116
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
117
|
+
if (!st)
|
118
|
+
rb_raise (rb_eException, "No Splaytreemap Object");
|
119
|
+
|
120
|
+
return INT2NUM((*st).size());
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
static VALUE sptm_max_capacity (VALUE self)
|
125
|
+
{
|
126
|
+
rb_splaymap* st = NULL;
|
127
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
128
|
+
if (!st)
|
129
|
+
rb_raise (rb_eException, "No Splaytreemap object");
|
130
|
+
|
131
|
+
return INT2NUM((*st).max_size());
|
132
|
+
}
|
133
|
+
|
134
|
+
static VALUE sptm_clear (VALUE self)
|
135
|
+
{
|
136
|
+
rb_splaymap* st = NULL;
|
137
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
138
|
+
if (!st)
|
139
|
+
rb_raise (rb_eException, "No Splaytreemap Object");
|
140
|
+
|
141
|
+
(*st).clear();
|
142
|
+
return self;
|
143
|
+
}
|
144
|
+
|
145
|
+
static VALUE sptm_empty (VALUE self)
|
146
|
+
{
|
147
|
+
rb_splaymap* st = NULL;
|
148
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
149
|
+
if (!st)
|
150
|
+
rb_raise (rb_eException, "No Splaytreemap Object");
|
151
|
+
|
152
|
+
if ((*st).empty()) {
|
153
|
+
return Qtrue;
|
154
|
+
} else {
|
155
|
+
return Qfalse;
|
156
|
+
}
|
157
|
+
}
|
158
|
+
|
159
|
+
static VALUE sptm_to_s (VALUE self)
|
160
|
+
{
|
161
|
+
VALUE s = rb_str_new2("");
|
162
|
+
|
163
|
+
rb_splaymap* st = NULL;
|
164
|
+
rb_splaymap_iterator q_iterator;
|
165
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
166
|
+
if (!st)
|
167
|
+
rb_raise (rb_eException, "No Splaytreemap Object");
|
168
|
+
|
169
|
+
for ( q_iterator = (*st).begin(); q_iterator != (*st).end(); q_iterator++ ) {
|
170
|
+
rb_str_concat(s,rb_str_new2(q_iterator->first));
|
171
|
+
rb_str_concat(s,rb_convert_type(q_iterator->second, T_STRING, "String", "to_str"));
|
172
|
+
}
|
173
|
+
|
174
|
+
return rb_str_to_str(s);
|
175
|
+
}
|
176
|
+
|
177
|
+
static VALUE sptm_to_a (VALUE self)
|
178
|
+
{
|
179
|
+
VALUE ary = rb_ary_new();
|
180
|
+
|
181
|
+
rb_splaymap* st = NULL;
|
182
|
+
rb_splaymap_iterator q_iterator;
|
183
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
184
|
+
if (!st)
|
185
|
+
rb_raise (rb_eException, "No Splaytreemap Object");
|
186
|
+
|
187
|
+
for ( q_iterator = (*st).begin(); q_iterator != (*st).end(); q_iterator++ ) {
|
188
|
+
rb_ary_push(ary,rb_str_new2(q_iterator->first));
|
189
|
+
rb_ary_push(ary,q_iterator->second);
|
190
|
+
}
|
191
|
+
|
192
|
+
return ary;
|
193
|
+
}
|
194
|
+
|
195
|
+
static VALUE sptm_inspect (VALUE self)
|
196
|
+
{
|
197
|
+
VALUE s = rb_str_new2("{");
|
198
|
+
VALUE arrow = rb_str_new2("=>");
|
199
|
+
VALUE comma = rb_str_new2(",");
|
200
|
+
|
201
|
+
rb_splaymap* st = NULL;
|
202
|
+
rb_splaymap_iterator q_iterator;
|
203
|
+
rb_splaymap_iterator last_q_iterator;
|
204
|
+
rb_splaymap_iterator before_last_q_iterator;
|
205
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
206
|
+
if (!st)
|
207
|
+
rb_raise (rb_eException, "No Splaytreemap Object");
|
208
|
+
|
209
|
+
before_last_q_iterator = last_q_iterator = (*st).end();
|
210
|
+
before_last_q_iterator--;
|
211
|
+
|
212
|
+
for ( q_iterator = (*st).begin(); q_iterator != last_q_iterator; q_iterator++ ) {
|
213
|
+
rb_str_concat(s,rb_inspect(rb_str_new2(q_iterator->first)));
|
214
|
+
rb_str_concat(s,arrow);
|
215
|
+
rb_str_concat(s,rb_inspect(q_iterator->second));
|
216
|
+
if (q_iterator != before_last_q_iterator)
|
217
|
+
rb_str_concat(s,comma);
|
218
|
+
}
|
219
|
+
rb_str_concat(s,rb_str_new2("}"));
|
220
|
+
|
221
|
+
return rb_str_to_str(s);
|
222
|
+
}
|
223
|
+
|
224
|
+
static VALUE sptm_first (VALUE self)
|
225
|
+
{
|
226
|
+
rb_splaymap* st = NULL;
|
227
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
228
|
+
if (!st)
|
229
|
+
rb_raise (rb_eException, "No Splaytreemap Object");
|
230
|
+
|
231
|
+
if ((*st).empty()) {
|
232
|
+
return Qnil;
|
233
|
+
} else {
|
234
|
+
rb_splaymap_iterator front = (*st).begin();
|
235
|
+
VALUE r = rb_ary_new();
|
236
|
+
rb_ary_push(r,rb_str_new2(front->first));
|
237
|
+
rb_ary_push(r,front->second);
|
238
|
+
return r;
|
239
|
+
}
|
240
|
+
}
|
241
|
+
|
242
|
+
static VALUE sptm_last (VALUE self)
|
243
|
+
{
|
244
|
+
rb_splaymap* st = NULL;
|
245
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
246
|
+
if (!st)
|
247
|
+
rb_raise (rb_eException, "No Splaytreemap Object");
|
248
|
+
|
249
|
+
if ((*st).empty()) {
|
250
|
+
return Qnil;
|
251
|
+
} else {
|
252
|
+
rb_splaymap_iterator back = (*st).end();
|
253
|
+
back--;
|
254
|
+
VALUE r = rb_ary_new();
|
255
|
+
rb_ary_push(r,rb_str_new2(back->first));
|
256
|
+
rb_ary_push(r,back->second);
|
257
|
+
return r;
|
258
|
+
}
|
259
|
+
}
|
260
|
+
|
261
|
+
static VALUE sptm_replace (VALUE self, VALUE new_st)
|
262
|
+
{
|
263
|
+
rb_splaymap* st = NULL;
|
264
|
+
rb_splaymap* nst = NULL;
|
265
|
+
rb_splaymap_iterator q_iterator;
|
266
|
+
rb_splaymap_iterator last_q_iterator;
|
267
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
268
|
+
Data_Get_Struct(new_st, rb_splaymap, nst);
|
269
|
+
if (!st)
|
270
|
+
rb_raise (rb_eException, "No Splaytreemap Object");
|
271
|
+
|
272
|
+
if (!nst)
|
273
|
+
rb_raise (rb_eException, "No Splaytreemap object to copy");
|
274
|
+
|
275
|
+
(*st).clear();
|
276
|
+
last_q_iterator = (*st).end();
|
277
|
+
for ( q_iterator = (*nst).begin(); q_iterator != last_q_iterator; q_iterator++ ) {
|
278
|
+
(*st).insert(*q_iterator);
|
279
|
+
}
|
280
|
+
|
281
|
+
return self;
|
282
|
+
}
|
283
|
+
|
284
|
+
static VALUE sptm_insert (VALUE self, VALUE key, VALUE val)
|
285
|
+
{
|
286
|
+
rb_splaymap* st = NULL;
|
287
|
+
std::pair<rb_splaymap_iterator, bool> rv;
|
288
|
+
std::pair<char *,VALUE> sp;
|
289
|
+
char * skey;
|
290
|
+
char * svp;
|
291
|
+
|
292
|
+
svp = StringValuePtr(key);
|
293
|
+
skey = new char[strlen(svp)+1];
|
294
|
+
strcpy(skey,svp);
|
295
|
+
|
296
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
297
|
+
|
298
|
+
if (!st)
|
299
|
+
rb_raise (rb_eException, "No Splaytreemap Object");
|
300
|
+
|
301
|
+
sp = make_pair(skey,val);
|
302
|
+
rv = (*st).insert(sp);
|
303
|
+
if (!rv.second) {
|
304
|
+
(*st).erase(rv.first);
|
305
|
+
(*st).insert(sp);
|
306
|
+
}
|
307
|
+
|
308
|
+
return self;
|
309
|
+
}
|
310
|
+
|
311
|
+
static VALUE sptm_at (VALUE self, VALUE key)
|
312
|
+
{
|
313
|
+
rb_splaymap* st = NULL;
|
314
|
+
rb_splaymap_iterator q_iterator;
|
315
|
+
|
316
|
+
char * skey = StringValuePtr(key);
|
317
|
+
|
318
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
319
|
+
|
320
|
+
if (!st)
|
321
|
+
rb_raise (rb_eException, "No Splaytreemap Object");
|
322
|
+
|
323
|
+
q_iterator = (*st).find(skey);
|
324
|
+
if (q_iterator == (*st).end()) {
|
325
|
+
return Qnil;
|
326
|
+
} else {
|
327
|
+
return q_iterator->second;
|
328
|
+
}
|
329
|
+
}
|
330
|
+
|
331
|
+
static VALUE sptm_has_key (VALUE self, VALUE key)
|
332
|
+
{
|
333
|
+
rb_splaymap* st = NULL;
|
334
|
+
rb_splaymap_iterator q_iterator;
|
335
|
+
|
336
|
+
char * skey = StringValuePtr(key);
|
337
|
+
|
338
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
339
|
+
|
340
|
+
if (!st)
|
341
|
+
rb_raise (rb_eException, "No Splaytreemap Object");
|
342
|
+
|
343
|
+
q_iterator = (*st).find(skey);
|
344
|
+
if (q_iterator == (*st).end()) {
|
345
|
+
return Qnil;
|
346
|
+
} else {
|
347
|
+
return key;
|
348
|
+
}
|
349
|
+
}
|
350
|
+
|
351
|
+
static VALUE sptm_delete (VALUE self, VALUE key)
|
352
|
+
{
|
353
|
+
rb_splaymap* st = NULL;
|
354
|
+
rb_splaymap_iterator q_iterator;
|
355
|
+
|
356
|
+
char * skey = StringValuePtr(key);
|
357
|
+
|
358
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
359
|
+
|
360
|
+
if (!st)
|
361
|
+
rb_raise (rb_eException, "No Splaytreemap Object");
|
362
|
+
|
363
|
+
q_iterator = (*st).find(skey);
|
364
|
+
if (q_iterator == (*st).end()) {
|
365
|
+
return Qnil;
|
366
|
+
} else {
|
367
|
+
(*st).erase(q_iterator);
|
368
|
+
}
|
369
|
+
|
370
|
+
return self;
|
371
|
+
}
|
372
|
+
|
373
|
+
/*
|
374
|
+
static VALUE sptm_delete_value (VALUE self, VALUE obj)
|
375
|
+
{
|
376
|
+
rb_splaymap* st = NULL;
|
377
|
+
rb_splaymap_iterator q_iterator;
|
378
|
+
rb_splaymap_iterator last_q_iterator;
|
379
|
+
|
380
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
381
|
+
|
382
|
+
if (!st)
|
383
|
+
rb_raise (rb_eException, "No Splaytreemap Object");
|
384
|
+
|
385
|
+
last_q_iterator = (*st).end();
|
386
|
+
for ( q_iterator = (*st).begin(); q_iterator != last_q_iterator; q_iterator++ ) {
|
387
|
+
if (rb_equal(q_iterator->second, obj)) {
|
388
|
+
(*st).erase(q_iterator);
|
389
|
+
break;
|
390
|
+
}
|
391
|
+
}
|
392
|
+
|
393
|
+
return self;
|
394
|
+
}
|
395
|
+
*/
|
396
|
+
|
397
|
+
static VALUE sptm_each (VALUE self)
|
398
|
+
{
|
399
|
+
rb_splaymap* st = NULL;
|
400
|
+
rb_splaymap_iterator q_iterator;
|
401
|
+
rb_splaymap_iterator last_q_iterator;
|
402
|
+
|
403
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
404
|
+
|
405
|
+
if (!st)
|
406
|
+
rb_raise (rb_eException, "No Splaytreemap Object");
|
407
|
+
|
408
|
+
last_q_iterator = (*st).end();
|
409
|
+
for ( q_iterator = (*st).begin(); q_iterator != last_q_iterator; q_iterator++ ) {
|
410
|
+
rb_yield_values(2,rb_str_new2(q_iterator->first),q_iterator->second);
|
411
|
+
}
|
412
|
+
|
413
|
+
return self;
|
414
|
+
}
|
415
|
+
|
416
|
+
static VALUE sptm_index (VALUE self, VALUE match_obj)
|
417
|
+
{
|
418
|
+
rb_splaymap* st = NULL;
|
419
|
+
rb_splaymap_iterator q_iterator;
|
420
|
+
rb_splaymap_iterator last_q_iterator;
|
421
|
+
|
422
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
423
|
+
|
424
|
+
if (!st)
|
425
|
+
rb_raise (rb_eException, "No Splaytreemap Object");
|
426
|
+
|
427
|
+
last_q_iterator = (*st).end();
|
428
|
+
for ( q_iterator = (*st).begin(); q_iterator != last_q_iterator; q_iterator++ ) {
|
429
|
+
if (rb_equal(q_iterator->second, match_obj)) {
|
430
|
+
return rb_str_new2(q_iterator->first);
|
431
|
+
}
|
432
|
+
}
|
433
|
+
return Qnil;
|
434
|
+
}
|
435
|
+
|
436
|
+
static VALUE sptm_keys (VALUE self)
|
437
|
+
{
|
438
|
+
rb_splaymap* st = NULL;
|
439
|
+
rb_splaymap_iterator q_iterator;
|
440
|
+
rb_splaymap_iterator last_q_iterator;
|
441
|
+
|
442
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
443
|
+
|
444
|
+
if (!st)
|
445
|
+
rb_raise (rb_eException, "No Splaytreemap Object");
|
446
|
+
|
447
|
+
last_q_iterator = (*st).end();
|
448
|
+
VALUE r = rb_ary_new();
|
449
|
+
for ( q_iterator = (*st).begin(); q_iterator != last_q_iterator; q_iterator++ ) {
|
450
|
+
rb_ary_push(r,rb_str_new2(q_iterator->first));
|
451
|
+
}
|
452
|
+
|
453
|
+
return r;
|
454
|
+
}
|
455
|
+
|
456
|
+
static VALUE sptm_values (VALUE self)
|
457
|
+
{
|
458
|
+
rb_splaymap* st = NULL;
|
459
|
+
rb_splaymap_iterator q_iterator;
|
460
|
+
rb_splaymap_iterator last_q_iterator;
|
461
|
+
|
462
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
463
|
+
|
464
|
+
if (!st)
|
465
|
+
rb_raise (rb_eException, "No Splaytreemap Object");
|
466
|
+
|
467
|
+
last_q_iterator = (*st).end();
|
468
|
+
VALUE r = rb_ary_new();
|
469
|
+
for ( q_iterator = (*st).begin(); q_iterator != last_q_iterator; q_iterator++ ) {
|
470
|
+
rb_ary_push(r,q_iterator->second);
|
471
|
+
}
|
472
|
+
|
473
|
+
return r;
|
474
|
+
}
|
475
|
+
|
476
|
+
static VALUE sptm_erase_childfree(VALUE self)
|
477
|
+
{
|
478
|
+
rb_splaymap* st = NULL;
|
479
|
+
|
480
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
481
|
+
|
482
|
+
if (!st)
|
483
|
+
rb_raise (rb_eException, "No Splaytreemap Object");
|
484
|
+
|
485
|
+
(*st).erase_childfree_nodes();
|
486
|
+
|
487
|
+
return Qnil;
|
488
|
+
}
|
489
|
+
|
490
|
+
static VALUE sptm_get_max_size(VALUE self) {
|
491
|
+
rb_splaymap* st = NULL;
|
492
|
+
|
493
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
494
|
+
|
495
|
+
if (!st)
|
496
|
+
rb_raise (rb_eException, "No Splaytreemap Object");
|
497
|
+
|
498
|
+
return INT2FIX((*st).get_max_permitted_size());
|
499
|
+
}
|
500
|
+
|
501
|
+
static VALUE sptm_set_max_size(VALUE self, VALUE sz) {
|
502
|
+
int n = FIX2INT(sz);
|
503
|
+
rb_splaymap* st = NULL;
|
504
|
+
|
505
|
+
Data_Get_Struct(self, rb_splaymap, st);
|
506
|
+
|
507
|
+
if (n < 0)
|
508
|
+
rb_raise (rb_eException, "Size can not be less than zero.");
|
509
|
+
|
510
|
+
if (!st)
|
511
|
+
rb_raise (rb_eException, "No Splaytreemap Object");
|
512
|
+
|
513
|
+
(*st).set_max_permitted_size(n);
|
514
|
+
|
515
|
+
while ((*st).size() > n) {
|
516
|
+
(*st).erase_childfree_nodes();
|
517
|
+
}
|
518
|
+
|
519
|
+
return sz;
|
520
|
+
}
|
521
|
+
|
522
|
+
/**********************
|
523
|
+
Init_sptm
|
524
|
+
**********************/
|
525
|
+
|
526
|
+
extern "C" void Init_splaytreemap()
|
527
|
+
{
|
528
|
+
SplayTreeMapClass = rb_define_class("Splaytreemap", rb_cObject);
|
529
|
+
|
530
|
+
rb_define_alloc_func (SplayTreeMapClass, sptm_alloc);
|
531
|
+
rb_define_method (SplayTreeMapClass, "initialize", (VALUE(*)(...))sptm_initialize,0);
|
532
|
+
// ==
|
533
|
+
rb_define_method (SplayTreeMapClass, "[]", (VALUE(*)(...))sptm_at,1);
|
534
|
+
rb_define_method (SplayTreeMapClass, "[]=", (VALUE(*)(...))sptm_insert,2);
|
535
|
+
rb_define_method (SplayTreeMapClass, "at", (VALUE(*)(...))sptm_at,1);
|
536
|
+
rb_define_method (SplayTreeMapClass, "clear", (VALUE(*)(...))sptm_clear,0);
|
537
|
+
rb_define_method (SplayTreeMapClass, "clear_childfree", (VALUE(*)(...))sptm_erase_childfree,0);
|
538
|
+
rb_define_method (SplayTreeMapClass, "delete", (VALUE(*)(...))sptm_delete,1);
|
539
|
+
// delete_if
|
540
|
+
// delete_values
|
541
|
+
rb_define_method (SplayTreeMapClass, "each", (VALUE(*)(...))sptm_each,0);
|
542
|
+
// each_key
|
543
|
+
rb_define_method (SplayTreeMapClass, "each_pair", (VALUE(*)(...))sptm_each,0);
|
544
|
+
// each_value
|
545
|
+
rb_define_method (SplayTreeMapClass, "empty?", (VALUE(*)(...))sptm_empty,0);
|
546
|
+
rb_define_method (SplayTreeMapClass, "fetch", (VALUE(*)(...))sptm_at,1);
|
547
|
+
rb_define_method (SplayTreeMapClass, "first", (VALUE(*)(...))sptm_first,0);
|
548
|
+
rb_define_method (SplayTreeMapClass, "has_key?", (VALUE(*)(...))sptm_has_key,1);
|
549
|
+
// has_value?
|
550
|
+
rb_define_method (SplayTreeMapClass, "include?", (VALUE(*)(...))sptm_has_key,1);
|
551
|
+
rb_define_method (SplayTreeMapClass, "index", (VALUE(*)(...))sptm_index,1);
|
552
|
+
// indices
|
553
|
+
rb_define_method (SplayTreeMapClass, "insert", (VALUE(*)(...))sptm_insert,2);
|
554
|
+
rb_define_method (SplayTreeMapClass, "inspect", (VALUE(*)(...))sptm_inspect,0);
|
555
|
+
// invert
|
556
|
+
rb_define_method (SplayTreeMapClass, "key?", (VALUE(*)(...))sptm_has_key,1);
|
557
|
+
rb_define_method (SplayTreeMapClass, "keys", (VALUE(*)(...))sptm_keys,0);
|
558
|
+
rb_define_method (SplayTreeMapClass, "last", (VALUE(*)(...))sptm_last,0);
|
559
|
+
rb_define_method (SplayTreeMapClass, "length", (VALUE(*)(...))sptm_size,0);
|
560
|
+
rb_define_method (SplayTreeMapClass, "max_capacity", (VALUE(*)(...))sptm_max_capacity,0);
|
561
|
+
rb_define_method (SplayTreeMapClass, "max_size", (VALUE(*)(...))sptm_get_max_size,0);
|
562
|
+
rb_define_method (SplayTreeMapClass, "max_size=", (VALUE(*)(...))sptm_set_max_size,1);
|
563
|
+
rb_define_method (SplayTreeMapClass, "member?", (VALUE(*)(...))sptm_has_key,1);
|
564
|
+
rb_define_method (SplayTreeMapClass, "parent", (VALUE(*)(...))sptm_parent,0);
|
565
|
+
rb_define_method (SplayTreeMapClass, "pop", (VALUE(*)(...))sptm_pop_back,0);
|
566
|
+
rb_define_method (SplayTreeMapClass, "replace", (VALUE(*)(...))sptm_replace,1);
|
567
|
+
rb_define_method (SplayTreeMapClass, "shift", (VALUE(*)(...))sptm_pop_front,0);
|
568
|
+
rb_define_method (SplayTreeMapClass, "size", (VALUE(*)(...))sptm_size,0);
|
569
|
+
rb_define_method (SplayTreeMapClass, "to_a", (VALUE(*)(...))sptm_to_a,0);
|
570
|
+
rb_define_method (SplayTreeMapClass, "to_s", (VALUE(*)(...))sptm_to_s,0);
|
571
|
+
rb_define_method (SplayTreeMapClass, "values", (VALUE(*)(...))sptm_values,0);
|
572
|
+
// values_at
|
573
|
+
|
574
|
+
rb_include_module (SplayTreeMapClass, rb_mEnumerable);
|
575
|
+
rb_require("splaytreemap/version.rb");
|
576
|
+
}
|