From 03daddf53a88bf1a3b7890d02577dd8921d70b76 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 2 Sep 2010 13:56:39 -0700 Subject: add Patricia#include? method This behaves like Patricia#match_best, but is more efficient as it does not need to allocate a new object on successful matches. --- README | 8 ++++++++ ext/rpatricia/rpatricia.c | 18 ++++++++++++++++++ test/test_include.rb | 25 +++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 test/test_include.rb diff --git a/README b/README index 23ce6c1..0f9c012 100644 --- a/README +++ b/README @@ -124,6 +124,14 @@ search_exact: match_exact: An alias of search_exact. +include?: + + pt.include?(key_string); + + This method behaves like match_best, but returns true on success + and false on failure. This method is more efficient than match_best + as it does not allocate a new object. + remove: pt.remove(key_string); diff --git a/ext/rpatricia/rpatricia.c b/ext/rpatricia/rpatricia.c index 866572c..2c3fcc9 100644 --- a/ext/rpatricia/rpatricia.c +++ b/ext/rpatricia/rpatricia.c @@ -117,6 +117,21 @@ p_match (VALUE self, VALUE r_key) return node ? wrap_node(node) : Qfalse; } +static VALUE +p_include (VALUE self, VALUE r_key) +{ + patricia_tree_t *tree; + patricia_node_t *node; + prefix_t *prefix; + + Data_Get_Struct(self, patricia_tree_t, tree); + prefix = my_ascii2prefix (AF_INET, r_key); + node = patricia_search_best(tree, prefix); + Deref_Prefix (prefix); + + return node ? Qtrue : Qfalse; +} + static VALUE p_match_exact (VALUE self, VALUE r_key) { @@ -304,6 +319,9 @@ Init_rpatricia (void) rb_define_method(cPatricia, "match_exact", p_match_exact, 1); rb_define_method(cPatricia, "search_exact", p_match_exact, 1); + /* check existence */ + rb_define_method(cPatricia, "include?", p_include, 1); + /* removal */ rb_define_method(cPatricia, "remove", p_remove, 1); rb_define_method(cPatricia, "remove_node", p_remove, 1); diff --git a/test/test_include.rb b/test/test_include.rb new file mode 100644 index 0000000..303ad46 --- /dev/null +++ b/test/test_include.rb @@ -0,0 +1,25 @@ +require 'test/unit' +require 'rpatricia' + +class TestInclude < Test::Unit::TestCase + + def setup + @t = Patricia.new + end + + def test_include_exact + @t.add '127.0.0.1' + assert_equal true, @t.include?('127.0.0.1') + assert_equal false, @t.include?('127.0.0.2') + @t.clear + assert_equal false, @t.include?('127.0.0.1') + end + + def test_include_match_prefix + @t.add '127.0.0.0/8' + assert_equal true, @t.include?('127.0.0.32') + assert_equal false, @t.include?('12.0.0.32') + @t.clear + assert_equal false, @t.include?('127.0.0.32') + end +end -- cgit v1.2.3-24-ge0c7