FileIO.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // Created by wrede on 19.04.16.
  3. //
  4. #ifndef GBMOT_FILEIO_H
  5. #define GBMOT_FILEIO_H
  6. #include <string>
  7. #include <fstream>
  8. #include <vector>
  9. #include "../core/ObjectData.h"
  10. #include "../graph/Definitions.h"
  11. namespace util
  12. {
  13. typedef std::vector<std::vector<std::vector<double>>> Vector3d;
  14. typedef std::vector<std::vector<double>> Vector2d;
  15. typedef std::unordered_map<std::string, double> ValueMap;
  16. typedef std::vector<ValueMap> ValueMapVector;
  17. /**
  18. * Utility class for file in- and output.
  19. */
  20. class FileIO
  21. {
  22. public:
  23. /**
  24. * Reads a CSV file and stores the values in a 3D array.
  25. * The first dimension is the first value of each row, used as a
  26. * index to bundle multiple rows with the same first value into a
  27. * single vector.
  28. * The second dimension is the row in the row bundle.
  29. * The third dimension is the value in that row.
  30. * @param values The 3D array of values to store the read values in
  31. * @param file_name The name of the file to read
  32. * @param delimiter The value delimiter of the file
  33. */
  34. static void ReadCSV(Vector3d& values,
  35. const std::string& file_name,
  36. char delimiter = ';');
  37. /**
  38. * Reads a CSV file and stores the values in a 2D array.
  39. * The first dimension is the row and the second the value in that row.
  40. * @param values The 2D array of values to store the read values in
  41. * @param file_name The name of the file to read
  42. * @param delimiter The value delimiter of the file
  43. */
  44. static void ReadCSV(Vector2d& values,
  45. const std::string& file_name,
  46. char delimiter = ';');
  47. /**
  48. * Lists all file names in the given folder.
  49. * @param folder The folder to look into
  50. * @param file_names The name of the files in the folder
  51. * @param sort True, if the files should be sorted alphabetically
  52. */
  53. static void ListFiles(const std::string& folder,
  54. std::vector<std::string>& file_names,
  55. bool sort = true);
  56. /**
  57. * Writes the specified graph into a CSV file with an format readable by
  58. * Matlab.
  59. *
  60. * @param graph The graph to write
  61. * @param file_name The name of the file to write
  62. */
  63. static void WriteCSVMatlab(DirectedGraph& graph,
  64. const std::string& file_name);
  65. /**
  66. * Writes the specified multi predecessor map into a CSV format
  67. * readable by Matlab to display all paths in the corresponding graph.
  68. *
  69. * @param map The multi predecessor map to extract the paths from
  70. * @param origin The origin, this is the vertex where all paths end
  71. * @param file_name The name of the file to write
  72. */
  73. static void WriteCSVMatlab(MultiPredecessorMap& map,
  74. Vertex& origin,
  75. const std::string& file_name);
  76. /**
  77. * Reads a CSV file.
  78. * The first line of the CSV file is a header specifying the keys.
  79. * The values are stored with their specified key into one map per line.
  80. *
  81. * @param values A vector of maps to store the key-value pairs into
  82. * @param file_name The name of the file to read
  83. * @param delimiter The value delimiter of the file
  84. */
  85. static void ReadCSV(ValueMapVector& values,
  86. const std::string& file_name,
  87. char delimiter = ',');
  88. /**
  89. * Reads a CSV file.
  90. * The header specifies the keys.
  91. * The values are stored with their specified key into one map per line.
  92. *
  93. * @param values A vector of maps to store the key-value pairs into
  94. * @param header A string containing the keys separated by the delimiter
  95. * @param file_name The name of the file to read
  96. * @param delimiter The value delimiter of the file
  97. */
  98. static void ReadCSV(ValueMapVector& values,
  99. const std::string& header,
  100. const std::string& file_name,
  101. char delimiter = ',');
  102. };
  103. }
  104. #endif //GBMOT_FILEIO_H