| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-27 23:38:01 +00:00
										 |  |  | // Copyright 2006-2008 Daniel James.
 | 
					
						
							| 
									
										
										
										
											2006-07-01 22:31:26 +00:00
										 |  |  | // Distributed under the Boost Software License, Version 1.0. (See accompanying
 | 
					
						
							|  |  |  | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if !defined(BOOST_UNORDERED_TEST_OBJECTS_HEADER)
 | 
					
						
							|  |  |  | #define BOOST_UNORDERED_TEST_OBJECTS_HEADER
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-11 19:42:55 +00:00
										 |  |  | #include <boost/config.hpp>
 | 
					
						
							|  |  |  | #include <boost/limits.hpp>
 | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  | #include <cstddef>
 | 
					
						
							| 
									
										
										
										
											2006-06-11 19:42:55 +00:00
										 |  |  | #include <iostream>
 | 
					
						
							| 
									
										
										
										
											2008-01-10 22:30:46 +00:00
										 |  |  | #include "../helpers/fwd.hpp"
 | 
					
						
							|  |  |  | #include "memory.hpp"
 | 
					
						
							| 
									
										
										
										
											2006-06-11 19:42:55 +00:00
										 |  |  | #include <map>
 | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace test | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // Note that the default hash function will work for any equal_to (but not
 | 
					
						
							|  |  |  |     // very well).
 | 
					
						
							|  |  |  |     class object; | 
					
						
							|  |  |  |     class hash; | 
					
						
							|  |  |  |     class less; | 
					
						
							|  |  |  |     class equal_to; | 
					
						
							|  |  |  |     template <class T> class allocator; | 
					
						
							| 
									
										
										
										
											2008-01-20 18:55:57 +00:00
										 |  |  |     object generate(object const*); | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     class object | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         friend class hash; | 
					
						
							|  |  |  |         friend class equal_to; | 
					
						
							|  |  |  |         friend class less; | 
					
						
							|  |  |  |         int tag1_, tag2_; | 
					
						
							|  |  |  |     public: | 
					
						
							|  |  |  |         explicit object(int t1 = 0, int t2 = 0) : tag1_(t1), tag2_(t2) {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-10-31 22:19:26 +00:00
										 |  |  |         ~object() { | 
					
						
							|  |  |  |             tag1_ = -1; | 
					
						
							|  |  |  |             tag2_ = -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |         friend bool operator==(object const& x1, object const& x2) { | 
					
						
							|  |  |  |             return x1.tag1_ == x2.tag1_ && x1.tag2_ == x2.tag2_; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         friend bool operator!=(object const& x1, object const& x2) { | 
					
						
							|  |  |  |             return x1.tag1_ != x2.tag1_ || x1.tag2_ != x2.tag2_; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         friend bool operator<(object const& x1, object const& x2) { | 
					
						
							|  |  |  |             return x1.tag1_ < x2.tag1_ || | 
					
						
							|  |  |  |                 (x1.tag1_ == x2.tag1_ && x1.tag2_ < x2.tag2_); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         friend object generate(object const*) { | 
					
						
							| 
									
										
										
										
											2006-06-12 23:30:46 +00:00
										 |  |  |             int* x = 0; | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |             return object(generate(x), generate(x)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         friend std::ostream& operator<<(std::ostream& out, object const& o) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             return out<<"("<<o.tag1_<<","<<o.tag2_<<")"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class hash | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         int type_; | 
					
						
							|  |  |  |     public: | 
					
						
							|  |  |  |         explicit hash(int t = 0) : type_(t) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         std::size_t operator()(object const& x) const { | 
					
						
							|  |  |  |             switch(type_) { | 
					
						
							|  |  |  |             case 1: | 
					
						
							|  |  |  |                 return x.tag1_; | 
					
						
							|  |  |  |             case 2: | 
					
						
							|  |  |  |                 return x.tag2_; | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 return x.tag1_ + x.tag2_;  | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         std::size_t operator()(int x) const { | 
					
						
							|  |  |  |             return x; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         friend bool operator==(hash const& x1, hash const& x2) { | 
					
						
							|  |  |  |             return x1.type_ == x2.type_; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         friend bool operator!=(hash const& x1, hash const& x2) { | 
					
						
							|  |  |  |             return x1.type_ != x2.type_; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class less | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         int type_; | 
					
						
							|  |  |  |     public: | 
					
						
							|  |  |  |         explicit less(int t = 0) : type_(t) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         bool operator()(object const& x1, object const& x2) const { | 
					
						
							|  |  |  |             switch(type_) { | 
					
						
							|  |  |  |             case 1: | 
					
						
							|  |  |  |                 return x1.tag1_ < x2.tag1_; | 
					
						
							|  |  |  |             case 2: | 
					
						
							|  |  |  |                 return x1.tag2_ < x2.tag2_; | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 return x1 < x2; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         std::size_t operator()(int x1, int x2) const { | 
					
						
							|  |  |  |             return x1 < x2; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         friend bool operator==(less const& x1, less const& x2) { | 
					
						
							|  |  |  |             return x1.type_ == x2.type_; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class equal_to | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         int type_; | 
					
						
							|  |  |  |     public: | 
					
						
							|  |  |  |         explicit equal_to(int t = 0) : type_(t) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         bool operator()(object const& x1, object const& x2) const { | 
					
						
							|  |  |  |             switch(type_) { | 
					
						
							|  |  |  |             case 1: | 
					
						
							|  |  |  |                 return x1.tag1_ == x2.tag1_; | 
					
						
							|  |  |  |             case 2: | 
					
						
							|  |  |  |                 return x1.tag2_ == x2.tag2_; | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 return x1 == x2; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         std::size_t operator()(int x1, int x2) const { | 
					
						
							|  |  |  |             return x1 == x2; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         friend bool operator==(equal_to const& x1, equal_to const& x2) { | 
					
						
							|  |  |  |             return x1.type_ == x2.type_; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         friend bool operator!=(equal_to const& x1, equal_to const& x2) { | 
					
						
							|  |  |  |             return x1.type_ != x2.type_; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         friend less create_compare(equal_to x) { | 
					
						
							|  |  |  |             return less(x.type_); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-11 19:42:55 +00:00
										 |  |  |     namespace detail | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         namespace { | 
					
						
							| 
									
										
										
										
											2008-01-18 19:35:55 +00:00
										 |  |  |             test::detail::memory_tracker<std::allocator<int> > tracker; | 
					
						
							| 
									
										
										
										
											2006-06-11 19:42:55 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |     template <class T> | 
					
						
							|  |  |  |     class allocator | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2006-05-21 17:14:11 +00:00
										 |  |  | # ifdef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
 | 
					
						
							|  |  |  |     public: | 
					
						
							|  |  |  | # else
 | 
					
						
							|  |  |  |         template <class> friend class allocator; | 
					
						
							|  |  |  | # endif
 | 
					
						
							|  |  |  |         int tag_; | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |     public: | 
					
						
							|  |  |  |         typedef std::size_t size_type; | 
					
						
							|  |  |  |         typedef std::ptrdiff_t difference_type; | 
					
						
							|  |  |  |         typedef T* pointer; | 
					
						
							|  |  |  |         typedef T const* const_pointer; | 
					
						
							|  |  |  |         typedef T& reference; | 
					
						
							|  |  |  |         typedef T const& const_reference; | 
					
						
							|  |  |  |         typedef T value_type; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         template <class U> struct rebind { typedef allocator<U> other; }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-10 22:30:46 +00:00
										 |  |  |         explicit allocator(int t = 0) : tag_(t) { detail::tracker.allocator_ref(); } | 
					
						
							|  |  |  |         template <class Y> allocator(allocator<Y> const& x) : tag_(x.tag_) { detail::tracker.allocator_ref(); } | 
					
						
							|  |  |  |         allocator(allocator const& x) : tag_(x.tag_) { detail::tracker.allocator_ref(); } | 
					
						
							|  |  |  |         ~allocator() { detail::tracker.allocator_unref(); } | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         pointer address(reference r) { return pointer(&r); } | 
					
						
							|  |  |  |         const_pointer address(const_reference r) { return const_pointer(&r); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         pointer allocate(size_type n) { | 
					
						
							| 
									
										
										
										
											2006-06-11 19:42:55 +00:00
										 |  |  |             pointer ptr(static_cast<T*>(::operator new(n * sizeof(T)))); | 
					
						
							| 
									
										
										
										
											2008-01-10 22:30:46 +00:00
										 |  |  |             detail::tracker.track_allocate((void*) ptr, n, sizeof(T), tag_); | 
					
						
							| 
									
										
										
										
											2006-06-11 19:42:55 +00:00
										 |  |  |             return ptr; | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Merged revisions 42882-42941 via svnmerge from 
https://svn.boost.org/svn/boost/branches/unordered/trunk
................
  r42887 | danieljames | 2008-01-20 21:32:04 +0000 (Sun, 20 Jan 2008) | 10 lines
  
  Merged revisions 42590-42664,42667-42697,42699-42723,42725-42855,42857-42881 via svnmerge from 
  https://svn.boost.org/svn/boost/trunk
  
  ........
    r42881 | danieljames | 2008-01-20 17:37:21 +0000 (Sun, 20 Jan 2008) | 1 line
    
    Include <new> to get std::bad_alloc.
  ........
................
  r42892 | danieljames | 2008-01-21 13:03:16 +0000 (Mon, 21 Jan 2008) | 1 line
  
  On some compilers the Rogue Wave/Apache stdcxx library doesn't have the normal std::distance, but instead has a variant that takes the result as the third parameter so it doesn't have to work out the type from the iterator.
................
  r42893 | danieljames | 2008-01-21 13:07:58 +0000 (Mon, 21 Jan 2008) | 1 line
  
  Fix a typo in the last commit.
................
  r42895 | danieljames | 2008-01-21 13:33:29 +0000 (Mon, 21 Jan 2008) | 1 line
  
  Remove tabs from the last checkin.
................
  r42896 | danieljames | 2008-01-21 15:51:40 +0000 (Mon, 21 Jan 2008) | 1 line
  
  Use Boost config to tell when we have a std::distance function. Also, no need for a macro.
................
  r42908 | danieljames | 2008-01-21 21:37:04 +0000 (Mon, 21 Jan 2008) | 1 line
  
  Use boost::long_long_type and boost::ulong_long_type.
................
  r42921 | danieljames | 2008-01-23 11:43:35 +0000 (Wed, 23 Jan 2008) | 1 line
  
  Remove some tabs.
................
  r42922 | danieljames | 2008-01-23 11:46:28 +0000 (Wed, 23 Jan 2008) | 2 lines
  
  Add missing include. Refs #1596
................
  r42923 | danieljames | 2008-01-23 11:52:47 +0000 (Wed, 23 Jan 2008) | 2 lines
  
  Always use void const* for the second parameter of allocate. Refs #1596.
................
  r42936 | danieljames | 2008-01-23 22:22:16 +0000 (Wed, 23 Jan 2008) | 1 line
  
  Use Boost style library name in the documentation.
................
  r42937 | danieljames | 2008-01-23 22:22:32 +0000 (Wed, 23 Jan 2008) | 1 line
  
  More tabs.
................
  r42941 | danieljames | 2008-01-23 23:35:01 +0000 (Wed, 23 Jan 2008) | 1 line
  
  Fix all the allocators.
................
[SVN r42943]
											
										 
											2008-01-23 23:39:59 +00:00
										 |  |  |         pointer allocate(size_type n, void const* u) | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2006-06-11 19:42:55 +00:00
										 |  |  |             pointer ptr(static_cast<T*>(::operator new(n * sizeof(T)))); | 
					
						
							| 
									
										
										
										
											2008-01-10 22:30:46 +00:00
										 |  |  |             detail::tracker.track_allocate((void*) ptr, n, sizeof(T), tag_); | 
					
						
							| 
									
										
										
										
											2006-06-11 19:42:55 +00:00
										 |  |  |             return ptr; | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         void deallocate(pointer p, size_type n) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2008-01-10 22:30:46 +00:00
										 |  |  |             detail::tracker.track_deallocate((void*) p, n, sizeof(T), tag_); | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |             ::operator delete((void*) p); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-11 19:42:55 +00:00
										 |  |  |         void construct(pointer p, T const& t) { | 
					
						
							| 
									
										
										
										
											2008-01-10 22:30:46 +00:00
										 |  |  |             detail::tracker.track_construct((void*) p, sizeof(T), tag_); | 
					
						
							| 
									
										
										
										
											2006-06-11 19:42:55 +00:00
										 |  |  |             new(p) T(t); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         void destroy(pointer p) { | 
					
						
							| 
									
										
										
										
											2008-01-10 22:30:46 +00:00
										 |  |  |             detail::tracker.track_destroy((void*) p, sizeof(T), tag_); | 
					
						
							| 
									
										
										
										
											2006-06-11 19:42:55 +00:00
										 |  |  |             p->~T(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-21 17:14:11 +00:00
										 |  |  |         size_type max_size() const { | 
					
						
							|  |  |  |             return (std::numeric_limits<size_type>::max)(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-18 13:24:38 +00:00
										 |  |  |         bool operator==(allocator const& x) const | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             return tag_ == x.tag_; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-18 13:24:38 +00:00
										 |  |  |         bool operator!=(allocator const& x) const | 
					
						
							| 
									
										
										
										
											2006-05-21 17:14:11 +00:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2006-06-18 13:24:38 +00:00
										 |  |  |             return tag_ != x.tag_; | 
					
						
							| 
									
										
										
										
											2006-05-21 17:14:11 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2006-12-03 23:08:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-12 23:30:46 +00:00
										 |  |  |     template <class T> | 
					
						
							| 
									
										
										
										
											2006-06-18 13:24:38 +00:00
										 |  |  |     bool equivalent_impl(allocator<T> const& x, allocator<T> const& y, test::derived_type) { | 
					
						
							|  |  |  |         return x == y; | 
					
						
							| 
									
										
										
										
											2006-06-12 23:30:46 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2007-07-17 23:17:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if BOOST_WORKAROUND(__GNUC__, < 3)
 | 
					
						
							|  |  |  |     void swap(test::object& x, test::object& y) { | 
					
						
							|  |  |  |         test::object tmp; | 
					
						
							|  |  |  |         tmp = x; | 
					
						
							|  |  |  |         x = y; | 
					
						
							|  |  |  |         y = tmp; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void swap(test::hash& x, test::hash& y) { | 
					
						
							|  |  |  |         test::hash tmp; | 
					
						
							|  |  |  |         tmp = x; | 
					
						
							|  |  |  |         x = y; | 
					
						
							|  |  |  |         y = tmp; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void swap(test::less& x, test::less& y) { | 
					
						
							|  |  |  |         test::less tmp; | 
					
						
							|  |  |  |         tmp = x; | 
					
						
							|  |  |  |         x = y; | 
					
						
							|  |  |  |         y = tmp; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void swap(test::equal_to& x, test::equal_to& y) { | 
					
						
							|  |  |  |         test::equal_to tmp; | 
					
						
							|  |  |  |         tmp = x; | 
					
						
							|  |  |  |         x = y; | 
					
						
							|  |  |  |         y = tmp; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     template <class T> | 
					
						
							|  |  |  |     void swap(test::allocator<T>& x, test::allocator<T>& y) { | 
					
						
							|  |  |  |         test::allocator<T> tmp; | 
					
						
							|  |  |  |         tmp = x; | 
					
						
							|  |  |  |         x = y; | 
					
						
							|  |  |  |         y = tmp; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2006-06-12 23:30:46 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  | #endif
 |