collect_token_statistics.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*=============================================================================
  2. Boost.Wave: A Standard compliant C++ preprocessor library
  3. Sample: Collect token statistics from the analysed files
  4. http://www.boost.org/
  5. Copyright (c) 2001-2010 Hartmut Kaiser. Distributed under the Boost
  6. Software License, Version 1.0. (See accompanying file
  7. LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. =============================================================================*/
  9. #if !defined(COLLECT_TOKEN_STATISTICS_VERSION_HPP)
  10. #define COLLECT_TOKEN_STATISTICS_VERSION_HPP
  11. #include <algorithm>
  12. #include <map>
  13. #include <boost/assert.hpp>
  14. #include <boost/wave/token_ids.hpp>
  15. ///////////////////////////////////////////////////////////////////////////////
  16. class collect_token_statistics
  17. {
  18. enum {
  19. count = boost::wave::T_LAST_TOKEN - boost::wave::T_FIRST_TOKEN
  20. };
  21. public:
  22. collect_token_statistics()
  23. {
  24. std::fill(&token_count[0], &token_count[count], 0);
  25. }
  26. // account for the given token type
  27. template <typename Token>
  28. void operator() (Token const& token)
  29. {
  30. using boost::wave::token_id;
  31. int id = token_id(token) - boost::wave::T_FIRST_TOKEN;
  32. BOOST_ASSERT(id < count);
  33. ++token_count[id];
  34. }
  35. // print out the token statistics in descending order
  36. void print() const
  37. {
  38. using namespace boost::wave;
  39. typedef std::multimap<int, token_id> ids_type;
  40. ids_type ids;
  41. for (unsigned int i = 0; i < count; ++i)
  42. {
  43. ids.insert(ids_type::value_type(
  44. token_count[i], token_id(i + T_FIRST_TOKEN)));
  45. }
  46. ids_type::reverse_iterator rend = ids.rend();
  47. for(ids_type::reverse_iterator rit = ids.rbegin(); rit != rend; ++rit)
  48. {
  49. std::cout << boost::wave::get_token_name((*rit).second) << ": "
  50. << (*rit).first << std::endl;
  51. }
  52. }
  53. private:
  54. int token_count[count];
  55. };
  56. #endif // !defined(COLLECT_TOKEN_STATISTICS_VERSION_HPP)