-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WordOccurrenceCounter: Implement thread-safe word counting
- Loading branch information
Showing
4 changed files
with
65 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
namespace WordOccurrenceCounter.Tests; | ||
|
||
public class WordCounterTests | ||
{ | ||
[Test] | ||
public void AddToCount_Parallel_ShouldCountCorrectly() | ||
{ | ||
var wordCounter = new WordCounter(); | ||
var words = new[] { "test", "example", "parallel", "test", "example", "test" }; | ||
int iterations = 1000; | ||
|
||
Parallel.For(0, iterations, i => | ||
{ | ||
foreach (var word in words) | ||
{ | ||
wordCounter.AddToCount(word); | ||
} | ||
}); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(wordCounter.GetCount("test"), Is.EqualTo(iterations * 3)); | ||
Assert.That(wordCounter.GetCount("example"), Is.EqualTo(iterations * 2)); | ||
Assert.That(wordCounter.GetCount("parallel"), Is.EqualTo(iterations * 1)); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace WordOccurrenceCounter; | ||
|
||
public interface IWordCounter | ||
{ | ||
void AddToCount(string word); | ||
|
||
int GetCount(string word); | ||
|
||
IReadOnlyDictionary<string, int> GetCounts(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,30 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Collections.Concurrent; | ||
|
||
namespace WordOccurrenceCounter; | ||
|
||
public class WordCounter | ||
public class WordCounter : IWordCounter | ||
{ | ||
private const int InitialValue = 1; | ||
|
||
private readonly ConcurrentDictionary<string, int> _wordCounts; | ||
|
||
public WordCounter() | ||
{ | ||
_wordCounts = new ConcurrentDictionary<string, int>(); | ||
} | ||
|
||
public void AddToCount(string word) | ||
{ | ||
_wordCounts.AddOrUpdate(word, _ => InitialValue, (_, count) => count + 1); | ||
} | ||
|
||
public int GetCount(string word) | ||
{ | ||
return _wordCounts.TryGetValue(word, out var count) ? count : 0; | ||
} | ||
|
||
public IReadOnlyDictionary<string, int> GetCounts() | ||
{ | ||
return _wordCounts; | ||
} | ||
} |