Comment detail

コード中の文字の頻度分析 (Nested Flatten)
アプリケーションのソースじゃなくて、
cygwin g++ の C++ ヘッダファイル群(全218個)を分析してみました。

  1:0x20  548301 (0.20355)
  2:   e  189982 (0.07053)
  3:   t  166220 (0.06171)
  4:   _  144935 (0.05380)
  5:   r  117461 (0.04361)
  6:   a  113395 (0.04210)
  7:   i  111175 (0.04127)
  8:   s  103785 (0.03853)
  9:   o   99888 (0.03708)
 10:   n   98172 (0.03644)
 11: 0xa   81784 (0.03036)
 12:   l   62672 (0.02327)
 13:   c   61995 (0.02301)
 14:   p   55391 (0.02056)
 15:   u   44881 (0.01666)
<以下省略>

スペースや改行は置いておくとして。
上位10位以内で、int や iterator が
(もうちょっと広げるとconst_iteratorも)
作れるのが興味深いところかと。

"_" が多いのはヘビ記法とローカル用識別子の影響かな。

括弧の類を見てみると、
"(", ")" はそれぞれ24,25位(0.9%),
templateにも使用する"<", ">" は それぞれ 38,33位 (0.4~0.5%),
"{", "}" に至っては 44,45位(0.26%) となっていました。

意外に括弧使ってないのね。。

ちなみに最下位は "$" (8件) でしたが、コメントの中かな...
  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
#include <map>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <functional>
#include <fstream>
#include <stdexcept>
#include <numeric>
#include <cctype>
#include <iterator>
#include <utility>

typedef std::map<char, int> freq_t;
typedef std::vector< std::pair<char, int> > freqv_t;

struct calc_freq
: std::unary_function< freq_t, char* >
{
  freq_t operator()(char* filename) const
  {
    std::ifstream ifs(filename);
    if ( !ifs )
      throw std::runtime_error(std::string("failed to open : ")+filename);

    freq_t f;
    while ( ifs )
    {
      int c = ifs.get();
      if (c >= 0) ++f[c];
    }

    return f;
  }
};

struct merge_freq
: std::binary_function<freq_t, freq_t, freq_t>
{
  freq_t& operator()(freq_t& lhs, const freq_t& rhs) const
  {
    for ( freq_t::const_iterator it = rhs.begin();
        it != rhs.end(); ++it )
      lhs[it->first] += it->second;

    return lhs;
  }
};

struct count_total
: public std::binary_function< int, int, freq_t::value_type >
{
  int& operator()(int& total, const freq_t::value_type& v) const
  {
    return total += v.second;
  }
};

template<class C>
struct freq_print_each
: std::unary_function< void, typename C::value_type >
{
  std::size_t total_;
  mutable std::size_t i_;
  freq_print_each<C>(std::size_t total) : total_(total), i_(0) {}

  void operator()(const typename C::value_type& v) const
  {
    std::cout << std::setw(3) << ++i_ << ":" << std::setw(4);
    if ( !isprint(v.first) || isspace(v.first) )
      std::cout << std::showbase << std::hex
                 << static_cast<int>(v.first) << std::dec;
    else
      std::cout << v.first;

    std::cout << std::setw(8)
      << v.second << " ("
      << std::setprecision(5) << std::fixed
      << static_cast<double>(v.second)/total_
      << ")\n";
  }
};

template <class C>
struct sorter
: std::binary_function<bool, typename C::value_type, typename C::value_type>
{
  typedef typename C::value_type value_type;
  bool operator()(const value_type& lhs, const value_type& rhs) const
  {
    return lhs.second > rhs.second;
  }
};

int main(int c, char** v)
{
  try {
    std::vector< freq_t > freqs;
    std::transform(v+1, v+c, std::back_inserter(freqs), calc_freq());

    const freq_t all_freq = std::accumulate(freqs.begin(), freqs.end(), freq_t(), merge_freq());
    const int total = std::accumulate(all_freq.begin(), all_freq.end(), 0, count_total());

    freqv_t all_freq2;
    all_freq2.reserve(all_freq.size());

    std::copy(all_freq.begin(), all_freq.end(), std::back_inserter(all_freq2));
    std::sort(all_freq2.begin(), all_freq2.end(), sorter<freqv_t>());

    std::for_each(all_freq2.begin(), all_freq2.end(), freq_print_each<freqv_t>(total));
  }
  catch (const std::exception& e) {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}

Index

Feed

Other

Link

Pathtraq

loading...