Chris Lattner | 4848689 | 2003-09-30 18:37:50 +0000 | [diff] [blame] | 1 | //===-- HashExtras.h - Useful functions for STL hash containers -*- C++ -*-===// |
John Criswell | b2109ce | 2003-10-20 19:46:57 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 57dbb3a | 2001-07-23 17:46:59 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file contains some templates that are useful if you are working with the |
| 11 | // STL Hashed containers. |
| 12 | // |
| 13 | // No library is required when using these functinons. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Brian Gaeke | a9f6e4a | 2003-06-17 00:35:55 +0000 | [diff] [blame] | 17 | #ifndef SUPPORT_HASHEXTRAS_H |
| 18 | #define SUPPORT_HASHEXTRAS_H |
Chris Lattner | 57dbb3a | 2001-07-23 17:46:59 +0000 | [diff] [blame] | 19 | |
Chris Lattner | 4a63b72 | 2002-10-28 02:11:53 +0000 | [diff] [blame] | 20 | #include "Support/hash_map" |
Misha Brukman | 53523e5 | 2003-08-15 17:52:02 +0000 | [diff] [blame] | 21 | #include <string> |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 22 | |
| 23 | // Cannot specialize hash template from outside of the std namespace. |
Chris Lattner | 8dc6716 | 2002-07-24 22:20:00 +0000 | [diff] [blame] | 24 | namespace HASH_NAMESPACE { |
Chris Lattner | 57dbb3a | 2001-07-23 17:46:59 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 8dc6716 | 2002-07-24 22:20:00 +0000 | [diff] [blame] | 26 | template <> struct hash<std::string> { |
| 27 | size_t operator()(std::string const &str) const { |
Chris Lattner | 57dbb3a | 2001-07-23 17:46:59 +0000 | [diff] [blame] | 28 | return hash<char const *>()(str.c_str()); |
| 29 | } |
| 30 | }; |
| 31 | |
Vikram S. Adve | bf2b7e8 | 2001-07-28 04:41:10 +0000 | [diff] [blame] | 32 | // Provide a hash function for arbitrary pointers... |
| 33 | template <class T> struct hash<T *> { |
Chris Lattner | 8b70b78 | 2003-11-16 20:21:15 +0000 | [diff] [blame] | 34 | inline size_t operator()(const T *Val) const { |
| 35 | return reinterpret_cast<size_t>(Val); |
| 36 | } |
Vikram S. Adve | bf2b7e8 | 2001-07-28 04:41:10 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 39 | } // End namespace std |
| 40 | |
Chris Lattner | 57dbb3a | 2001-07-23 17:46:59 +0000 | [diff] [blame] | 41 | #endif |