initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / OpenFOAM / containers / HashTables / HashPtrTable / HashPtrTable.C
blobd481c6fb8a50169fc93025bfed6b7bad1bec775c
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "error.H"
29 #include "HashPtrTable.H"
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 namespace Foam
36 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
38 // Construct given initial table size
39 template<class T, class Key, class Hash>
40 HashPtrTable<T, Key, Hash>::HashPtrTable(label size)
42     HashTable<T*, Key, Hash>(size)
46 // Construct as copy
47 template<class T, class Key, class Hash>
48 HashPtrTable<T, Key, Hash>::HashPtrTable(const HashPtrTable<T, Key, Hash>& ht)
50     HashTable<T*, Key, Hash>()
52     for (const_iterator iter = ht.begin(); iter != ht.end(); ++iter)
53     {
54         insert(iter.key(), new T(**iter));
55     }
59 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
61 template<class T, class Key, class Hash>
62 HashPtrTable<T, Key, Hash>::~HashPtrTable()
64     clear();
68 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
70 template<class T, class Key, class Hash>
71 T* HashPtrTable<T, Key, Hash>::remove(iterator& it)
73     T* elemPtr = *it;
74     HashTable<T*, Key, Hash>::erase(it);
75     return elemPtr;
79 template<class T, class Key, class Hash>
80 bool HashPtrTable<T, Key, Hash>::erase(iterator& it)
82     T* elemPtr = *it;
84     if (HashTable<T*, Key, Hash>::erase(it))
85     {
86         if (elemPtr)
87         {
88             delete elemPtr;
89         }
91         return true;
92     }
93     else
94     {
95         return false;
96     }
100 template<class T, class Key, class Hash>
101 void HashPtrTable<T, Key, Hash>::clear()
103     for
104     (
105         iterator iter = this->begin();
106         iter != this->end();
107         ++iter
108     )
109     {
110         delete *iter;
111     }
113     HashTable<T*, Key, Hash>::clear();
117 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
119 template<class T, class Key, class Hash>
120 void HashPtrTable<T, Key, Hash>::operator=(const HashPtrTable<T, Key, Hash>& ht)
122     // Check for assignment to self
123     if (this == &ht)
124     {
125         FatalErrorIn
126         (
127             "HashPtrTable<T, Key, Hash>::operator="
128             "(const HashPtrTable<T, Key, Hash>&)"
129         )   << "attempted assignment to self"
130             << abort(FatalError);
131     }
133     clear();
135     for(const_iterator iter = ht.begin(); iter != ht.end(); ++iter)
136     {
137         insert(iter.key(), new T(**iter));
138     }
142 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
144 } // End namespace Foam
146 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
148 #include "HashPtrTableIO.C"
150 // ************************************************************************* //