File: loopFileAscii.h

package info (click to toggle)
groops 0%2Bgit20250907%2Bds-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 11,140 kB
  • sloc: cpp: 135,607; fortran: 1,603; makefile: 20
file content (116 lines) | stat: -rw-r--r-- 3,877 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/***********************************************/
/**
* @file loopFileAscii.h
*
* @brief Loop over list of strings from files.
*
* @author Torsten Mayer-Guerr
* @author Sebastian Strasser
* @date 2017-01-27
*
*/
/***********************************************/

#ifndef __GROOPS_LOOPFILEASCII__
#define __GROOPS_LOOPFILEASCII__

// Latex documentation
#ifdef DOCSTRING_Loop
static const char *docstringLoopFileAscii = R"(
\subsection{FileAscii}
Loop over list of strings from files.
)";
#endif

/***********************************************/

#include "base/import.h"
#include "files/fileStringTable.h"
#include "classes/loop/loop.h"
#include <unordered_set>

/***** CLASS ***********************************/

/** @brief Loop over list of strings from files.
* @ingroup loopGroup
* @see Loop */
class LoopFileAscii : public Loop
{
  std::string nameString, nameIndex, nameCount;
  std::vector<std::string> strings;

public:
  LoopFileAscii(Config &config);

  UInt count() const override {return strings.size();}
  Bool iteration(VariableList &varList) override;
};

/***********************************************/
/***** Inlines *********************************/
/***********************************************/

inline LoopFileAscii::LoopFileAscii(Config &config)
{
  try
  {
    std::vector<FileName> fileName;
    UInt startIndex;
    UInt countElements = MAX_UINT;
    Bool sort;
    Bool removeDuplicates;

    readConfig(config, "inputfile",          fileName,         Config::MUSTSET,  "",           "simple ASCII file with strings (separated by whitespace)");
    readConfig(config, "sort",               sort,             Config::DEFAULT,  "0",          "sort entries alphabetically (ascending)");
    readConfig(config, "removeDuplicates",   removeDuplicates, Config::DEFAULT,  "0",          "remove duplicate entries (order is preserved)");
    readConfig(config, "startIndex",         startIndex,       Config::DEFAULT,  "0",          "start at element startIndex (counting from 0)");
    readConfig(config, "count",              countElements,    Config::OPTIONAL, "",           "use count elements (default: use all)");
    readConfig(config, "variableLoopString", nameString,       Config::OPTIONAL, "loopString", "name of the variable to be replaced");
    readConfig(config, "variableLoopIndex",  nameIndex,        Config::OPTIONAL, "",           "variable with index of current iteration (starts with zero)");
    readConfig(config, "variableLoopCount",  nameCount,        Config::OPTIONAL, "",           "variable with total number of iterations");
    readConfigCondition(config);
    if(isCreateSchema(config)) return;

    for(UInt i=0; i<fileName.size(); i++)
    {
      std::vector<std::string> str;
      readFileStringList(fileName.at(i), str);
      strings.insert(strings.end(), str.begin(), str.end());
    }

    if(sort)
      std::sort(strings.begin(), strings.end());

    if(removeDuplicates)
    {
      std::unordered_set<std::string> set;
      auto end = std::copy_if(strings.begin(), strings.end(), strings.begin(), [&set](auto &s) {return set.insert(s).second;});
      strings.erase(end, strings.end());
    }

    strings.erase(strings.begin(), strings.begin()+std::min(startIndex, strings.size()));
    strings.erase(strings.begin()+std::min(countElements, strings.size()), strings.end());
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

inline Bool LoopFileAscii::iteration(VariableList &varList)
{
  if(index() >= count())
    return FALSE;

  if(!nameString.empty()) varList.setVariable(nameString, strings.at(index()));
  if(!nameIndex.empty())  varList.setVariable(nameIndex,  index());
  if(!nameCount.empty())  varList.setVariable(nameCount,  count());

  return checkCondition(varList);
}

/***********************************************/

#endif