Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

updating for part two of the tutorial

  • Loading branch information...
commit 31613f998741944c88ccb11a2d63ab8aba3aecc8 1 parent 8589b7c
@tenderlove authored
View
23 Rakefile
@@ -2,22 +2,17 @@
require 'rubygems'
require 'hoe'
-
-HOE = Hoe.spec 'stree' do
- developer('Aaron Patterson', 'aaron@tenderlovemaking.com')
- self.readme_file = 'README.rdoc'
- self.history_file = 'CHANGELOG.rdoc'
- self.extra_rdoc_files = FileList['*.rdoc']
- self.extra_dev_deps << ['rake-compiler', '>= 0']
- self.spec_extras = { :extensions => ["ext/stree/extconf.rb"] }
-end
-
require "rake/extensiontask"
-Rake::ExtensionTask.new(HOE.name, HOE.spec) do |ext|
- ext.lib_dir = File.join('lib', 'stree')
-end
+Hoe.plugin :debugging
+Hoe.plugin :compiler
+Hoe.plugin :git
-Rake::Task[:test].prerequisites << :compile
+Hoe.spec 'stree' do
+ developer('Aaron Patterson', 'aaron@tenderlovemaking.com')
+ self.readme_file = 'README.rdoc'
+ self.history_file = 'CHANGELOG.rdoc'
+ self.extra_rdoc_files = FileList['*.rdoc']
+end
# vim: syntax=ruby
View
11 ext/stree/extconf.rb
@@ -33,18 +33,13 @@
'/usr/lib',
]
-stree_dirs = dir_config('stree', '/opt/local/include', '/opt/local/lib')
+dir_config('stree', HEADER_DIRS, LIB_DIRS)
-unless ["", ""] == stree_dirs
- HEADER_DIRS.unshift stree_dirs.first
- LIB_DIRS.unshift stree_dirs[1]
-end
-
-unless find_header('stree/lst_string.h', *HEADER_DIRS)
+unless find_header('stree/lst_string.h')
abort "libstree is missing. please install libstree"
end
-unless find_library('stree', 'lst_stree_free', *LIB_DIRS)
+unless find_library('stree', 'lst_stree_free')
abort "libstree is missing. please install libstree"
end
View
49 ext/stree/stree.c
@@ -1,53 +1,10 @@
#include <stree.h>
-VALUE cStree;
-
-static void dealloc(void *ptr)
-{
- lst_stree_free((LST_STree *)ptr);
-}
-
-static VALUE allocate(VALUE klass)
-{
- LST_STree * ptr;
- VALUE rb = Data_Make_Struct(klass, LST_STree, NULL, dealloc, ptr);
- lst_stree_init(ptr);
- return rb;
-}
-
-static VALUE push(VALUE self, VALUE o)
-{
- LST_STree * tree;
-
- Data_Get_Struct(self, LST_STree, tree);
-
- LST_String * string = lst_string_new(
- StringValuePtr(o), sizeof(char), RSTRING_LEN(o));
-
- lst_stree_add_string(tree, string);
-
- return self;
-}
-
-static VALUE longest_common_substr(VALUE self, VALUE min_len, VALUE max_len)
-{
- LST_STree * ptr;
-
- Data_Get_Struct(self, LST_STree, ptr);
-
- LST_StringSet * set =
- lst_alg_longest_common_substring(ptr, NUM2INT(min_len), NUM2INT(max_len));
-
- return Stree_Wrap_StringSet(set);
-}
+VALUE mSTree;
void Init_stree()
{
- cStree = rb_define_class("Stree", rb_cObject);
-
- rb_define_alloc_func(cStree, allocate);
- rb_define_method(cStree, "push", push, 1);
- rb_define_method(cStree, "longest_common_substr", longest_common_substr, 2);
+ mSTree = rb_define_module("STree");
- Init_stree_stringset();
+ Init_stree_string();
}
View
6 ext/stree/stree.h
@@ -3,11 +3,9 @@
#include <ruby.h>
#include <stree/lst_string.h>
-#include <stree/lst_stree.h>
-#include <stree/lst_algorithms.h>
-#include <stree_stringset.h>
+#include <stree_string.h>
-extern VALUE cStree;
+extern VALUE mSTree;
#endif
View
52 ext/stree/stree_string.c
@@ -0,0 +1,52 @@
+#include <stree_string.h>
+
+static void deallocate(void * string)
+{
+ lst_string_free((LST_String *)string);
+}
+
+static VALUE allocate(VALUE klass)
+{
+ LST_String * string = malloc(sizeof(LST_String));
+
+ return Data_Wrap_Struct(klass, NULL, deallocate, string);
+}
+
+static VALUE initialize(VALUE self, VALUE rb_string)
+{
+ LST_String * string;
+ void * data;
+
+ Check_Type(rb_string, T_STRING);
+
+ Data_Get_Struct(self, LST_String, string);
+
+ data = calloc(RSTRING_LEN(rb_string), sizeof(char));
+ memcpy(data, StringValuePtr(rb_string), RSTRING_LEN(rb_string));
+
+ lst_string_init(
+ string,
+ data,
+ sizeof(char),
+ RSTRING_LEN(rb_string));
+
+ return self;
+}
+
+static VALUE length(VALUE self)
+{
+ LST_String * string;
+
+ Data_Get_Struct(self, LST_String, string);
+
+ return INT2NUM(lst_string_get_length(string));
+}
+
+void Init_stree_string()
+{
+ VALUE cSTreeString = rb_define_class_under(mSTree, "String", rb_cObject);
+
+ rb_define_alloc_func(cSTreeString, allocate);
+ rb_define_method(cSTreeString, "initialize", initialize, 1);
+ rb_define_method(cSTreeString, "length", length, 0);
+}
View
8 ext/stree/stree_string.h
@@ -0,0 +1,8 @@
+#ifndef RUBY_STREE_STRING
+#define RUBY_STREE_STRING
+
+#include <stree.h>
+
+void Init_stree_string();
+
+#endif
View
89 ext/stree/stree_stringset.c
@@ -1,89 +0,0 @@
-#include <stree_stringset.h>
-
-VALUE cStreeStringSet;
-
-static void dealloc(void *ptr)
-{
- LST_StringSet *set = (LST_StringSet *)ptr;
- lst_stringset_free(set);
-}
-
-static VALUE allocate(VALUE klass)
-{
- LST_StringSet * ss = lst_stringset_new();
- return Data_Wrap_Struct(klass, NULL, dealloc, ss);
-}
-
-VALUE Stree_Wrap_StringSet(LST_StringSet * set)
-{
- VALUE ss = Data_Wrap_Struct(cStreeStringSet, NULL, dealloc, set);
- rb_obj_call_init(ss, 0, NULL);
- return ss;
-}
-
-static VALUE push(VALUE self, VALUE string)
-{
- LST_String * string_p = lst_string_new(
- StringValuePtr(string), sizeof(char), RSTRING_LEN(string));
-
- LST_StringSet * ss;
-
- Data_Get_Struct(self, LST_StringSet, ss);
-
- lst_stringset_add(ss, string_p);
-
- return self;
-}
-
-static VALUE length(VALUE self)
-{
- LST_StringSet * ss;
-
- Data_Get_Struct(self, LST_StringSet, ss);
- return INT2NUM(ss->size);
-}
-
-static VALUE delete(VALUE self, VALUE string)
-{
- LST_StringSet * ss;
- LST_String * ss_string;
-
- Data_Get_Struct(self, LST_StringSet, ss);
-
- for(ss_string = ss->members.lh_first; ss_string; ss_string = ss_string->set.le_next)
- {
- VALUE tmp_string = rb_str_new2(lst_string_print(ss_string));
- if(Qtrue == rb_funcall(string, rb_intern("=="), 1, tmp_string)) {
- LIST_REMOVE(ss_string, set);
- ss->size--;
- return string;
- }
- }
-
- return Qnil;
-}
-
-static void each_cb(LST_String * string, void * ctx)
-{
- rb_yield(rb_str_new2(lst_string_print(string)));
-}
-
-static VALUE each(VALUE self)
-{
- LST_StringSet * ss;
-
- Data_Get_Struct(self, LST_StringSet, ss);
- lst_stringset_foreach(ss, each_cb, (void *)self);
- return self;
-}
-
-void Init_stree_stringset()
-{
- cStreeStringSet = rb_define_class_under(cStree, "StringSet", rb_cObject);
- rb_define_alloc_func(cStreeStringSet, allocate);
-
- rb_define_method(cStreeStringSet, "push", push, 1);
- rb_define_method(cStreeStringSet, "length", length, 0);
- rb_define_method(cStreeStringSet, "delete", delete, 1);
- rb_define_method(cStreeStringSet, "each", each, 0);
-}
View
9 ext/stree/stree_stringset.h
@@ -1,9 +0,0 @@
-#ifndef RUBY_STREE_STRINGSET
-#define RUBY_STREE_STRINGSET
-
-#include <stree.h>
-
-void Init_stree_stringset();
-VALUE Stree_Wrap_StringSet(LST_StringSet * set);
-
-#endif
View
3  lib/stree.rb
@@ -1,6 +1,5 @@
require 'stree/stree'
-require 'stree/string_set'
-class Stree
+module Stree
VERSION = '1.0.0'
end
View
11 lib/stree/string_set.rb
@@ -1,11 +0,0 @@
-class Stree
- class StringSet
- include Enumerable
-
- def initialize
- @weak_refs = {}
- end
-
- alias :<< :push
- end
-end
View
24 test/test_stree.rb
@@ -1,24 +0,0 @@
-require "test/unit"
-require "stree"
-
-class TestStree < Test::Unit::TestCase
- def setup
- @tree = Stree.new
- end
-
- def test_new
- assert Stree.new
- end
-
- def test_push
- @tree.push 'foo'
- end
-
- def test_longest_common_substr
- @tree.push 'yokatta'
- @tree.push 'nemukatta'
-
- list = @tree.longest_common_substr(0, 10)
- assert_equal 'katta', list.first
- end
-end
View
17 test/test_stree_string.rb
@@ -0,0 +1,17 @@
+require 'stree'
+require 'test/unit'
+
+module STree
+ class TestString < Test::Unit::TestCase
+ def test_length
+ string = STree::String.new 'foo'
+ assert_equal 3, string.length
+ end
+
+ def test_type_error
+ assert_raises(TypeError) do
+ STree::String.new Object.new
+ end
+ end
+ end
+end
View
45 test/test_string_set.rb
@@ -1,45 +0,0 @@
-require "test/unit"
-require "stree"
-
-class Stree
- class TestStringSet < Test::Unit::TestCase
- def setup
- @ss = Stree::StringSet.new
- end
-
- def test_push_arg_error
- assert_raises(TypeError) do
- @ss << Object.new
- end
- end
-
- def test_push
- @ss << 'foo'
- assert_equal 1, @ss.length
- end
-
- def test_delete
- foo = 'foo'
- @ss << foo
-
- assert_equal 1, @ss.length
- assert_equal foo, @ss.delete(foo)
- end
-
- def test_delete_not_there
- foo = 'foo'
- assert_nil @ss.delete(foo)
- end
-
- def test_each
- foo = 'foo'
- @ss << foo
- list = []
- @ss.each do |thing|
- list << thing
- end
-
- assert_equal([foo], list)
- end
- end
-end
Please sign in to comment.
Something went wrong with that request. Please try again.