hashedWordList.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2016-2018 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "hashedWordList.H"
29 #include "CStringList.H"
30 
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
34 (
35  const label len,
36  const char** array,
37  bool unique
38 )
39 :
40  wordList(len)
41 {
42  for (label i=0; i < len; ++i)
43  {
44  wordList::operator[](i) = array[i];
45  }
46 
47  rehash(unique);
48 }
49 
50 
51 Foam::hashedWordList::hashedWordList(const char** array, bool unique)
52 :
53  hashedWordList(CStringList::count(array), array, unique)
54 {}
55 
56 
57 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
58 
60 {
61  lookup_.clear();
62 
63  const wordUList& list = *this;
64  const label len = list.size();
65 
66  for (label i=0; i < len; ++i)
67  {
68  lookup_.insert(list[i], i);
69  }
70 }
71 
72 
74 {
75  lookup_.clear();
76 
77  wordList& list = *this;
78  const label len = list.size();
79 
80  label count = 0;
81  for (label i=0; i < len; ++i)
82  {
83  word& item = list[i];
84 
85  if (lookup_.insert(item, i))
86  {
87  if (count != i)
88  {
89  list[count] = std::move(item);
90  }
91  ++count;
92  }
93  }
94 
95  list.resize(count);
96 }
97 
98 
99 // ************************************************************************* //
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::List::resize
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
Foam::wordList
List< word > wordList
A List of words.
Definition: fileName.H:62
Foam::hashedWordList::hashedWordList
hashedWordList()=default
Default construct an empty list.
Foam::hashedWordList
A wordList with hashed named lookup, which can be faster in some situations than using the normal lis...
Definition: hashedWordList.H:54
CStringList.H
hashedWordList.H
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::List< word >
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
Foam::hashedWordList::uniq
void uniq()
Definition: hashedWordList.C:73
Foam::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Foam::hashedWordList::rehash
void rehash() const
Rebuild the lookup hash indices.
Definition: hashedWordList.C:59
Foam::CStringList
An adapter for copying a list of C++ strings into a list of C-style strings for passing to C code tha...
Definition: CStringList.H:69