source: trunk/lib/Stats.h @ 1321

Last change on this file since 1321 was 1321, checked in by Peter Johansson, 11 years ago

updating includes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.8 KB
Line 
1#ifndef _theplu_svndigest_stats_
2#define _theplu_svndigest_stats_
3
4// $Id: Stats.h 1321 2010-11-28 23:41:31Z peter $
5
6/*
7  Copyright (C) 2005 Peter Johansson
8  Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
9  Copyright (C) 2010 Peter Johansson
10
11  This file is part of svndigest, http://dev.thep.lu.se/svndigest
12
13  svndigest is free software; you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation; either version 3 of the License, or
16  (at your option) any later version.
17
18  svndigest is distributed in the hope that it will be useful, but
19  WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  General Public License for more details.
22
23  You should have received a copy of the GNU General Public License
24  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
25*/
26
27#include "LineTypeParser.h"
28#include "Vector.h"
29
30#include <subversion-1/svn_types.h>
31
32#include <map>
33#include <istream>
34#include <set>
35#include <string>
36#include <vector>
37
38namespace theplu{
39namespace svndigest{
40
41  ///
42  /// Class taking care of statistics from svn.
43  ///
44  class Stats
45  {
46  public:
47    ///
48    /// @brief Default Constructor
49    ///
50    explicit Stats(const std::string& path);
51
52    /**
53       \brief Destructor
54    */
55    virtual ~Stats(void);
56
57    ///
58    /// @return set of authors
59    ///
60    const std::set<std::string>& authors(void) const;
61
62    ///
63    /// \return number of code lines for \a user for latest revision.
64    ///
65    unsigned int code(const std::string& user="all") const; 
66
67
68    ///
69    /// \return number of comments lines for \a user for latest revision.
70    ///
71    unsigned int comments(const std::string& user="all") const; 
72
73    ///
74    /// \return number of empty lines for \a user for latest revision.
75    ///
76    unsigned int empty(const std::string& user="all") const; 
77
78    ///
79    /// \return revison node was modified
80    ///
81    svn_revnum_t last_changed_rev(void) const;
82
83    ///
84    /// \return number of lines for \a user for latest revision
85    ///
86    unsigned int lines(const std::string& user="all") const; 
87
88    /**
89       Load object from a stream.
90
91       \a latest_ver is set to true if cache_file is latest version;
92       otherwise it is set to false.
93
94       \return revision the cache represents - 0 if failed
95     */
96    svn_revnum_t load_cache(std::istream&, bool& latest_ver);
97
98    /**
99       Do the parsing for \a path. Revisions from \a rev will be parsed.
100    */
101    void parse(const std::string& path, svn_revnum_t rev=0);
102
103    /**
104       Send Stats to a stream.
105     */
106    void print(std::ostream&) const;
107
108    ///
109    /// @brief Clear all statistics
110    ///
111    void reset(void); 
112
113    /**
114       set vectors in stats_ to size \a rev
115    */
116    void resize(svn_revnum_t rev);
117
118    ///
119    /// \return latest revision for whole project
120    ///
121    svn_revnum_t revision(void) const;
122
123    /**
124       \return resulting Stats
125    */
126    Stats& operator+=(const Stats&);
127
128    /**
129       \return number of lines for \a author and \a linetype from
130       revision \a rev.
131
132       \throw if \a author does not exist
133     */
134    size_t operator()(int linetype, std::string author, svn_revnum_t rev) const;
135
136  protected:
137    typedef std::map<std::string, SumVector> Author2Vector;
138    typedef Author2Vector::iterator A2VIter;
139    typedef Author2Vector::const_iterator A2VConstIter;
140
141    std::vector<Author2Vector> stats_; // from linetype to a2v
142
143    /**
144     */
145    void add(const std::vector<std::map<std::string, SparseVector> >& data);
146
147    void add_author(std::string);
148    void add_authors(std::set<std::string>::const_iterator, 
149                     std::set<std::string>::const_iterator);
150
151    // references to data
152    inline Author2Vector& code_stats(void) 
153    { return stats_[LineTypeParser::code]; }
154    inline Author2Vector& comment_stats(void) 
155    { return stats_[LineTypeParser::comment]; }
156    inline Author2Vector& copyright_stats(void) 
157    { return stats_[LineTypeParser::copyright]; }
158    inline Author2Vector& other_stats(void) 
159    { return stats_[LineTypeParser::other]; }
160    inline Author2Vector& comment_or_copy_stats(void) 
161    { return stats_[LineTypeParser::comment_or_copy]; }
162    inline Author2Vector& total_stats(void) 
163    { return stats_[LineTypeParser::total]; }
164
165    // const references to data
166    inline const Author2Vector& code_stats(void) const
167    { return stats_[LineTypeParser::code]; }
168    inline const Author2Vector& comment_stats(void) const
169    { return stats_[LineTypeParser::comment]; }
170    inline const Author2Vector& copyright_stats(void) const
171    { return stats_[LineTypeParser::copyright]; }
172    inline const Author2Vector& other_stats(void) const
173    { return stats_[LineTypeParser::other]; }
174    inline const Author2Vector& comment_or_copy_stats(void) const
175    { return stats_[LineTypeParser::comment_or_copy]; }
176    inline const Author2Vector& total_stats(void) const
177    { return stats_[LineTypeParser::total]; }
178
179    /**
180       add range [\a first, \a last) to \a map
181     */
182    void map_add(Author2Vector::const_iterator first, 
183                 Author2Vector::const_iterator last,
184                 Author2Vector& map);
185
186    std::set<std::string> authors_;
187
188    const SumVector& get_vector(const Author2Vector&, 
189                                const std::string& user) const;
190  private:
191    /// one liner used in cache file to validate that cache file was
192    /// created current configuration
193    std::string config_code_;
194
195    void add(SumVector& vec, svn_revnum_t rev, unsigned int n);
196
197    /**
198       Parse statistics for fundamental categories, i.e., code,
199       comment, empty, and copyright for each author. Ignore revisions
200       earlier than \a first_rev.
201     */
202    virtual void do_parse(const std::string&, svn_revnum_t first_rev)=0;
203   
204    /// load cache file version 7
205    svn_revnum_t load_cache7(std::istream&);
206    /// load cache file version 8
207    svn_revnum_t load_cache8(std::istream&);
208
209    // Change this string if cache format is changed in such a way
210    // that all old cache files are obsolete.
211    inline std::string cache_check_str(void) const 
212    {return "CACHE FILE VERSION 7";} 
213
214    void calc_all(void);
215    void calc_comment_or_copy(void);
216    void calc_total(void);
217    unsigned int get_back(const Author2Vector&, std::string user) const;
218    void load(std::istream& is, Author2Vector& m);
219    /**
220       Finds the largets element by iterating through the entire
221       vector. Inherited classes should implement their own version
222       when it is possible to get the largest element in faster than
223       in linear time.
224
225       \return the largest largest element in \a v.
226    */
227    virtual unsigned int max_element(const SumVector& v) const; 
228
229    void print(std::ostream& os, const Author2Vector& m) const;
230
231    svn_revnum_t revision_; // Should be the latest revision for whole project
232    svn_revnum_t last_changed_rev_; // Should be the latest revision for file
233
234    // using compiler generated copy constructor
235    //Stats(const Stats&);
236    // no assignment
237    Stats& operator=(const Stats&);
238
239    friend class StatsPlotter;
240  };
241}} // end of namespace svndigest end of namespace theplu
242
243#endif
Note: See TracBrowser for help on using the repository browser.