rbtree 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. data/ChangeLog +22 -0
  2. data/LICENSE +1 -1
  3. data/README +3 -3
  4. data/extconf.rb +3 -8
  5. data/rbtree.c +59 -23
  6. data/test.rb +41 -12
  7. metadata +2 -2
data/ChangeLog CHANGED
@@ -1,3 +1,25 @@
1
+ 2009-11-14 OZAWA Takuma
2
+
3
+ * version 0.3.0 released.
4
+
5
+ 2009-11-10 OZAWA Takuma
6
+
7
+ * rbtree.c: applied patches for Ruby 1.9 by Florian Gilcher and
8
+ Nobu Nakada. Thanks.
9
+ http://redmine.ruby-lang.org/issues/show/2348
10
+
11
+ * test.rb: ditto.
12
+
13
+ * extconf.rb: ditto.
14
+
15
+ * extconf.rb: remove options for only gcc.
16
+
17
+ * test.rb (test_inspect): correct recursion tests.
18
+
19
+ * rbtree.c (rbtree_to_s_recursive): should return if recursive.
20
+
21
+ * test.rb (test_to_s): add recursion tests.
22
+
1
23
  2009-01-27 OZAWA Takuma
2
24
 
3
25
  * version 0.2.1 released.
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2002-2004, 2007, 2009 OZAWA Takuma
1
+ Copyright (c) 2002-2004, 2007, 2009-2010 OZAWA Takuma
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person
4
4
  obtaining a copy of this software and associated documentation
data/README CHANGED
@@ -55,7 +55,7 @@ duplications of keys but MultiRBTree does.
55
55
 
56
56
  == Requirement
57
57
 
58
- * Ruby 1.8.x
58
+ * Ruby 1.8.x, 1.9.1
59
59
 
60
60
  == Install
61
61
 
@@ -63,7 +63,7 @@ duplications of keys but MultiRBTree does.
63
63
 
64
64
  or download a tarball from the link below
65
65
 
66
- * ((<"Ruby/RBTree 0.2.1"|URL:rbtree-0.2.1.tar.gz>))
66
+ * ((<"Ruby/RBTree 0.3.0"|URL:rbtree-0.3.0.tar.gz>))
67
67
 
68
68
  and then
69
69
 
