Skip to content

CS0436 \ConcurrentHashSet\src\ConcurrentHashSet\NullableAttributes.cs… #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/ConcurrentCollections.sln
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26228.9
# Visual Studio Version 17
VisualStudioVersion = 17.1.32228.430
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConcurrentHashSet", "ConcurrentHashSet\ConcurrentHashSet.csproj", "{58063048-C7B0-4F4D-B27E-1ED5F9789AF7}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConcurrentHashSet", "ConcurrentHashSet\ConcurrentHashSet.csproj", "{58063048-C7B0-4F4D-B27E-1ED5F9789AF7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConcurrentHashSet.Test", "ConcurrentHashSet.Test\ConcurrentHashSet.Test.csproj", "{A559187C-0971-42CD-AE8C-2BC1132965FE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,8 +17,15 @@ Global
{58063048-C7B0-4F4D-B27E-1ED5F9789AF7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{58063048-C7B0-4F4D-B27E-1ED5F9789AF7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{58063048-C7B0-4F4D-B27E-1ED5F9789AF7}.Release|Any CPU.Build.0 = Release|Any CPU
{A559187C-0971-42CD-AE8C-2BC1132965FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A559187C-0971-42CD-AE8C-2BC1132965FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A559187C-0971-42CD-AE8C-2BC1132965FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A559187C-0971-42CD-AE8C-2BC1132965FE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F8839D42-C1EB-4D5A-8C15-D7A32C0EA9E2}
EndGlobalSection
EndGlobal
20 changes: 20 additions & 0 deletions src/ConcurrentHashSet.Test/ConcurrentHashSet.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\ConcurrentHashSet\ConcurrentHashSet.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="stopwords_en_nltk.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
68 changes: 68 additions & 0 deletions src/ConcurrentHashSet.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// See https://aka.ms/new-console-template for more information
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Text;

Console.WriteLine("ConcurrentHashSet test now!");

try
{
var watch1 = new Stopwatch();
watch1.Start();

int total1 = 0;
ConcurrentHashSet<string> hashSet1 = new ConcurrentHashSet<string>();
using (var sr = new StreamReader("stopwords_en_nltk.txt", Encoding.UTF8))
{
string? line = null;
while ((line = sr.ReadLine()) != null)
{
if(!hashSet1.Contains(line))
hashSet1.Add(line);
System.Threading.Interlocked.Add(ref total1, 1);
}
}
watch1.Stop();
Console.WriteLine($"ConcurrentHashSet: stopwords_en_nltk.txt load finished, Count:{hashSet1.Count}, total:{total1}. time elapsed {watch1.ElapsedMilliseconds} ms");

var watch2 = new Stopwatch();
watch2.Start();
int total2 = 0;
ConcurrentHashSet<string> hashSet2 = new ConcurrentHashSet<string>();
System.Threading.Tasks.Parallel.ForEach(File.ReadLines("stopwords_en_nltk.txt", Encoding.UTF8), (line, _, lineNumber) =>
{
if (!hashSet2.Contains(line))
hashSet2.Add(line);
System.Threading.Interlocked.Add(ref total2, 1);
});

watch2.Stop();
Console.WriteLine($"ConcurrentHashSet: stopwords_en_nltk.txt load as parallel finished, Count:{hashSet2.Count}, total:{total2}. time elapsed {watch2.ElapsedMilliseconds} ms");

var watch3 = new Stopwatch();
watch3.Start();
int total3 = 0;
ConcurrentBag<string> bag = new ConcurrentBag<string>();
System.Threading.Tasks.Parallel.ForEach(File.ReadLines("stopwords_en_nltk.txt", Encoding.UTF8), (line, _, lineNumber) =>
{
if (!bag.Contains(line))
bag.Add(line);
System.Threading.Interlocked.Add(ref total3, 1);
});

watch3.Stop();

Console.WriteLine($"ConcurrentBag: stopwords_en_nltk.txt load as parallel finished, Count:{bag.Count}, total:{total3}. time elapsed {watch3.ElapsedMilliseconds} ms");

Console.WriteLine("Press any key to exit.");
}
catch (IOException e)
{
Console.WriteLine(string.Format("{0} load failure, reason: {1}", "stopwords_en_nltk.txt", e.Message));
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

Console.ReadKey();
127 changes: 127 additions & 0 deletions src/ConcurrentHashSet.Test/stopwords_en_nltk.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
i
me
my
myself
we
our
ours
ourselves
you
your
yours
yourself
yourselves
he
him
his
himself
she
her
hers
herself
it
its
itself
they
them
their
theirs
themselves
what
which
who
whom
this
that
these
those
am
is
are
was
were
be
been
being
have
has
had
having
do
does
did
doing
a
an
the
and
but
if
or
because
as
until
while
of
at
by
for
with
about
against
between
into
through
during
before
after
above
below
to
from
up
down
in
out
on
off
over
under
again
further
then
once
here
there
when
where
why
how
all
any
both
each
few
more
most
other
some
such
no
nor
not
only
own
same
so
than
too
very
s
t
can
will
just
don
should
now
30 changes: 20 additions & 10 deletions src/ConcurrentHashSet/ConcurrentHashSet.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Threading;

namespace ConcurrentCollections
namespace System.Collections.Concurrent
{
/// <summary>
/// Represents a thread-safe hash-based unique collection.
Expand Down Expand Up @@ -274,7 +271,7 @@ private ConcurrentHashSet(int concurrencyLevel, int capacity, bool growLockArray
/// <exception cref="OverflowException">The <see cref="ConcurrentHashSet{T}"/>
/// contains too many items.</exception>
public bool Add(T item) =>
AddInternal(item, _comparer.GetHashCode(item), true);
AddInternal(item, true); // , _comparer.GetHashCode(item)

/// <summary>
/// Removes all items from the <see cref="ConcurrentHashSet{T}"/>.
Expand Down Expand Up @@ -322,8 +319,14 @@ public void Clear()
/// a value that has more complete data than the value you currently have, although their
/// comparer functions indicate they are equal.
/// </remarks>
public bool TryGetValue(T equalValue, [MaybeNullWhen(false)] out T actualValue)
public bool TryGetValue(T equalValue, out T? actualValue)
{
if (equalValue == null)
{
actualValue = default;
return false;
}

var hashcode = _comparer.GetHashCode(equalValue);

// We must capture the _buckets field in a local variable. It is set to a new table on each table resize.
Expand Down Expand Up @@ -357,6 +360,9 @@ public bool TryGetValue(T equalValue, [MaybeNullWhen(false)] out T actualValue)
/// <returns>true if an item was removed successfully; otherwise, false.</returns>
public bool TryRemove(T item)
{
if (item == null)
return false;

var hashcode = _comparer.GetHashCode(item);
while (true)
{
Expand Down Expand Up @@ -569,9 +575,9 @@ void ICollection<T>.CopyTo(T[] array, int arrayIndex)

private void InitializeFromCollection(IEnumerable<T> collection)
{
foreach (var item in collection)
foreach (T item in collection)
{
AddInternal(item, _comparer.GetHashCode(item), false);
AddInternal(item, false); // , _comparer.GetHashCode(item)
}

if (_budget == 0)
Expand All @@ -581,8 +587,12 @@ private void InitializeFromCollection(IEnumerable<T> collection)
}
}

private bool AddInternal(T item, int hashcode, bool acquireLock)
private bool AddInternal(T item, bool acquireLock) // , int hashcode
{
if (item == null)
return false;

int hashcode = _comparer.GetHashCode(item);
while (true)
{
var tables = _tables;
Expand Down
12 changes: 6 additions & 6 deletions src/ConcurrentHashSet/ConcurrentHashSet.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyName>ConcurrentCollections</AssemblyName>
<AssemblyName>System.Collections.Concurrent.ConcurrentHashSet</AssemblyName>
<Authors>Bar Arnon</Authors>
<ContinuousIntegrationBuild>True</ContinuousIntegrationBuild>
<Copyright>Copyright 2021 Bar Arnon</Copyright>
Expand All @@ -12,20 +12,20 @@
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<IncludeSource>true</IncludeSource>
<IncludeSymbols>true</IncludeSymbols>
<PackageId>ConcurrentHashSet</PackageId>
<PackageId>System.Collections.Concurrent.ConcurrentHashSet</PackageId>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<PackageTags>concurrency</PackageTags>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<RootNamespace>ConcurrentCollections</RootNamespace>
<RootNamespace>System.Collections.Concurrent</RootNamespace>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<TargetFrameworks>netstandard1.0;netstandard2.0;net461</TargetFrameworks>
<TargetFrameworks>netstandard1.0;netstandard2.0;netstandard2.1;net6</TargetFrameworks>
<Title>ConcurrentHashSet</Title>
<Version>1.2.0</Version>
<Version>1.2.1</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>

</Project>
20 changes: 0 additions & 20 deletions src/ConcurrentHashSet/NullableAttributes.cs

This file was deleted.