1// -*- C++ -*-
2
3// Copyright (C) 2005-2015 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library.  This library is free
6// software; you can redistribute it and/or modify it under the terms
7// of the GNU General Public License as published by the Free Software
8// Foundation; either version 3, or (at your option) any later
9// version.
10
11// This library is distributed in the hope that it will be useful, but
12// WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14// General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this library; see the file COPYING3.  If not see
18// <http://www.gnu.org/licenses/>.
19
20
21// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
22
23// Permission to use, copy, modify, sell, and distribute this software
24// is hereby granted without fee, provided that the above copyright
25// notice appears in all copies, and that both that copyright notice
26// and this permission notice appear in supporting documentation. None
27// of the above authors, nor IBM Haifa Research Laboratories, make any
28// representation about the suitability of this software for any
29// purpose. It is provided "as is" without express or implied
30// warranty.
31
32/**
33 * @file native_type_trait.hpp
34 * Contains traits for a random regression test
35 *    for a specific container type.
36 */
37
38#ifndef PB_DS_REGRESSION_TEST_NATIVE_TYPE_TRAIT_HPP
39#define PB_DS_REGRESSION_TEST_NATIVE_TYPE_TRAIT_HPP
40
41namespace __gnu_pbds
42{
43
44  namespace test
45  {
46
47    namespace detail
48    {
49
50      template<typename Key, typename _Alloc>
51      struct native_key_type;
52
53      template<typename _Alloc>
54      struct native_key_type<
55	basic_type,
56	_Alloc>
57      {
58	typedef std::string type;
59
60	static type
61        native_key(typename _Alloc::template rebind<
62		   basic_type>::other::const_reference r_key)
63	{
64	  return (std::string(r_key));
65	}
66      };
67
68      template<typename Hd, class Tl, typename _Alloc>
69      struct native_key_type<
70	std::pair<
71        Hd,
72        Tl>,
73	_Alloc>
74      {
75	typedef typename native_key_type< Hd, _Alloc>::type hd_type;
76
77	typedef typename native_key_type< Tl, _Alloc>::type tl_type;
78
79	typedef std::pair< hd_type, tl_type> type;
80
81	static type
82        native_key(typename _Alloc::template rebind<            std::pair<Hd, Tl> >::other::const_reference r_key)
83	{
84	  return (std::make_pair(
85				 native_key_type<Hd, _Alloc>::native_key(r_key.first),
86				 native_key_type<Tl, _Alloc>::native_key(r_key.second)));
87	}
88      };
89
90      template<typename Native_Key_Type,
91	       class Key_Type,
92	       class Data_Type,
93	       typename _Alloc>
94      struct native_type_traits_base;
95
96      template<typename Native_Key_Type, class Key_Type, typename _Alloc>
97      struct native_type_traits_base<
98	Native_Key_Type,
99	Key_Type,
100	basic_type,
101	_Alloc>
102      {
103      public:
104	typedef std::map< Native_Key_Type, std::string> type;
105
106      public:
107	static const typename type::key_type&
108        extract_key(typename type::const_reference r_val)
109	{
110	  return (r_val.first);
111	}
112
113	static typename type::value_type
114        native_value(typename _Alloc::template rebind<            std::pair<Key_Type, basic_type> >::other::const_reference r_val)
115	{
116	  return (std::make_pair(
117				 native_key_type<Key_Type, _Alloc>::native_key(r_val.first),
118				 std::string(r_val.second)));
119	}
120      };
121
122      template<typename Native_Key_Type, class Key_Type, typename _Alloc>
123      struct native_type_traits_base<
124	Native_Key_Type,
125	Key_Type,
126	__gnu_pbds::null_type,
127	_Alloc>
128      {
129      public:
130	typedef std::set< Native_Key_Type> type;
131
132      public:
133	static const typename type::key_type&
134        extract_key(typename type::const_reference r_val)
135	{
136	  return (r_val);
137	}
138
139	static typename type::value_type
140        native_value(typename _Alloc::template rebind<
141		     Key_Type>::other::const_reference r_val)
142	{
143	  return (native_key_type<Key_Type, _Alloc>::native_key(
144								   r_val));
145	}
146      };
147
148#define PB_DS_NATIVE_KEY_TYPE_C_DEC				\
149      native_key_type<						\
150						Key_Type,	\
151						_Alloc>
152
153#define PB_DS_BASE_C_DEC						\
154      native_type_traits_base<						\
155									typename PB_DS_NATIVE_KEY_TYPE_C_DEC::type, \
156									Key_Type, \
157									Data_Type, \
158									_Alloc>
159
160      template<typename Key_Type, class Data_Type, typename _Alloc>
161      struct native_type_traits : public PB_DS_BASE_C_DEC
162      {
163	typedef typename PB_DS_BASE_C_DEC::type type;
164
165	typedef typename type::key_type key_type;
166
167	static typename PB_DS_NATIVE_KEY_TYPE_C_DEC::type
168        native_key(typename _Alloc::template rebind<
169		   Key_Type>::other::const_reference r_key)
170	{
171	  return (PB_DS_NATIVE_KEY_TYPE_C_DEC::native_key(r_key));
172	}
173      };
174
175#undef PB_DS_BASE_C_DEC
176
177#undef PB_DS_NATIVE_KEY_TYPE_C_DEC
178
179    } // namespace detail
180
181  } // namespace test
182
183} // namespace __gnu_pbds
184
185#endif // #ifndef PB_DS_REGRESSION_TEST_NATIVE_TYPE_TRAIT_HPP
186