@@ -85,7 +85,7 @@ or online documents at ((<URL:http://rbtree.rubyforge.org/>)).
85
85
 
86
86
  == License
87
87
 
88
- MIT License. Copyright (c) 2002-2004, 2007, 2009 OZAWA Takuma.
88
+ MIT License. Copyright (c) 2002-2004, 2007, 2009-2010 OZAWA Takuma.
89
89
 
90
90
  dict.c and dict.h are modified copies that are originally in Kazlib
91
91
  written by Kaz Kylheku. Copyright is held by Kaz Kylheku, see dict.c
data/extconf.rb CHANGED
@@ -1,11 +1,6 @@
1
1
  require 'mkmf'
2
2
 
3
- if $DEBUG
4
- $CFLAGS << ' -std=c89 -pedantic -Wall -Wno-long-long'
5
- $defs << ' -Dinline=__inline'
6
- else
7
- $defs << '-DNDEBUG'
8
- end
9
-
10
- have_func('rb_enumeratorize')
3
+ $defs << '-DNDEBUG'
4
+ have_header('ruby/st.h')
5
+ have_func('rb_exec_recursive', 'ruby.h')
11
6
  create_makefile('rbtree')
data/rbtree.c CHANGED
@@ -1,20 +1,30 @@
1
1
  /*
2
2
  * MIT License
3
- * Copyright (c) 2002-2004, 2007, 2009 OZAWA Takuma
3
+ * Copyright (c) 2002-2004, 2007, 2009-2010 OZAWA Takuma
4
4
  */
5
5
  #include <ruby.h>
6
- #include <version.h>
6
+ #ifdef HAVE_RUBY_ST_H
7
+ #include <ruby/st.h>
8
+ #else
7
9
  #include <st.h>
10
+ #endif
8
11
  #include <stdarg.h>
9
12
  #include "dict.h"
10
13
 
11
14
  #define RBTREE_PROC_DEFAULT FL_USER2
12
15
  #define HASH_PROC_DEFAULT FL_USER2
13
16
 
14
- #ifndef HAVE_RB_ENUMERATORIZE
17
+ #ifndef RETURN_ENUMERATOR
15
18
  #define RETURN_ENUMERATOR(obj, argc, argv) ((void)0)
16
19
  #endif
17
20
 
21
+ #ifndef RHASH_TBL
22
+ #define RHASH_TBL(h) RHASH(h)->tbl
23
+ #endif
24
+ #ifndef RHASH_IFNONE
25
+ #define RHASH_IFNONE(h) RHASH(h)->ifnone
26
+ #endif
27
+
18
28
  VALUE RBTree;
19
29
  VALUE MultiRBTree;
20
30
 
@@ -184,24 +194,24 @@ rbtree_s_create(int argc, VALUE* argv, VALUE klass)
184
194
  tmp = rb_check_convert_type(argv[0], T_HASH, "Hash", "to_hash");
185
195
  if (!NIL_P(tmp)) {
186
196
  rbtree = rbtree_alloc(klass);
187
- st_foreach(RHASH(tmp)->tbl, hash_to_rbtree_i, rbtree);
197
+ st_foreach(RHASH_TBL(tmp), hash_to_rbtree_i, rbtree);
188
198
  return rbtree;
189
199
  }
190
200
 
191
201
  tmp = rb_check_array_type(argv[0]);
192
202
  if (!NIL_P(tmp)) {
193
203
  rbtree = rbtree_alloc(klass);
194
- for (i = 0; i < RARRAY(tmp)->len; i++) {
195
- VALUE v = rb_check_array_type(RARRAY(tmp)->ptr[i]);
204
+ for (i = 0; i < RARRAY_LEN(tmp); i++) {
205
+ VALUE v = rb_check_array_type(RARRAY_PTR(tmp)[i]);
196
206
  if (NIL_P(v)) {
197
207
  continue;
198
208
  }
199
- switch(RARRAY(v)->len) {
209
+ switch(RARRAY_LEN(v)) {
200
210
  case 1:
201
- rbtree_aset(rbtree, RARRAY(v)->ptr[0], Qnil);
211
+ rbtree_aset(rbtree, RARRAY_PTR(v)[0], Qnil);
202
212
  break;
203
213
  case 2:
204
- rbtree_aset(rbtree, RARRAY(v)->ptr[0], RARRAY(v)->ptr[1]);
214
+ rbtree_aset(rbtree, RARRAY_PTR(v)[0], RARRAY_PTR(v)[1]);
205
215
  break;
206
216
  default:
207
217
  continue;
@@ -351,7 +361,7 @@ rbtree_fetch(int argc, VALUE* argv, VALUE self)
351
361
  rbtree_argc_error();
352
362
  block_given = rb_block_given_p();
353
363
  if (block_given && argc == 2)
354
- rb_warn("block supersedes default value argument");
364
+ rb_warn("block supersedes default value argument");
355
365
 
356
366
  node = dict_lookup(DICT(self), TO_KEY(argv[0]));
357
367
  if (node != NULL)
@@ -427,7 +437,7 @@ rbtree_default_proc(VALUE self)
427
437
  static int
428
438
  value_eq(const void* key1, const void* key2)
429
439
  {
430
- return rb_equal((VALUE)key1, (VALUE)key2);
440
+ return rb_equal((VALUE)key1, (VALUE)key2) != 0;
431
441
  }
432
442
 
433
443
  /*
@@ -1059,7 +1069,7 @@ rbtree_to_a(VALUE self)
1059
1069
  static each_return_t
1060
1070
  to_hash_i(dnode_t* node, void* hash)
1061
1071
  {
1062
- st_insert(RHASH(hash)->tbl, GET_KEY(node), GET_VAL(node));
1072
+ st_insert(RHASH_TBL((VALUE)hash), GET_KEY(node), GET_VAL(node));
1063
1073
  return EACH_NEXT;
1064
1074
  }
1065
1075
 
@@ -1076,7 +1086,7 @@ rbtree_to_hash(VALUE self)
1076
1086
 
1077
1087
  hash = rb_hash_new();
1078
1088
  rbtree_for_each(self, to_hash_i, (void*)hash);
1079
- RHASH(hash)->ifnone = IFNONE(self);
1089
+ RHASH_IFNONE(hash) = IFNONE(self);
1080
1090
  if (FL_TEST(self, RBTREE_PROC_DEFAULT))
1081
1091
  FL_SET(hash, HASH_PROC_DEFAULT);
1082
1092
  OBJ_INFECT(hash, self);
@@ -1096,9 +1106,8 @@ static VALUE
1096
1106
  rbtree_begin_inspect(VALUE self)
1097
1107
  {
1098
1108
  const char* c = rb_class2name(CLASS_OF(self));
1099
- VALUE str = rb_str_new(0, strlen(c) + 5);
1100
- const size_t len = sprintf(RSTRING(str)->ptr, "#<%s: ", c);
1101
- RSTRING(str)->len = len;
1109
+ VALUE str = rb_str_new(0, strlen(c) + 4);
1110
+ sprintf(RSTRING_PTR(str), "#<%s: ", c);
1102
1111
  return str;
1103
1112
  }
1104
1113
 
@@ -1108,15 +1117,29 @@ to_s_rbtree(VALUE self, VALUE nil)
1108
1117
  return rb_ary_to_s(rbtree_to_a(self));
1109
1118
  }
1110
1119
 
1120
+ #ifdef HAVE_RB_EXEC_RECURSIVE
1121
+ static VALUE
1122
+ rbtree_to_s_recursive(VALUE self, VALUE arg, int recursive)
1123
+ {
1124
+ if (recursive)
1125
+ return rb_str_cat2(rbtree_begin_inspect(self), "...>");
1126
+ return to_s_rbtree(self, Qnil);
1127
+ }
1128
+ #endif
1129
+
1111
1130
  /*
1112
1131
  *
1113
1132
  */
1114
1133
  VALUE
1115
1134
  rbtree_to_s(VALUE self)
1116
1135
  {
1136
+ #ifdef HAVE_RB_EXEC_RECURSIVE
1137
+ return rb_exec_recursive(rbtree_to_s_recursive, self, Qnil);
1138
+ #else
1117
1139
  if (rb_inspecting_p(self))
1118
1140
  return rb_str_cat2(rbtree_begin_inspect(self), "...>");
1119
1141
  return rb_protect_inspect(to_s_rbtree, self, Qnil);
1142
+ #endif
1120
1143
  }
1121
1144
 
1122
1145
  static each_return_t
@@ -1125,8 +1148,8 @@ inspect_i(dnode_t* node, void* ret_)
1125
1148
  VALUE ret = (VALUE)ret_;
1126
1149
  VALUE str;
1127
1150
 
1128
- if (RSTRING(ret)->ptr[0] == '-')
1129
- RSTRING(ret)->ptr[0] = '#';
1151
+ if (RSTRING_PTR(ret)[0] == '-')
1152
+ RSTRING_PTR(ret)[0] = '#';
1130
1153
  else
1131
1154
  rb_str_cat2(ret, ", ");
1132
1155
 
@@ -1149,9 +1172,9 @@ inspect_rbtree(VALUE self, VALUE ret)
1149
1172
  VALUE str;
1150
1173
 
1151
1174
  rb_str_cat2(ret, "{");
1152
- RSTRING(ret)->ptr[0] = '-';
1175
+ RSTRING_PTR(ret)[0] = '-';
1153
1176
  rbtree_for_each(self, inspect_i, (void*)ret);
1154
- RSTRING(ret)->ptr[0] = '#';
1177
+ RSTRING_PTR(ret)[0] = '#';
1155
1178
  rb_str_cat2(ret, "}");
1156
1179
 
1157
1180
  str = rb_inspect(IFNONE(self));
@@ -1169,16 +1192,29 @@ inspect_rbtree(VALUE self, VALUE ret)
1169
1192
  return ret;
1170
1193
  }
1171
1194
 
1195
+ static VALUE
1196
+ rbtree_inspect_recursive(VALUE self, VALUE arg, int recursive)
1197
+ {
1198
+ VALUE str = rbtree_begin_inspect(self);
1199
+ if (recursive)
1200
+ return rb_str_cat2(str, "...>");
1201
+ return inspect_rbtree(self, str);
1202
+ }
1203
+
1172
1204
  /*
1173
1205
  *
1174
1206
  */
1175
1207
  VALUE
1176
1208
  rbtree_inspect(VALUE self)
1177
1209
  {
1210
+ #ifdef HAVE_RB_EXEC_RECURSIVE
1211
+ return rb_exec_recursive(rbtree_inspect_recursive, self, Qnil);
1212
+ #else
1178
1213
  VALUE str = rbtree_begin_inspect(self);
1179
1214
  if (rb_inspecting_p(self))
1180
1215
  return rb_str_cat2(str, "...>");
1181
1216
  return rb_protect_inspect(inspect_rbtree, self, str);
1217
+ #endif
1182
1218
  }
1183
1219
 
1184
1220
  /*
@@ -1463,7 +1499,7 @@ rbtree_pretty_print(VALUE self, VALUE pp)
1463
1499
  VALUE
1464
1500
  rbtree_pretty_print_cycle(VALUE self, VALUE pp)
1465
1501
  {
1466
- return rb_funcall(pp, id_pp, 1, rbtree_inspect(self));
1502
+ return rb_funcall(pp, id_pp, 1, rbtree_inspect_recursive(self, Qnil, 1));
1467
1503
  }
1468
1504
 
1469
1505
  /*********************************************************************/
@@ -1510,8 +1546,8 @@ rbtree_s_load(VALUE klass, VALUE str)
1510
1546
  {
1511
1547
  VALUE rbtree = rbtree_alloc(klass);
1512
1548
  VALUE ary = rb_marshal_load(str);
1513
- VALUE* ptr = RARRAY(ary)->ptr;
1514
- long len = RARRAY(ary)->len - 1;
1549
+ VALUE* ptr = RARRAY_PTR(ary);
1550
+ long len = RARRAY_LEN(ary) - 1;
1515
1551
  long i;
1516
1552
 
1517
1553
  for (i = 0; i < len; i += 2)
data/test.rb CHANGED
@@ -1,4 +1,4 @@
1
- require "rbtree"
1
+ require "./rbtree"
2
2
  require "test/unit.rb"
3
3
 
4
4
  class RBTreeTest < Test::Unit::TestCase
@@ -135,7 +135,7 @@ class RBTreeTest < Test::Unit::TestCase
135
135
  assert_equal("e", rbtree.default("f"))
136
136
  assert_raises(ArgumentError) { rbtree.default("e", "f") }
137
137
 
138
- rbtree = RBTree.new {|rbtree, key| @rbtree[key || "c"] }
138
+ rbtree = RBTree.new {|tree, key| @rbtree[key || "c"] }
139
139
  assert_equal("C", rbtree.default(nil))
140
140
  assert_equal("B", rbtree.default("b"))
141
141
  end
@@ -181,7 +181,7 @@ class RBTreeTest < Test::Unit::TestCase
181
181
 
182
182
  a = RBTree.new
183
183
  b = RBTree.new
184
- a.readjust {|a, b| a <=> b }
184
+ a.readjust {|x, y| x <=> y }
185
185
  assert_not_equal(a, b)
186
186
  b.readjust(a.cmp_proc)
187
187
  assert_equal(a, b)
@@ -197,7 +197,17 @@ class RBTreeTest < Test::Unit::TestCase
197
197
 
198
198
  assert_equal("E", @rbtree.fetch("e", "E"))
199
199
  assert_equal("E", @rbtree.fetch("e") { "E" })
200
- assert_equal("E", @rbtree.fetch("e", "F") { "E" })
200
+
201
+ class << (stderr = "")
202
+ alias write <<
203
+ end
204
+ $stderr, stderr, $VERBOSE, verbose = stderr, $stderr, false, $VERBOSE
205
+ begin
206
+ assert_equal("E", @rbtree.fetch("e", "F") { "E" })
207
+ ensure
208
+ $stderr, stderr, $VERBOSE, verbose = stderr, $stderr, false, $VERBOSE
209
+ end
210
+ assert_match(/warning: block supersedes default value argument/, stderr)
201
211
 
202
212
  assert_raises(ArgumentError) { @rbtree.fetch }
203
213
  assert_raises(ArgumentError) { @rbtree.fetch("e", "E", "E") }
@@ -497,7 +507,18 @@ class RBTreeTest < Test::Unit::TestCase
497
507
  end
498
508
 
499
509
  def test_to_s
500
- assert_equal("aAbBcCdD", @rbtree.to_s)
510
+ if RUBY_VERSION < "1.9"
511
+ assert_equal("aAbBcCdD", @rbtree.to_s)
512
+ else
513
+ expected = "[[\"a\", \"A\"], [\"b\", \"B\"], [\"c\", \"C\"], [\"d\", \"D\"]]"
514
+ assert_equal(expected, @rbtree.to_s)
515
+
516
+ rbtree = RBTree.new
517
+ rbtree[rbtree] = rbtree
518
+ rbtree.default = rbtree
519
+ expected = "[[#<RBTree: {#<RBTree: ...>=>#<RBTree: ...>}, default=#<RBTree: ...>, cmp_proc=nil>, #<RBTree: {#<RBTree: ...>=>#<RBTree: ...>}, default=#<RBTree: ...>, cmp_proc=nil>]]"
520
+ assert_equal(expected, rbtree.to_s)
521
+ end
501
522
  end
502
523
 
503
524
  def test_to_hash
@@ -529,7 +550,7 @@ class RBTreeTest < Test::Unit::TestCase
529
550
  tree, default, cmp_proc = match.to_a[1..-1]
530
551
  assert_equal(%({"a"=>"A", "b"=>"B", "c"=>"C", "d"=>"D"}), tree)
531
552
  assert_equal(%("e"), default)
532
- assert_match(/#<Proc:\w+(@test.rb:\d+)?>/, cmp_proc)
553
+ assert_match(/#<Proc:\w+(@#{__FILE__}:\d+)?>/o, cmp_proc)
533
554
 
534
555
  rbtree = RBTree.new
535
556
  assert_match(re, rbtree.inspect)
@@ -540,10 +561,13 @@ class RBTreeTest < Test::Unit::TestCase
540
561
  assert_equal("nil", cmp_proc)
541
562
 
542
563
  rbtree = RBTree.new
543
- rbtree["e"] = rbtree
544
- assert_match(re, rbtree.inspect)
564
+ rbtree[rbtree] = rbtree
565
+ rbtree.default = rbtree
545
566
  match = re.match(rbtree.inspect)
546
- assert_equal(%({"e"=>#<RBTree: ...>}), match[1])
567
+ tree, default, cmp_proc = match.to_a[1..-1]
568
+ assert_equal("{#<RBTree: ...>=>#<RBTree: ...>}", tree)
569
+ assert_equal("#<RBTree: ...>", default)
570
+ assert_equal("nil", cmp_proc)
547
571
  end
548
572
 
549
573
  def test_lower_bound
@@ -702,7 +726,7 @@ class RBTreeTest < Test::Unit::TestCase
702
726
 
703
727
  def test_pp
704
728
  assert_equal(%(#<RBTree: {}, default=nil, cmp_proc=nil>\n),
705
- PP.pp(RBTree[], ""))
729
+ PP.pp(RBTree.new, ""))
706
730
  assert_equal(%(#<RBTree: {"a"=>"A", "b"=>"B"}, default=nil, cmp_proc=nil>\n),
707
731
  PP.pp(RBTree[*%w(a A b B)], ""))
708
732
 
@@ -728,7 +752,7 @@ class RBTreeTest < Test::Unit::TestCase
728
752
  EOS
729
753
  assert_equal(expected, PP.pp(rbtree, ""))
730
754
 
731
- rbtree = RBTree[]
755
+ rbtree = RBTree.new
732
756
  rbtree[rbtree] = rbtree
733
757
  rbtree.default = rbtree
734
758
  expected = <<EOS
@@ -778,7 +802,12 @@ class MultiRBTreeTest < Test::Unit::TestCase
778
802
  end
779
803
 
780
804
  def test_to_s
781
- assert_equal("aAbBbCbDcC", @rbtree.to_s)
805
+ if RUBY_VERSION < "1.9"
806
+ assert_equal("aAbBbCbDcC", @rbtree.to_s)
807
+ else
808
+ expected = "[[\"a\", \"A\"], [\"b\", \"B\"], [\"b\", \"C\"], \[\"b\", \"D\"], [\"c\", \"C\"]]"
809
+ assert_equal(expected, @rbtree.to_s)
810
+ end
782
811
  end
783
812
 
784
813
  def test_to_hash
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbtree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - OZAWA Takuma
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-27 00:00:00 +09:00
12
+ date: 2009-11-14 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies: []
15
15