aboutsummaryrefslogtreecommitdiff
path: root/0.4.0/paludis/hashed_containers.hh.in
diff options
context:
space:
mode:
Diffstat (limited to '0.4.0/paludis/hashed_containers.hh.in')
-rw-r--r--0.4.0/paludis/hashed_containers.hh.in293
1 files changed, 293 insertions, 0 deletions
diff --git a/0.4.0/paludis/hashed_containers.hh.in b/0.4.0/paludis/hashed_containers.hh.in
new file mode 100644
index 000000000..857d06417
--- /dev/null
+++ b/0.4.0/paludis/hashed_containers.hh.in
@@ -0,0 +1,293 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+@GENERATED_FILE@
+
+/*
+ * Copyright (c) 2006 Ciaran McCreesh <ciaran.mccreesh@blueyonder.co.uk>
+ *
+ * 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
+ * Public License version 2, as published by the Free Software Foundation.
+ *
+ * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef PALUDIS_GUARD_PALUDIS_HASHED_CONTAINERS_HH
+#define PALUDIS_GUARD_PALUDIS_HASHED_CONTAINERS_HH 1
+
+/** \file
+ * Declarations for the MakeHashedMap and MakeHashedSet classes, and related
+ * utilities.
+ *
+ * \ingroup grphashedcontainers
+ */
+
+/**
+ * Non-zero if we have std::tr1:: hashes (g++-4).
+ *
+ * \ingroup grphashedcontainers
+ */
+#define PALUDIS_HAVE_TR1_HASHES @HAVE_TR1_HASHES@
+
+/**
+ * Non-zero if we have __gnu_cxx:: hashes under ext/ (g++-3).
+ *
+ * \ingroup grphashedcontainers
+ */
+#define PALUDIS_HAVE_EXT_HASHES @HAVE_EXT_HASHES@
+
+
+/**
+ * Non-zero if we have std:: hashes (icc70).
+ *
+ * \ingroup grphashedcontainers
+ */
+#define PALUDIS_HAVE_STD_HASHES @HAVE_STD_HASHES@
+
+#include <paludis/name.hh>
+#include <paludis/util/validated.hh>
+#include <paludis/version_spec.hh>
+
+#if PALUDIS_HAVE_TR1_HASHES
+# include <tr1/unordered_set>
+# include <tr1/unordered_map>
+#elif PALUDIS_HAVE_EXT_HASHES
+# include <ext/hash_set>
+# include <ext/hash_map>
+#elif PALUDIS_HAVE_STD_HASHES
+# include <hash_set>
+# include <hash_map>
+#else
+# include <set>
+# include <map>
+#endif
+
+#include <limits>
+#include <string>
+#include <functional>
+
+namespace paludis
+{
+ /**
+ * Hash function base template.
+ *
+ * \ingroup grphashedcontainers
+ */
+ template <typename T_>
+ struct CRCHash;
+
+ /**
+ * Make a hashed map of some kind from Key_ to Value_.
+ *
+ * \ingroup grphashedcontainers
+ */
+ template <typename Key_, typename Value_>
+ struct MakeHashedMap
+ {
+#if PALUDIS_HAVE_TR1_HASHES
+ /// Our map type.
+ typedef std::tr1::unordered_map<Key_, Value_, CRCHash<Key_> > Type;
+
+#elif PALUDIS_HAVE_EXT_HASHES
+ /// Our map type.
+ typedef __gnu_cxx::hash_map<Key_, Value_, CRCHash<Key_> > Type;
+
+#elif PALUDIS_HAVE_STD_HASHES
+ /// Our map type.
+ typedef std::hash_map<Key_, Value_, CRCHash<Key_> > Type;
+
+#else
+ /// Our map type.
+ typedef std::map<Key_, Value_> Type;
+#endif
+ };
+
+ /**
+ * Make a hashed set of some kind of Key_.
+ *
+ * \ingroup grphashedcontainers
+ */
+ template <typename Key_>
+ struct MakeHashedSet
+ {
+#if PALUDIS_HAVE_TR1_HASHES
+ /// Our set type.
+ typedef std::tr1::unordered_set<Key_, CRCHash<Key_> > Type;
+
+#elif PALUDIS_HAVE_EXT_HASHES
+ /// Our set type.
+ typedef __gnu_cxx::hash_set<Key_, CRCHash<Key_> > Type;
+
+#elif PALUDIS_HAVE_STD_HASHES
+ /// Our set type.
+ typedef std::hash_set<Key_, CRCHash<Key_> > Type;
+
+#else
+ /// Our set type.
+ typedef std::set<Key_> Type;
+#endif
+ };
+
+#if PALUDIS_HAVE_TR1_HASHES || PALUDIS_HAVE_EXT_HASHES || PALUDIS_HAVE_STD_HASHES
+ namespace hashed_containers_internals
+ {
+ /**
+ * Base definitions for our CRCHash.
+ *
+ * \ingroup grphashedcontainers
+ */
+ struct CRCHashBase
+ {
+ /// Shift value.
+ static const std::size_t h_shift = std::numeric_limits<std::size_t>::digits - 5;
+
+ /// Mask value.
+ static const std::size_t h_mask = static_cast<std::size_t>(0x1f) << h_shift;
+ };
+ }
+
+ /**
+ * Hash, for QualifiedPackageName.
+ *
+ * \ingroup grphashedcontainers
+ */
+ template <>
+ class CRCHash<QualifiedPackageName> :
+ public std::unary_function<QualifiedPackageName, std::size_t>,
+ protected hashed_containers_internals::CRCHashBase
+ {
+ public:
+ /// Hash function.
+ std::size_t operator() (const QualifiedPackageName & val) const;
+
+#if (! PALUDIS_HAVE_TR1_HASHES) && (! PALUDIS_HAVE_EXT_HASHES)
+ enum
+ {
+ min_buckets = 32,
+ bucket_size = 4
+ };
+
+ bool operator() (const QualifiedPackageName & i1, const QualifiedPackageName & i2) const
+ {
+ return i1 < i2;
+ }
+#endif
+ };
+
+ /**
+ * Hash, for a validated string type.
+ *
+ * \ingroup grphashedcontainers
+ */
+ template <typename Validated_>
+ class CRCHash<Validated<std::string, Validated_> > :
+ public std::unary_function<Validated<std::string, Validated_>, std::size_t>,
+ protected hashed_containers_internals::CRCHashBase
+ {
+ public:
+ /// Hash function.
+ std::size_t operator() (const Validated<std::string, Validated_> & val) const;
+
+#if (! PALUDIS_HAVE_TR1_HASHES) && (! PALUDIS_HAVE_EXT_HASHES)
+ enum
+ {
+ min_buckets = 32,
+ bucket_size = 4
+ };
+
+ bool operator() (const Validated<std::string, Validated_> i1,
+ const Validated<std::string, Validated_> & i2) const
+ {
+ return i1 < i2;
+ }
+#endif
+ };
+
+
+ /**
+ * Hash, for a string.
+ *
+ * \ingroup grphashedcontainers
+ */
+ template<>
+ class CRCHash<std::string> :
+ public std::unary_function<std::string, std::size_t>,
+ protected hashed_containers_internals::CRCHashBase
+ {
+ public:
+ /// Hash function.
+ std::size_t operator() (const std::string & val) const;
+
+#if (! PALUDIS_HAVE_TR1_HASHES) && (! PALUDIS_HAVE_EXT_HASHES)
+ enum
+ {
+ min_buckets = 32,
+ bucket_size = 4
+ };
+
+ bool operator() (const std::string & i1, const std::string & i2) const
+ {
+ return i1 < i2;
+ }
+#endif
+ };
+
+ /**
+ * Hash, for a QualifiedPackageName + VersionSpec pair.
+ *
+ * \ingroup grphashedcontainers
+ */
+ template <>
+ class CRCHash<std::pair<QualifiedPackageName, VersionSpec> > :
+ public std::unary_function<std::pair<QualifiedPackageName, VersionSpec>, std::size_t>,
+ protected hashed_containers_internals::CRCHashBase
+ {
+ public:
+ /// Hash function.
+ std::size_t operator() (const std::pair<QualifiedPackageName, VersionSpec> & val) const;
+
+#if (! PALUDIS_HAVE_TR1_HASHES) && (! PALUDIS_HAVE_EXT_HASHES)
+ enum
+ {
+ min_buckets = 32,
+ bucket_size = 4
+ };
+
+ bool operator() (const std::pair<QualifiedPackageName, VersionSpec> & i1,
+ const std::pair<QualifiedPackageName, VersionSpec> & i2) const
+ {
+ return i1 < i2;
+ }
+#endif
+ };
+
+ template <typename Validated_>
+ std::size_t
+ CRCHash<Validated<std::string, Validated_> >::operator() (const Validated<std::string, Validated_> & val) const
+ {
+ const std::string & s1(val.data());
+ std::size_t h(0);
+
+ for (std::string::size_type t(0) ; t < s1.length() ; ++t)
+ {
+ std::size_t hh(h & h_mask);
+ h <<= 5;
+ h ^= (hh >> h_shift);
+ h ^= s1[t];
+ }
+
+ return h;
+ }
+
+#endif
+
+}
+
+#endif