本文整理汇总了C#中HashedSet.Contains方法的典型用法代码示例。如果您正苦于以下问题:C# HashedSet.Contains方法的具体用法?C# HashedSet.Contains怎么用?C# HashedSet.Contains使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HashedSet
的用法示例。
在下文中一共展示了HashedSet.Contains方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RandomAddDelete
public void RandomAddDelete()
{
const int SIZE = 50000;
bool[] present = new bool[SIZE];
Random rand = new Random();
HashedSet<int> set1 = new HashedSet<int>();
bool b;
// Add and delete values at random.
for (int i = 0; i < SIZE * 10; ++i)
{
int v = rand.Next(SIZE);
if (present[v])
{
Assert.IsTrue(set1.Contains(v));
b = set1.Remove(v);
Assert.IsTrue(b);
present[v] = false;
}
else
{
Assert.IsFalse(set1.Contains(v));
b = set1.Add(v);
Assert.IsTrue(b);
present[v] = true;
}
}
int count = 0;
foreach (bool x in present)
{
if (x)
{
++count;
}
}
Assert.AreEqual(count, set1.Count);
// Make sure the set has all the correct values, not in order.
foreach (int v in set1)
{
Assert.IsTrue(present[v]);
present[v] = false;
}
// Make sure all were found.
count = 0;
foreach (bool x in present)
{
if (x)
{
++count;
}
}
Assert.AreEqual(0, count);
}
示例2: ContainsShouldReturnCorrectResult
public void ContainsShouldReturnCorrectResult()
{
var set = new HashedSet<string>();
for (int i = 0; i < 20; i++)
{
set.Add(string.Format("pesho #{0}", i % 5));
}
Assert.IsTrue(set.Contains("pesho #3"));
Assert.IsFalse(set.Contains("pesho #13"));
}
示例3: Main
static void Main()
{
HashedSet<int> set = new HashedSet<int>();
set.Add(5);
set.Add(5);
set.Add(6);
set.Add(1);
set.Add(3);
set.Add(3);
set.Add(8);
set.Remove(6);
Console.WriteLine("First set contains 6 - {0}", set.Contains(6));
Console.WriteLine("First set contains 8 - {0}", set.Contains(8));
Console.WriteLine("First set");
foreach (var item in set)
{
Console.Write("{0} ", item);
}
Console.WriteLine();
Console.WriteLine("Second set");
HashedSet<int> set2 = new HashedSet<int>();
set2.Add(13);
set2.Add(3);
set2.Add(1);
foreach (var item in set2)
{
Console.Write("{0} ", item);
}
Console.WriteLine();
HashedSet<int> union = set.UnionWith(set2);
Console.WriteLine("Union: ");
foreach (var item in union)
{
Console.Write("{0} ", item);
}
Console.WriteLine();
HashedSet<int> intersection = set.IntersectWith(set2);
Console.WriteLine("Intersection: ");
foreach (var item in intersection)
{
Console.Write("{0} ", item);
}
Console.WriteLine();
}
示例4: TestContainsMethod
public void TestContainsMethod()
{
var hashedSet = new HashedSet<int>();
Assert.IsTrue(hashedSet.Add(1));
Assert.IsTrue(hashedSet.Add(2));
Assert.IsTrue(hashedSet.Add(3));
Assert.IsTrue(hashedSet.Add(4));
Assert.AreEqual(4, hashedSet.Count);
Assert.IsTrue(hashedSet.Contains(3));
Assert.IsTrue(hashedSet.Contains(4));
Assert.IsFalse(hashedSet.Contains(-5));
Assert.IsFalse(hashedSet.Contains(14));
}
示例5: HashedSetUnionTestSameElementsInBoth
public void HashedSetUnionTestSameElementsInBoth()
{
HashedSet<int> firstSet = new HashedSet<int>();
int firstSetLength = 5;
for (int i = 0; i < firstSetLength; i++)
{
firstSet.Add(i);
}
HashedSet<int> secondSet = new HashedSet<int>();
int secondSetLength = 5;
for (int i = 0; i < secondSetLength; i++)
{
secondSet.Add(i);
}
Assert.AreEqual(firstSetLength, firstSet.Count, "Incorrect set count!");
Assert.AreEqual(secondSetLength, secondSet.Count, "Incorrect set count!");
firstSet.Union(secondSet);
for (int i = 0; i < firstSetLength; i++)
{
Assert.IsTrue(firstSet.Contains(i), "Incorrect union!");
}
Assert.AreEqual(firstSetLength, firstSet.Count, "Incorrect amount of elements after union");
}
示例6: Synchronize
public static void Synchronize(DirectoryInfo source, DirectoryInfo destination, bool smart)
{
if (!destination.Exists)
{
destination.Create();
}
FileInfo[] sources = source.GetFiles();
ISet<string> srcNames = new HashedSet<string>();
foreach (FileInfo fileInfo in sources)
{
srcNames.Add(fileInfo.Name);
}
FileInfo[] dests = destination.GetFiles();
// Delete files not present in source
foreach (FileInfo file in dests)
{
if (!srcNames.Contains(file.Name))
{
try
{
/*
* Try to delete, could fail because windows don't permit a file to be
* deleted while it is in use. If it is the case, the file would be deleted
* when the index is reopened or in the next synchronization.
*/
file.Delete();
}
catch (IOException e)
{
if (log.IsWarnEnabled)
{
log.Warn("Unable to delete file " + file.Name + ", maybe in use per another reader", e);
}
}
}
}
// Copy each file from source
foreach (FileInfo sourceFile in sources)
{
FileInfo destinationFile = new FileInfo(Path.Combine(destination.FullName, sourceFile.Name));
long destinationChanged = destinationFile.LastWriteTime.Ticks/LastWriteTimePrecision;
long sourceChanged = sourceFile.LastWriteTime.Ticks/LastWriteTimePrecision;
if (!smart || destinationChanged != sourceChanged)
{
sourceFile.CopyTo(destinationFile.FullName, true);
}
}
foreach (DirectoryInfo directoryInfo in source.GetDirectories())
{
Synchronize(directoryInfo, new DirectoryInfo(Path.Combine(destination.FullName, directoryInfo.Name)), smart);
}
}
示例7: ContainsShouldReturnTruePersistentlyWhenKeyHasBeenAdded
public void ContainsShouldReturnTruePersistentlyWhenKeyHasBeenAdded()
{
var hashedset = new HashedSet<int>();
for (int i = 0; i < 1000; i++)
{
hashedset.Add(i);
Assert.IsTrue(hashedset.Contains(i));
}
}
示例8: AddShouldResultInKeysContainingAddedKey
public void AddShouldResultInKeysContainingAddedKey()
{
var set = new HashedSet<int>();
int value = 5;
set.Add(value);
bool expected = true;
Assert.AreEqual(expected, set.Contains(value));
}
示例9: ContainsKeyShouldReturnFalsePersistentlyWhenKeyHasNotBeenAdded
public void ContainsKeyShouldReturnFalsePersistentlyWhenKeyHasNotBeenAdded()
{
var hashedset = new HashedSet<int>();
for (int i = 0; i < 2000; i += 2)
{
hashedset.Add(i);
}
for (int i = 1; i < 2000; i += 2)
{
Assert.IsFalse(hashedset.Contains(i));
}
}
示例10: RemoveShouldDeleteElementCorrectly
public void RemoveShouldDeleteElementCorrectly()
{
var set = new HashedSet<string>();
for (int i = 0; i < 20; i++)
{
set.Add(string.Format("pesho #{0}", i % 5));
}
set.Remove("pesho #3");
Assert.AreEqual(4, set.Count);
Assert.IsFalse(set.Contains("pesho #3"));
}
示例11: ClearShouldWorkCorrectly
public void ClearShouldWorkCorrectly()
{
var set = new HashedSet<string>();
for (int i = 0; i < 20; i++)
{
set.Add(string.Format("pesho #{0}", i % 5));
}
set.Clear();
Assert.AreEqual(0, set.Count);
for (int i = 0; i < 4; i++)
{
Assert.IsFalse(set.Contains(string.Format("pesho #{0}", i)));
}
}
示例12: getTrackListing
/// <summary>
/// With explicit sorting.
/// </summary>
public static IList<Track> getTrackListing(ARelation relation, Track.Property sortExpression, SortDirection sortDirection)
{
ICriteria criteria = NHibernateSessionManager.Instance.GetSession().CreateCriteria(typeof(Track));
ISet<string> aliases = new HashedSet<string>();
Junction junction = relation.BinaryRelator == Relator.Binary.AND ? new Conjunction() : new Disjunction() as Junction;
foreach(ARelation relatum in relation.Relata)
TrackSearch_Service.loadJunctions(junction, relatum, aliases, criteria);
criteria.Add(junction);
if(!aliases.Contains(sortExpression.ToString().ToLower()))
criteria.CreateAlias(sortExpression.ToString(), sortExpression.ToString().ToLower());
Order order;
if(sortDirection == SortDirection.Ascending)
order = Order.Asc(String.Format("{0}.{1}", sortExpression.ToString().ToLower(), Track.getChildPropertyAsString(sortExpression)));
else
order = Order.Desc(String.Format("{0}.{1}", sortExpression.ToString().ToLower(), Track.getChildPropertyAsString(sortExpression)));
criteria.AddOrder(order);
return criteria.List<Track>();
}
示例13: OrderedHbmFiles
/// <summary>
/// Returns an <see cref="IList"/> of <c>hbm.xml</c> files in the order that ensures
/// base classes are loaded before their subclass/joined-subclass.
/// </summary>
/// <param name="unorderedClasses">An <see cref="ISet"/> of <see cref="ClassEntry"/> objects.</param>
/// <returns>
/// An <see cref="IList"/> of <see cref="String"/> objects that contain the <c>hbm.xml</c> file names.
/// </returns>
private static ArrayList OrderedHbmFiles(ISet unorderedClasses)
{
// Make sure joined-subclass mappings are loaded after base class
ArrayList sortedList = new ArrayList();
ISet processedClassNames = new HashedSet();
ArrayList processedInThisIteration = new ArrayList();
while (true)
{
foreach (ClassEntry ce in unorderedClasses)
{
if (ce.FullExtends == null || processedClassNames.Contains(ce.FullExtends))
{
// This class extends nothing, or is derived from one of the classes that were already processed.
// Append it to the list since it's safe to process now.
// but only if the list doesn't already contain an entry for this file (each file should only appear once in the list)
if (!CollectionUtils.Contains(sortedList,
delegate(object entry) { return ((ClassEntry)entry).FileName == ce.FileName; }))
{
sortedList.Add(ce);
}
processedClassNames.Add(ce.FullClassName);
processedInThisIteration.Add(ce);
}
}
unorderedClasses.RemoveAll(processedInThisIteration);
if (processedInThisIteration.Count == 0)
{
if (!unorderedClasses.IsEmpty)
{
throw new NHibernate.MappingException(FormatExceptionMessage(unorderedClasses));
}
break;
}
processedInThisIteration.Clear();
}
return sortedList;
}
示例14: RemoveExistingKeyShouldResultInKeysCountOfZero
public void RemoveExistingKeyShouldResultInKeysCountOfZero()
{
var set = new HashedSet<int>();
int value = 5;
set.Add(value);
set.Remove(value);
bool expected = true;
Assert.AreEqual(expected, !set.Contains(value));
}
示例15: RemoveShouldDeleteTheElement
public void RemoveShouldDeleteTheElement()
{
var hashedset = new HashedSet<int>();
hashedset.Add(10);
Assert.IsTrue(hashedset.Contains(10));
var removed = hashedset.Remove(10);
Assert.IsTrue(removed);
}