Skip to content

Commit

Permalink
regenerate readme file using cargo-readme
Browse files Browse the repository at this point in the history
  • Loading branch information
coriolinus committed May 19, 2020
1 parent d368fcc commit 871da3e
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Counter
# counter

Counter counts recurrent elements of iterables. It is based on [the Python implementation](https://docs.python.org/3.5/library/collections.html#collections.Counter).

The struct [`Counter`](struct.Counter.html) is the entry-point type for this module.

## Examples

### Just count an iterable
Expand All @@ -14,6 +16,12 @@ let counts_counts = char_counts.values().collect::<Counter<_>>();

### Update a count

```rust
let mut counts = "aaa".chars().collect::<Counter<_>>();
counts[&'a'] += 1;
counts[&'b'] += 1;
```

```rust
let mut counts = "able babble table babble rabble table able fable scrabble"
.split_whitespace().collect::<Counter<_>>();
Expand All @@ -25,6 +33,14 @@ let other_counts = "scrabble cabbie fable babble"
let difference = counts - other_counts;
```

### Get items with keys

```rust
let counts = "aaa".chars().collect::<Counter<_>>();
assert_eq!(counts[&'a'], 3);
assert_eq!(counts[&'b'], 0);
```

### Get the most common items

`most_common_ordered()` uses the natural ordering of keys which are `Ord`.
Expand Down Expand Up @@ -58,6 +74,15 @@ counter.remove(&'-');
assert!(counter == "aabbcc".chars().collect::<Counter<_>>());
```

Note that `Counter<T, N>` itself implements `Index`. `Counter::index` returns a reference to a `zero` value for missing keys.

```rust
let counter = "aaa".chars().collect::<Counter<_>>();
assert_eq!(counter[&'b'], 0);
// panics
// assert_eq!((*counter)[&'b'], 0);
```

## Advanced Usage

### Count any iterable which is `Hash + Eq`
Expand Down Expand Up @@ -110,3 +135,5 @@ let counter: Counter<_, i8> = "abbccc".chars().collect();
let expected: HashMap<char, i8> = [('a', 1), ('b', 2), ('c', 3)].iter().cloned().collect();
assert!(counter.into_map() == expected);
```

License: MIT

0 comments on commit 871da3e

Please sign in to comment.