1// -*- C++ -*-
2
3// Copyright (C) 2005, 2006 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 2, 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 COPYING.  If not, write to
18// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19// MA 02111-1307, USA.
20
21// As a special exception, you may use this file as part of a free
22// software library without restriction.  Specifically, if other files
23// instantiate templates or use macros or inline functions from this
24// file, or you compile this file and link it with other files to
25// produce an executable, this file does not by itself cause the
26// resulting executable to be covered by the GNU General Public
27// License.  This exception does not however invalidate any other
28// reasons why the executable file might be covered by the GNU General
29// Public License.
30
31// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32
33// Permission to use, copy, modify, sell, and distribute this software
34// is hereby granted without fee, provided that the above copyright
35// notice appears in all copies, and that both that copyright notice
36// and this permission notice appear in supporting documentation. None
37// of the above authors, nor IBM Haifa Research Laboratories, make any
38// representation about the suitability of this software for any
39// purpose. It is provided "as is" without express or implied
40// warranty.
41
42/**
43 * @file constructor_destructor_fn_imps.hpp
44 * Contains implementations of cc_ht_map_'s constructors, destructor,
45 *    and related functions.
46 */
47
48PB_DS_CLASS_T_DEC
49typename PB_DS_CLASS_C_DEC::entry_allocator
50PB_DS_CLASS_C_DEC::s_entry_allocator;
51
52PB_DS_CLASS_T_DEC
53typename PB_DS_CLASS_C_DEC::entry_pointer_allocator
54PB_DS_CLASS_C_DEC::s_entry_pointer_allocator;
55
56PB_DS_CLASS_T_DEC
57template<typename It>
58void
59PB_DS_CLASS_C_DEC::
60copy_from_range(It first_it, It last_it)
61{
62  while (first_it != last_it)
63    insert(*(first_it++));
64}
65
66PB_DS_CLASS_T_DEC
67PB_DS_CLASS_C_DEC::
68PB_DS_CLASS_NAME() :
69  ranged_hash_fn_base(resize_base::get_nearest_larger_size(1)),
70  m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0),
71  m_entries(s_entry_pointer_allocator.allocate(m_num_e))
72{
73  initialize();
74  _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
75}
76
77PB_DS_CLASS_T_DEC
78PB_DS_CLASS_C_DEC::
79PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn) :
80  ranged_hash_fn_base(resize_base::get_nearest_larger_size(1), r_hash_fn),
81  m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0),
82  m_entries(s_entry_pointer_allocator.allocate(m_num_e))
83{
84  initialize();
85  _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
86}
87
88PB_DS_CLASS_T_DEC
89PB_DS_CLASS_C_DEC::
90PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn) :
91  PB_DS_HASH_EQ_FN_C_DEC(r_eq_fn),
92  ranged_hash_fn_base(resize_base::get_nearest_larger_size(1), r_hash_fn),
93  m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0),
94  m_entries(s_entry_pointer_allocator.allocate(m_num_e))
95{
96  std::fill(m_entries, m_entries + m_num_e, (entry_pointer)NULL);
97  Resize_Policy::notify_cleared();
98  ranged_hash_fn_base::notify_resized(m_num_e);
99  _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
100}
101
102PB_DS_CLASS_T_DEC
103PB_DS_CLASS_C_DEC::
104PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn, const Comb_Hash_Fn& r_comb_hash_fn) :
105  PB_DS_HASH_EQ_FN_C_DEC(r_eq_fn),
106  ranged_hash_fn_base(resize_base::get_nearest_larger_size(1),
107		      r_hash_fn, r_comb_hash_fn),
108  m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0),
109  m_entries(s_entry_pointer_allocator.allocate(m_num_e))
110{
111  initialize();
112  _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
113}
114
115PB_DS_CLASS_T_DEC
116PB_DS_CLASS_C_DEC::
117PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn, const Comb_Hash_Fn& r_comb_hash_fn, const Resize_Policy& r_resize_policy) :
118  PB_DS_HASH_EQ_FN_C_DEC(r_eq_fn),
119  Resize_Policy(r_resize_policy),
120  ranged_hash_fn_base(resize_base::get_nearest_larger_size(1),
121		      r_hash_fn, r_comb_hash_fn),
122  m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0),
123  m_entries(s_entry_pointer_allocator.allocate(m_num_e))
124{
125  initialize();
126  _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
127}
128
129PB_DS_CLASS_T_DEC
130PB_DS_CLASS_C_DEC::
131PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC& other) :
132#ifdef _GLIBCXX_DEBUG
133  map_debug_base(other),
134#endif
135  PB_DS_HASH_EQ_FN_C_DEC(other),
136  resize_base(other), ranged_hash_fn_base(other),
137  m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0),
138  m_entries(m_entries = s_entry_pointer_allocator.allocate(m_num_e))
139{
140  initialize();
141  _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
142    try
143      {
144        copy_from_range(other.begin(), other.end());
145      }
146    catch(...)
147      {
148        deallocate_all();
149        __throw_exception_again;
150      }
151  _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
152}
153
154PB_DS_CLASS_T_DEC
155PB_DS_CLASS_C_DEC::
156~PB_DS_CLASS_NAME()
157{ deallocate_all(); }
158
159PB_DS_CLASS_T_DEC
160void
161PB_DS_CLASS_C_DEC::
162swap(PB_DS_CLASS_C_DEC& other)
163{
164  _GLIBCXX_DEBUG_ONLY(assert_valid());
165  _GLIBCXX_DEBUG_ONLY(other.assert_valid());
166
167  std::swap(m_entries, other.m_entries);
168  std::swap(m_num_e, other.m_num_e);
169  std::swap(m_num_used_e, other.m_num_used_e);
170  ranged_hash_fn_base::swap(other);
171  hash_eq_fn_base::swap(other);
172  resize_base::swap(other);
173
174  _GLIBCXX_DEBUG_ONLY(map_debug_base::swap(other));
175  _GLIBCXX_DEBUG_ONLY(assert_valid());
176  _GLIBCXX_DEBUG_ONLY(other.assert_valid());
177}
178
179PB_DS_CLASS_T_DEC
180void
181PB_DS_CLASS_C_DEC::
182deallocate_all()
183{
184  clear();
185  s_entry_pointer_allocator.deallocate(m_entries, m_num_e);
186}
187
188PB_DS_CLASS_T_DEC
189void
190PB_DS_CLASS_C_DEC::
191initialize()
192{
193  std::fill(m_entries, m_entries + m_num_e, entry_pointer(NULL));
194  Resize_Policy::notify_resized(m_num_e);
195  Resize_Policy::notify_cleared();
196  ranged_hash_fn_base::notify_resized(m_num_e);
197}
198