As the description of the NamedEnum in OpenFOAM in NamedEnum.H:
1 2
Description Initialise the NamedEnum HashTable from the staticlist of names.
In the previous code study (the devolatilization model), this class has been used. In this blog, this class will be studied more in details. In phaseProperties.H, there is an public object defined in class phaseProperties as:
1 2
//- Corresponding word representations for phase type enumerations staticconst NamedEnum<phaseType, 4> phaseTypeNames;
template<classEnum, unsignedintnEnum> Foam::NamedEnum<Enum, nEnum>::NamedEnum() : HashTable<unsignedint>(2*nEnum) { for (unsignedint enumI = 0; enumI < nEnum; ++enumI) { if (!names[enumI] || names[enumI][0] == '\0') // '\0' means a null charactor { stringList goodNames(enumI); // initilize with a size of enumI
for (unsignedint i = 0; i < enumI; ++i) { goodNames[i] = names[i]; }
FatalErrorInFunction << "Illegal enumeration name at position " << enumI << endl << "after entries " << goodNames << ".\n" << "Possibly your NamedEnum<Enum, nEnum>::names array" << " is not of size " << nEnum << endl << abort(FatalError); } insert(names[enumI], enumI); } }
Here, it should be noted that the NamedEnum class is a sub class of HashTable class. In this case the Enum is phaseType and the nEnum is 4. Firstly, the HashTable class is initialized:
The goodNames is initialized with the size of the enumI, which is a number indicates where the loop is, due to before this value, the whole list is consisting of good “names”. The insert function is defined in the HashTableI.H:
resize(2*tableSize_); } } elseif (protect) { // Found - but protected from overwriting // this corresponds to the STL 'insert' convention #ifdef FULLDEBUG if (debug) { InfoInFunction << "Cannot insert " << key << " already in hash table\n"; } #endif returnfalse; } else { // Found - overwrite existing entry // this corresponds to the Perl convention hashedEntry* ep = new hashedEntry(key, existing->next_, newEntry);
// Replace existing element - within list or insert at the head if (prev) { prev->next_ = ep; } else { table_[hashIdx] = ep; }