Skip to content

Commit

Permalink
SimpleCounter iterators are in insertion order
Browse files Browse the repository at this point in the history
  • Loading branch information
nh13 authored Nov 1, 2021
1 parent b851fd3 commit b862308
Showing 1 changed file with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ object SimpleCounter {

/**
* Super-simple class for counting occurrences of any kind of object. Will return
* zero for any item that has not been counted yet.
* zero for any item that has not been counted yet. The iterator and all traversal
* methods of this class visit elements in the order they were inserted.
*/
class SimpleCounter[T]extends Iterable[(T, Long)] {
/** Creates the map in which we store the counts. */
protected def makeMap(): mutable.Map[T, Long] = new mutable.HashMap()
/** Creates the map in which we store the counts, with iteration in insertion order. */
protected def makeMap(): mutable.Map[T, Long] = new mutable.LinkedHashMap()

private val counts: mutable.Map[T, Long] = makeMap().withDefaultValue(0L)

Expand Down

0 comments on commit b862308

Please sign in to comment.