/
loc.go
102 lines (76 loc) · 1.76 KB
/
loc.go
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
// Copyright 2014-2015 The DevMine Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package anlzr
import "github.com/DevMine/srcanlzr/src"
const (
maxInt64 = int64(^uint64(0) >> 1)
minInt64 = -maxInt64 - 1
)
type LoC struct{}
func (lca LoC) Analyze(p *src.Project, r *Result) error {
r.TotalLoC = p.LoC
var totalFuncs int64
var totalLoCFunc int64
maxLoCFunc := minInt64
minLoCFunc := maxInt64
hist := make(map[int64]int64)
for _, pkg := range p.Packages {
for _, sf := range pkg.SrcFiles {
for _, f := range sf.Funcs {
totalFuncs++
totalLoCFunc += f.LoC
if f.LoC > maxLoCFunc {
maxLoCFunc = f.LoC
}
if f.LoC < minLoCFunc {
minLoCFunc = f.LoC
}
hist[f.LoC] += int64(1)
}
for _, cls := range sf.Classes {
for _, m := range cls.Methods {
totalFuncs++
totalLoCFunc += m.LoC
if m.LoC > maxLoCFunc {
maxLoCFunc = m.LoC
}
if m.LoC < minLoCFunc {
minLoCFunc = m.LoC
}
hist[m.LoC] += int64(1)
}
}
for _, mod := range sf.Traits {
for _, m := range mod.Methods {
totalFuncs++
totalLoCFunc += m.LoC
if m.LoC > maxLoCFunc {
maxLoCFunc = m.LoC
}
if m.LoC < minLoCFunc {
minLoCFunc = m.LoC
}
hist[m.LoC] += int64(1)
}
}
}
}
r.AverageFuncLen = float32(totalLoCFunc) / float32(totalFuncs)
r.MaxFuncLen = maxLoCFunc
r.MinFuncLen = minLoCFunc
r.MedianFuncLen = lca.median(hist)
return nil
}
// median computes the media of a given histogram.
func (lca LoC) median(h map[int64]int64) int64 {
maxVal := minInt64
maxKey := int64(-1)
for k, v := range h {
if v > maxVal {
maxVal = v
maxKey = k
}
}
return maxKey
}