aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Richard Brown <rbrown@exherbo.org> 2008-01-22 08:02:43 +0000
committerAvatar Richard Brown <rbrown@exherbo.org> 2008-01-22 08:02:43 +0000
commit547635fc730cd01c8700a0bdaf9448d891cbcc23 (patch)
tree953818e64b94a58a1c4ef336bbab3660ad77ba59
parentb7eab59421d75a83c5a46f5f7a283b7fd1d96ce3 (diff)
downloadpaludis-547635fc730cd01c8700a0bdaf9448d891cbcc23.tar.gz
paludis-547635fc730cd01c8700a0bdaf9448d891cbcc23.tar.xz
(ruby) Add eql? and hash methods to PackageID and QualifiedPackakgeName to make them work as hash keys.
-rw-r--r--ruby/package_id.cc14
-rw-r--r--ruby/package_id_TEST.rb14
-rw-r--r--ruby/paludis_ruby.hh28
-rw-r--r--ruby/qualified_package_name.cc5
-rw-r--r--ruby/qualified_package_name_TEST.rb13
5 files changed, 60 insertions, 14 deletions
diff --git a/ruby/package_id.cc b/ruby/package_id.cc
index 9ced9ad..ccf2bf0 100644
--- a/ruby/package_id.cc
+++ b/ruby/package_id.cc
@@ -1,7 +1,7 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
- * Copyright (c) 2007 Richard Brown
+ * Copyright (c) 2007, 2008 Richard Brown
* Copyright (c) 2007 Ciaran McCreesh
*
* This file is part of the Paludis package manager. Paludis is free software;
@@ -413,14 +413,6 @@ namespace
}
};
- VALUE
- package_id_equal(VALUE self, VALUE other)
- {
- tr1::shared_ptr<const PackageID> * self_ptr;
- Data_Get_Struct(self, tr1::shared_ptr<const PackageID>, self_ptr);
- return (**self_ptr == *value_to_package_id(other));
- }
-
void do_register_package_id()
{
/*
@@ -435,9 +427,11 @@ namespace
rb_define_method(c_package_id, "version", RUBY_FUNC_CAST(&package_id_version), 0);
rb_define_method(c_package_id, "slot", RUBY_FUNC_CAST((&BaseValue<SlotName,PackageID,&PackageID::slot>::fetch)), 0);
rb_define_method(c_package_id, "repository_name", RUBY_FUNC_CAST(&package_id_repository_name), 0);
- rb_define_method(c_package_id, "==", RUBY_FUNC_CAST(&package_id_equal), 1);
+ rb_define_method(c_package_id, "==", RUBY_FUNC_CAST(&Common<tr1::shared_ptr<const PackageID> >::equal_via_ptr), 1);
rb_define_method(c_package_id, "[]", RUBY_FUNC_CAST(&package_id_subscript), 1);
rb_define_method(c_package_id, "to_s", RUBY_FUNC_CAST(&Common<tr1::shared_ptr<const PackageID> >::to_s_via_ptr), 0);
+ rb_define_method(c_package_id, "hash", RUBY_FUNC_CAST(&Common<tr1::shared_ptr<const PackageID> >::hash_via_ptr), 0);
+ rb_define_method(c_package_id, "eql?", RUBY_FUNC_CAST(&Common<tr1::shared_ptr<const PackageID> >::equal_via_ptr), 1);
rb_define_method(c_package_id, "supports_action", RUBY_FUNC_CAST(&package_id_supports_action), 1);
rb_define_method(c_package_id, "perform_action", RUBY_FUNC_CAST(&package_id_perform_action), 1);
rb_define_method(c_package_id, "each_metadata", RUBY_FUNC_CAST(&package_id_each_metadata), 0);
diff --git a/ruby/package_id_TEST.rb b/ruby/package_id_TEST.rb
index d921385..e312ed3 100644
--- a/ruby/package_id_TEST.rb
+++ b/ruby/package_id_TEST.rb
@@ -2,7 +2,7 @@
# vim: set sw=4 sts=4 et tw=80 :
#
-# Copyright (c) 2007 Richard Brown
+# Copyright (c) 2007, 2008 Richard Brown
#
# This file is part of the Paludis package manager. Paludis is free software;
# you can redistribute it and/or modify it under the terms of the GNU General
@@ -183,6 +183,18 @@ module Paludis
assert_kind_of RepositoryMask, mask
assert_equal "repository_mask", mask.mask_key.raw_name
end
+
+ def test_hash
+ a = pid_testrepo
+ b = pid_testrepo
+ assert_equal a.hash, b.hash
+ end
+
+ def test_eql
+ a = pid_testrepo
+ b = pid_testrepo
+ assert a.eql?(b)
+ end
end
class TestCase_ERepo < Test::Unit::TestCase
diff --git a/ruby/paludis_ruby.hh b/ruby/paludis_ruby.hh
index b19edc1..f45928a 100644
--- a/ruby/paludis_ruby.hh
+++ b/ruby/paludis_ruby.hh
@@ -2,7 +2,7 @@
/*
* Copyright (c) 2006, 2007 Ciaran McCreesh
- * Copyright (c) 2006, 2007 Richard Brown
+ * Copyright (c) 2006, 2007, 2008 Richard Brown
*
* This file is part of the Paludis package manager. Paludis is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
@@ -37,6 +37,7 @@
#include <paludis/mask-fwd.hh>
#include <paludis/metadata_key.hh>
#include <paludis/util/stringify.hh>
+#include <paludis/hashed_containers.hh>
#ifdef ENABLE_RUBY_QA
#include <paludis/qa.hh>
@@ -165,14 +166,37 @@ namespace paludis
return (*left_ptr == *right_ptr) ? Qtrue : Qfalse;
}
+ static VALUE equal_via_ptr(VALUE left, VALUE right)
+ {
+ T_ * left_ptr, * right_ptr;
+ Data_Get_Struct(left, T_, left_ptr);
+ Data_Get_Struct(right, T_, right_ptr);
+ return (**left_ptr == **right_ptr) ? Qtrue : Qfalse;
+ }
+
static VALUE to_s(VALUE self)
{
T_ * self_ptr;
Data_Get_Struct(self, T_, self_ptr);
-
return rb_str_new2(stringify(*self_ptr).c_str());
+
}
+ static VALUE hash(VALUE self)
+ {
+ T_ * self_ptr;
+ Data_Get_Struct(self, T_, self_ptr);
+ return INT2FIX(CRCHash<T_>()(*self_ptr));
+ }
+
+ static VALUE hash_via_ptr(VALUE self)
+ {
+ T_ * self_ptr;
+ Data_Get_Struct(self, T_, self_ptr);
+ return INT2FIX(CRCHash<typename tr1::remove_const<typename T_::element_type>::type>()(**self_ptr));
+ }
+
+
static VALUE to_s_via_ptr(VALUE self)
{
T_ * self_ptr;
diff --git a/ruby/qualified_package_name.cc b/ruby/qualified_package_name.cc
index f2f2b1d..a552325 100644
--- a/ruby/qualified_package_name.cc
+++ b/ruby/qualified_package_name.cc
@@ -2,6 +2,7 @@
/*
* Copyright (c) 2006, 2007 Ciaran McCreesh
+ * Copyright (c) 2008 Richard Brown
*
* This file is part of the Paludis package manager. Paludis is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
@@ -162,10 +163,12 @@ namespace
c_qualified_package_name = rb_define_class_under(paludis_module(), "QualifiedPackageName", rb_cObject);
rb_define_singleton_method(c_qualified_package_name, "new", RUBY_FUNC_CAST(&qualified_package_name_new), -1);
rb_define_method(c_qualified_package_name, "initialize", RUBY_FUNC_CAST(&qualified_package_name_init), -1);
- rb_define_method(c_qualified_package_name, "<=>", RUBY_FUNC_CAST(qualified_package_name_compare), 1);
+ rb_define_method(c_qualified_package_name, "<=>", RUBY_FUNC_CAST(&qualified_package_name_compare), 1);
rb_include_module(c_qualified_package_name, rb_mComparable);
rb_define_method(c_qualified_package_name, "to_s", RUBY_FUNC_CAST(&Common<QualifiedPackageName>::to_s), 0);
rb_define_alias(c_qualified_package_name, "to_str", "to_s");
+ rb_define_method(c_qualified_package_name, "hash", RUBY_FUNC_CAST(&Common<QualifiedPackageName>::hash), 0);
+ rb_define_method(c_qualified_package_name, "eql?", RUBY_FUNC_CAST(&Common<QualifiedPackageName>::equal), 1);
rb_define_method(c_qualified_package_name, "category",
RUBY_FUNC_CAST((&QPNMember<CategoryNamePart, &QualifiedPackageName::category>::fetch)), 0);
rb_define_method(c_qualified_package_name, "category=",
diff --git a/ruby/qualified_package_name_TEST.rb b/ruby/qualified_package_name_TEST.rb
index aec32f6..0ca6e0f 100644
--- a/ruby/qualified_package_name_TEST.rb
+++ b/ruby/qualified_package_name_TEST.rb
@@ -3,6 +3,7 @@
#
# Copyright (c) 2006, 2007 Ciaran McCreesh
+# Copyright (c) 2008 Richard Brown
#
# This file is part of the Paludis package manager. Paludis is free software;
# you can redistribute it and/or modify it under the terms of the GNU General
@@ -98,6 +99,18 @@ module Paludis
QualifiedPackageName.new('foo-bar/baz') <=> Paludis::parse_user_package_dep_spec('foo-bar/baz', [])
end
end
+
+ def test_hash
+ qpn = QualifiedPackageName.new('a/b')
+ qpn2 = QualifiedPackageName.new('a','b')
+ assert_equal qpn.hash, qpn2.hash
+ end
+
+ def test_eql?
+ qpn = QualifiedPackageName.new('a/b')
+ qpn2 = QualifiedPackageName.new('a','b')
+ assert qpn.eql?(qpn2)
+ end
end
end