Skip to content

Commit

Permalink
Simple utilities to track and compute averages and maximums
Browse files Browse the repository at this point in the history
I use these when reading in PSML column data. The average length and
maximum length of a column are passed as configuration to the table that
displays the data. A rendered column will use no more than the column
maximum number of cells, and if that number of cells is not
available (e.g. on a narrower terminal), then the width of the column
will be determined by the column's average in proportion to the other
displayed columns' averages or rendered widths.
  • Loading branch information
gcla committed Jan 17, 2021
1 parent 8ec489f commit c7d7725
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions pcap/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2019-2021 Graham Clark. All rights reserved. Use of this source
// code is governed by the MIT license that can be found in the LICENSE
// file.

package pcap

import (
"github.com/gcla/gowid/gwutil"
)

//======================================================================

type averageTracker struct {
count uint64
total uint64
}

func (a averageTracker) average() gwutil.IntOption {
if a.count == 0 {
return gwutil.NoneInt()
}
return gwutil.SomeInt(int(a.total / a.count))
}

func (a *averageTracker) update(more int) {
a.count += 1
a.total += uint64(more)
}

type maxTracker struct {
cur int
}

func (a maxTracker) max() int {
return a.cur
}

func (a *maxTracker) update(candidate int) {
if candidate > a.cur {
a.cur = candidate
}
}

//======================================================================
// Local Variables:
// mode: Go
// fill-column: 78
// End:

0 comments on commit c7d7725

Please sign in to comment.