Skip to content

Commit 634fac3

Browse files
committed
2 parents b58be46 + 2191f40 commit 634fac3

File tree

1 file changed

+59
-3
lines changed

1 file changed

+59
-3
lines changed

README.md

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,70 @@ To use Pythonize.Counter.Net in your project, you can install it via NuGet Packa
1414
Install-Package Pythonize.Counter.Net
1515
```
1616

17-
## Usage
18-
Example:
19-
```csharp
17+
## How-to-use
18+
>obviously like collections.Counter
19+
20+
A Counter is a Dictionary for couting objects. Keys are elements and Values are counts.
21+
22+
### Counter<T> Class
23+
24+
The `Counter<T>` class is a generic class that counts the occurrences of items in an enumerable or span of items.
25+
Initializes a new instance of the `Counter<T>` class with the specified enumerable of items.
26+
27+
```csharp
2028
var value = "abacaba";
2129
var counter = new Counter<char>(value);
30+
```
31+
32+
#### Dictionary<T, int> Get()
33+
34+
Gets the counts of items.
35+
36+
```csharp
37+
var counter = new Counter<string>(new[] { "apple", "banana", "apple", "orange", "banana", "banana" });
2238
var counts = counter.Get();
39+
// Output:
40+
// { ["apple": 2], ["banana": 3], ["orange": 1] }
41+
```
42+
43+
#### int Total()
44+
45+
Calculate the sum of the counts.
46+
47+
```csharp
48+
var counter = new Counter<string>(new[] { "apple", "banana", "apple", "orange", "banana", "banana" });
49+
var total = counter.Total();
50+
// Output:
51+
// 6
2352
```
2453

54+
#### IEnumerable<T> Elements()
55+
56+
Returns an enumerable of elements based on the counts of items.
57+
58+
```csharp
59+
var counter = new Counter<string>(new[] { "apple", "banana", "apple", "orange", "banana", "banana" });
60+
var elements = counter.Elements();
61+
// Output:
62+
// { "apple", "banana", "apple", "orange", "banana", "banana" }
63+
```
64+
65+
#### IEnumerable<KeyValuePair<T, int>> MostCommon(int count)
66+
67+
Returns the specified number of most common elements along with their counts from the dictionary.
68+
69+
```csharp
70+
var counter = new Counter<string>(new[] { "apple", "banana", "apple", "orange", "banana", "banana" });
71+
var elements = counter.MostCommon(2);
72+
// Output:
73+
// { ("banana": 3), ("apple": 2) }
74+
```
75+
76+
### Exceptions
77+
78+
- `ArgumentNullException`: thrown when the `value` parameter is null.
79+
- `ArgumentOutOfRangeException`: thrown when the `count` parameter is less than 1.
80+
2581
## Changelog
2682
See all changes with versions [Here](https://github.com/Lukeuke/Pythonize.Counter.Net/blob/main/CHANGELOG.md)
2783

0 commit comments

Comments
 (0)