Comment detail

タブ区切りデータの処理 (Nested Flatten)
何とか。
しかし何か美しくない...
  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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>

namespace
{

class Row
{
  std::vector<std::string> data_;

  friend std::istream& operator>>(std::istream&, Row&);
  friend std::ostream& operator<<(std::ostream&, const Row&);

  public:
  std::size_t find(const std::string& key) const; 

  std::string& operator[](std::size_t i)
  { return data_.at(i); }
  const std::string& operator[](std::size_t i) const
  { return data_.at(i); }

  bool empty() const
  { return data_.empty() || data_.size() == 1 && data_[0] == ""; }
};

std::istream&
operator>>(std::istream& is, Row& r_)
{
  Row r;
  std::string line;
  std::getline(is, line);
  
  std::string::size_type i, o = 0; 
  while ((i = line.find("\t", o)) != std::string::npos)
  {
    r.data_.push_back(line.substr(o, i - o));
    o = i+1;
  }
  r.data_.push_back(line.substr(o));
  std::swap(r, r_);
  return is;
}

std::ostream&
operator<<(std::ostream& os, const Row& r)
{
  std::copy(r.data_.begin(), r.data_.end(), std::ostream_iterator<std::string>(os, "\t"));
  os << "\n";
  return os;
}

std::size_t
Row::find(const std::string& key)
const
{
  std::vector<std::string>::const_iterator it =
    std::find(data_.begin(), data_.end(), key);
  if ( it == data_.end() )
    throw "not found";
  return std::distance(data_.begin(), it);
}

typedef Row Header;
class Table
{
  Header header_;
  std::vector<Row> rows_;

  friend std::istream& operator>>(std::istream&, Table&);
  friend std::ostream& operator<<(std::ostream&, const Table&);

  public:
    void sort_by(std::size_t idx);
    void swap_columns(std::size_t col1, std::size_t col2);
    template <typename Filter>
      void filter(std::size_t idx, Filter f);

  private:
    class comparator
      : public std::binary_function<bool, Row, Row>
    {
      std::size_t col_;
      public:
      comparator(std::size_t col) : col_(col) {}

      bool operator()(const Row& r1, const Row& r2) const
      { return r1[col_] < r2[col_]; }
    };
};

std::istream&
operator>>(std::istream& is, Table& t)
{
  is >> t.header_;

  std::istream_iterator<Row> b(is),e;
  std::copy(b, e, std::back_inserter(t.rows_));
  if (t.rows_.back().empty())
    t.rows_.erase(t.rows_.end() - 1);

  return is;
}

std::ostream&
operator<<(std::ostream& os, const Table& t)
{
  os << t.header_;
  std::copy(t.rows_.begin(), t.rows_.end(), std::ostream_iterator<Row>(os, ""));
  return os;
}

void 
Table::sort_by(std::size_t idx)
{
  comparator comp(idx);
  std::sort(rows_.begin(), rows_.end(), comp);
}

void
Table::swap_columns(std::size_t col1, std::size_t col2)
{
  if ( col1 == col2 ) return;
  std::swap(header_[col1], header_[col2]);
  std::vector<Row>::iterator it = rows_.begin();
  for ( ; it != rows_.end(); ++it )
    std::swap((*it)[col1], (*it)[col2]);
}

template <typename Filter>
void
Table::filter(std::size_t idx, Filter f)
{
  std::vector<Row>::iterator it = rows_.begin();
  for ( ; it != rows_.end(); ++it )
  {
    std::stringstream ss((*it)[idx]);
    typename Filter::result_type v;
    ss >> v;
    v = f(v);
    ss.clear();
    ss << v;
    (*it)[idx] = ss.str();
  }
}

int filter(int i)
{ return i+1; }

} // namespace

int main()
{
  std::istringstream ss(
      "ID\tSurname\tForename\tAge\n"
      "1\tSato\tHanako\t17\n"
      "0\tSuzuki\tTaro\t18\n"
      );

  Table t;
  ss >> t;
  std::cout << t << "\n=====\n";

  t.sort_by(0);
  t.swap_columns(1,2);
  t.filter(3, std::ptr_fun(filter));

  std::cout << t << "\n";
  return 0;
}

Index

Feed

Other

Link

Pathtraq

loading...