aircampro

csv time order sort

Jul 27th, 2025
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.63 KB | Software | 0 0
  1. // read csv lines and sort on the time column specified
  2. //
  3. #include <vector>
  4. #include <iostream>
  5. #include <string>
  6. #include <fstream>
  7. #include <sstream>
  8. #include <string>
  9.  
  10. // fast vector copy
  11. // std::copy(v2.begin(),v2.end(),std::back_inserter(v1));
  12.  
  13. // function to split the csv lines into string fields
  14. //
  15. std::vector<std::string> split(std::string &input, char delimiter) {
  16.   std::istringstream stream(input);
  17.   std::string field;
  18.   std::vector<std::string> result;
  19.   while (getline(stream, field, delimiter)) result.push_back(field);
  20.   return result;
  21. }
  22.  
  23. int main() {
  24.  
  25.   // create a vector of test data
  26.   std::vector<std::string> ss = {"90,0.3,74,43","1,0.3,34,3","1,2.3,34,3","1,7.3,34,3","97,0.3,94,03","1,8.13,34,3","1,8.15,34,53","1,8.05,64,13","1,8.18,74,13","501,8.07,74,13","13,0.37,34,3"};
  27.   std::vector<std::string> out = {};
  28.   ss.reserve(20000);                                                      // reserver the memory for speed
  29.   out.reserve(20000);
  30.  
  31.   auto it2 = out.begin();                                                  // initialize the insert position
  32.   unsigned int line_no = 1;
  33.   const unsigned int time_col = 1;                                         // define the sort column as 1 (starts @ 0)
  34.  
  35.   for ( auto line : ss ) {
  36.     if (line_no == 1) {                                                   // first line
  37.        out.push_back(line);
  38.        ++line_no;
  39.     } else if (line_no == 2) {                                            // second line
  40.        std::vector<std::string> row_list = split(line,',');
  41.        auto time_spec_line = std::stod(row_list.at(time_col));
  42.        std::vector<std::string> out_list = split(out.at(0),',');
  43.        auto time_spec_out = std::stod(out_list.at(time_col));
  44.        if (time_spec_line > time_spec_out) {
  45.            out.push_back(line);
  46.        } else {
  47.            auto it2 = out.begin();
  48.            out.insert(it2, line);
  49.        }
  50.        ++line_no;
  51.     } else {                                                               // next lines starting @ line 3
  52.        std::vector<std::string> row_list = split(line,',');
  53.        auto time_spec_line = std::stod(row_list.at(time_col));
  54.        std::vector<std::string> out_list = split(out.at(0),',');
  55.        auto time_spec_out_front = std::stod(out_list.at(time_col));
  56.        out_list = split(out.at(out.size()-1),',');
  57.        auto time_spec_out_back = std::stod(out_list.at(time_col));
  58.        if ((time_spec_line > time_spec_out_front) && (time_spec_line < time_spec_out_back)) {
  59.            auto d1 = time_spec_line - time_spec_out_front;
  60.            auto d2 = time_spec_out_back - time_spec_line;
  61.            if (d1 < d2) {                                                     // closest to the front time
  62.                auto it3 = out.begin();                                        // point to the strt of the vector list
  63.                ++it3;                                                         // advance 1 record fprward as we already checked it         
  64.                auto length = out.size();
  65.                for (auto i = 1; i < length; ++i) {                            // advance forward through the output records
  66.                    out_list = split(out.at(i),',');
  67.                    time_spec_out_front = std::stod(out_list.at(time_col));
  68.                    if (time_spec_line < time_spec_out_front) {
  69.                        out.insert(it3, line);
  70.                        break;
  71.                    }
  72.                    ++it3;                  
  73.                }           
  74.            } else {                                                          // time read is closer to the back of the out list
  75.                auto length = out.size();
  76.                auto it4 = out.end();                                         // point to the end of the vector
  77.                --it4;                                                        // advance 1 record back already done it
  78.                for (auto i = length-2; i > 0; --i) {                         // advance backward through the output records
  79.                    out_list = split(out.at(i),',');
  80.                    time_spec_out_back = std::stod(out_list.at(time_col));
  81.                    if (time_spec_line > time_spec_out_back) {
  82.                        out.insert(it4, line);
  83.                        break;
  84.                    }
  85.                    --it4;                  
  86.                }
  87.            }
  88.        } else if (time_spec_line <= time_spec_out_front) {
  89.            auto it5 = out.begin();
  90.            out.insert(it5, line);
  91.        } else if (time_spec_line >= time_spec_out_back) {
  92.            out.push_back(line);
  93.        }
  94.        ++line_no;  
  95.     }
  96.   }
  97.  
  98.   // print the output lines sorted numerically in time
  99.   for (auto i : out) {
  100.     std::cout << i << std::endl;
  101.   }
  102.   return 0;
  103. }
Tags: c++ csv
Advertisement
Add Comment
Please, Sign In to add comment