本文整理汇总了C#中HashSet.Intersect方法的典型用法代码示例。如果您正苦于以下问题:C# HashSet.Intersect方法的具体用法?C# HashSet.Intersect怎么用?C# HashSet.Intersect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HashSet
的用法示例。
在下文中一共展示了HashSet.Intersect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PrintChanges
/// <summary>
/// Locates the changes between the prior and post state of the modules..
/// </summary>
/// <param name="modules_prior">List of the available modules prior to the update.</param>
/// <param name="modules_post">List of the available modules after the update.</param>
private void PrintChanges(List<CkanModule> modules_prior, List<CkanModule> modules_post)
{
var prior = new HashSet<CkanModule>(modules_prior, new NameComparer());
var post = new HashSet<CkanModule>(modules_post, new NameComparer());
var added = new HashSet<CkanModule>(post.Except(prior, new NameComparer()));
var removed = new HashSet<CkanModule>(prior.Except(post, new NameComparer()));
var unchanged = post.Intersect(prior);//Default compare includes versions
var updated = post.Except(unchanged).Except(added).Except(removed).ToList();
// Print the changes.
user.RaiseMessage("Found {0} new modules, {1} removed modules and {2} updated modules.", added.Count(), removed.Count(), updated.Count());
if (added.Count > 0)
{
PrintModules("New modules [Name (CKAN identifier)]:", added);
}
if (removed.Count > 0)
{
PrintModules("Removed modules [Name (CKAN identifier)]:", removed);
}
if (updated.Count > 0)
{
PrintModules("Updated modules [Name (CKAN identifier)]:", updated);
}
}
示例2: run
public bool run()
{
var triangleNumbers = new HashSet<long>();
var pentagonalNumbers = new HashSet<long>();
var hexagonalNumbers = new HashSet<long>();
for (var n = 1L; ; n++)
{
long v1 = n * (n + 1) / 2;
long v2 = n * (3 * n - 1) / 2;
long v3 = n * (2 * n - 1);
triangleNumbers.Add(v1);
pentagonalNumbers.Add(v2);
hexagonalNumbers.Add(v3);
if ((pentagonalNumbers.Contains(v1) && hexagonalNumbers.Contains(v1)) ||
(triangleNumbers.Contains(v2) && hexagonalNumbers.Contains(v2)) ||
(triangleNumbers.Contains(v3) && pentagonalNumbers.Contains(v3)))
{
{
if (triangleNumbers.Intersect(pentagonalNumbers)
.Intersect(hexagonalNumbers).Count() == 3)
{
break;
}
}
}
}
var result =
triangleNumbers.Intersect(pentagonalNumbers)
.Intersect(hexagonalNumbers).OrderBy(v => v).Last();
return result == 1533776805;
}
示例3: Main
static void Main(string[] args)
{
int[] input = Console.ReadLine().Split(' ')
.Select(int.Parse).ToArray();
int firstSetCount = input.First();
int secondSetCount = input.Last();
HashSet<int> firstSet = new HashSet<int>();
HashSet<int> secondSet = new HashSet<int>();
for (int i = 0; i < firstSetCount; i++)
{
firstSet.Add(int.Parse(Console.ReadLine()));
}
for (int i = 0; i < secondSetCount; i++)
{
secondSet.Add(int.Parse(Console.ReadLine()));
}
List<int> intersect = firstSet.Intersect(secondSet).ToList();
intersect.ForEach(a => Console.Write(a + " "));
}
示例4: UpdateReservedWordsInDialect
public void UpdateReservedWordsInDialect()
{
var reservedDb = new HashSet<string>();
var configuration = TestConfigurationHelper.GetDefaultConfiguration();
var dialect = Dialect.Dialect.GetDialect(configuration.Properties);
var connectionHelper = new ManagedProviderConnectionHelper(configuration.Properties);
connectionHelper.Prepare();
try
{
var metaData = dialect.GetDataBaseSchema(connectionHelper.Connection);
foreach (var rw in metaData.GetReservedWords())
{
reservedDb.Add(rw.ToLowerInvariant());
}
}
finally
{
connectionHelper.Release();
}
var sf = (ISessionFactoryImplementor) configuration.BuildSessionFactory();
SchemaMetadataUpdater.Update(sf);
var match = reservedDb.Intersect(sf.Dialect.Keywords);
Assert.That(match, Is.EquivalentTo(reservedDb));
}
示例5: Main
public static void Main()
{
HashSet<int> mySet = new HashSet<int>();
mySet.Add(10);
mySet.Add(11);
mySet.Add(12);
mySet.Add(13);
mySet.Add(19);
mySet.Add(14);
mySet.Add(15);
HashSet<int> myOtherSet = new HashSet<int>();
myOtherSet.Add(10);
myOtherSet.Add(11);
myOtherSet.Add(12);
myOtherSet.Add(13);
myOtherSet.Add(14);
myOtherSet.Add(15);
myOtherSet.Add(16);
//var union = mySet.Union(myOtherSet);
//foreach (var item in union)
//{
// Console.WriteLine(item);
//}
var intersect = mySet.Intersect(myOtherSet);
foreach (var item in intersect)
{
Console.WriteLine(item);
}
}
示例6: simularity
public static double simularity(HashSet<string> setA, HashSet<string> setB)
{
double interCount = setA.Intersect(setB).Count();
if (interCount == 0) return 0;
double unionCount = setA.Union(setB).Count();
return interCount / unionCount;
}
示例7: HashSetIntersects
/// <summary>
/// Does intersect using hash sets.
/// </summary>
/// <param name="list1"></param>
/// <param name="list2"></param>
/// <param name="toLower"></param>
/// <param name="factor"></param>
/// <returns></returns>
public static bool HashSetIntersects(IEnumerable<string> list1, IEnumerable<string> list2, bool toLower, int factor)
{
var set1 = new HashSet<string>(list1);
var set2 = new HashSet<string>(list2);
if (toLower)
{
return set1.Select(x => x.ToLower()).Intersect(set2.Select(x => x.ToLower())).Count() >= factor;
}
return set1.Intersect(set2).Count() >= factor;
}
示例8: TestRecognizeSpirals
public void TestRecognizeSpirals()
{
Bitmap b = MakeBitmap(100, 100, (Graphics g) =>
{
int top = 20;
int bottom = 75;
for (int i = 20; i <= 60; i += 10 )
{
g.DrawLine(Pens.Black, i, top, i, bottom);
if (i != 80) {
if ((i / 10) % 2 == 0) {
top += 10;
g.DrawLine(Pens.Black, i, bottom, i + 10, bottom);
}
else {
bottom -= 10;
g.DrawLine(Pens.Black, i, top, i + 10, top);
}
}
}
g.FillPolygon(Brushes.Black, new Point[] {
new Point(30, 20),
new Point(80, 20),
new Point(80, 45) });
g.DrawPolygon(Pens.Black, new Point[] { //Make sure that we draw the borders too
new Point(30, 20),
new Point(80, 20),
new Point(80, 45) });
g.FillPolygon(Brushes.Black, new Point[] {
new Point(30, 80),
new Point(80, 80),
new Point(80, 55) });
g.DrawPolygon(Pens.Black, new Point[] {
new Point(30, 80),
new Point(80, 80),
new Point(80, 55) });
});
HashSet<Rectangle> expected = new HashSet<Rectangle>();
HashSet<Rectangle> blobs = new HashSet<Rectangle>(from blob in MangaParser.Graphics.Raster.Graphics.IdentifyBlobs(b)
select blob.BoundingBox);
expected.Add(new Rectangle(20, 20, 51, 56));
expected.Add(new Rectangle(30, 20, 51, 26));
expected.Add(new Rectangle(30, 55, 51, 26));
Assert.AreEqual(expected.Count, blobs.Intersect(expected).Count());
}
示例9: SimpleRouteRule
/// <summary>
/// 创建一个简单路由规则
/// </summary>
/// <param name="name">规则名称</param>
/// <param name="urlPattern">URL 模式</param>
/// <param name="routeValues">静态/默认路由值</param>
/// <param name="queryKeys">可用于 QueryString 的参数</param>
internal SimpleRouteRule( string name, string urlPattern, IDictionary<string, string> routeValues, string[] queryKeys )
{
Name = name;
if ( queryKeys == null )
{
LimitedQueries = false;
queryKeys = new string[0];
}
else
LimitedQueries = true;
DataTokens = new RouteValueDictionary();
var match = urlPatternRegex.Match( urlPattern );
if ( !match.Success )
throw new FormatException( "URL模式格式不正确" );
_paragraphes = match.Groups["paragraph"].Captures.Cast<Capture>().Select( c => c.Value ).ToArray();
_urlPattern = urlPattern;
_staticValues = new Dictionary<string, string>( routeValues, StringComparer.OrdinalIgnoreCase );
_routeKeys = new HashSet<string>( _staticValues.Keys, StringComparer.OrdinalIgnoreCase );
_dynamics = new HashSet<string>( _paragraphes.Where( p => p.StartsWith( "{" ) && p.EndsWith( "}" ) ).Select( p => p.Substring( 1, p.Length - 2 ) ), StringComparer.OrdinalIgnoreCase );
foreach ( var key in _dynamics )
{
if ( _routeKeys.Contains( key ) )
throw new FormatException( "URL模式格式不正确,包含重复的动态参数名或动态参数名与预设路由键重复" );
_routeKeys.Add( key );
}
if ( _routeKeys.Intersect( queryKeys, StringComparer.OrdinalIgnoreCase ).Any() )
throw new FormatException( "URL模式格式不正确,动态参数或预设路由键与可选查询字符串名重复" );
_queryKeys = new HashSet<string>( queryKeys, StringComparer.OrdinalIgnoreCase );
_allKeys = new HashSet<string>( _routeKeys, StringComparer.OrdinalIgnoreCase );
_allKeys.UnionWith( _queryKeys );
}
示例10: Main
public static void Main(string[] args)
{
HashSet<int> first = new HashSet<int>();
first.Add(1);
first.Add(2);
first.Add(3);
Console.Write("First set: ");
foreach (var item in first)
{
Console.Write(item.Key + " ");
}
Console.WriteLine();
HashSet<int> second = new HashSet<int>();
second.Add(4);
second.Add(1);
second.Remove(1);
Console.Write("Second set: ");
foreach (var item in second)
{
Console.Write(item.Key + " ");
}
Console.WriteLine();
first.Union(second);
Console.Write("Sets union: ");
foreach (var item in first)
{
Console.Write(item.Key + " ");
}
Console.WriteLine();
//second.Add(1);
first.Intersect(second);
Console.Write("Sets intersection: ");
foreach (var item in first)
{
Console.Write(item.Key + " ");
}
Console.WriteLine();
}
示例11: GetMatchesByKeysAndTown
public IEnumerable<Person> GetMatchesByKeysAndTown(string[] param)
{
var matchedPersons = names[param[0]];
HashSet<Person> result = new HashSet<Person>(matchedPersons);
for (int i = 1; i < param.Length; i++)
{
matchedPersons = names[param[i]];
var hashedSet = new HashSet<Person>(matchedPersons);
result = new HashSet<Person>(result.Intersect(hashedSet));
}
return result;
}
示例12: InternalHandle
protected override void InternalHandle(Output result, string args, DependencyGraph graph)
{
var projctsFiltered = FilterLibs(graph, args);
IDictionary<Library, int> components;
graph.StronglyConnectedComponents(out components);
var circularDependencies = components.Select(c => new { Proj = c.Key, Group = c.Value })
.GroupBy(c => c.Group)
.Where(g => g.Count() > 1);
bool found = false;
foreach (var g in circularDependencies)
{
var libs = g.Select(i => i.Proj)
.ToList();
var projsSet = new HashSet<Library>(libs);
if (!projsSet.Intersect(projctsFiltered)
.Any())
continue;
found = true;
libs.Sort(Library.NaturalOrdering);
result.AppendLine("Circular dependency:");
result.IncreaseIndent();
foreach (var lib in libs)
{
var proj = lib as Project;
if (proj != null)
result.AppendLine("{0} (project path: {1})", GetName(proj), proj.ProjectPath);
else
result.AppendLine(GetName(lib));
}
result.DecreaseIndent();
result.AppendLine();
}
if (!found)
result.AppendLine("No circular dependency found");
}
示例13: HashSetShouldIntersectElementCorrectly
public void HashSetShouldIntersectElementCorrectly()
{
var set1 = new HashSet<int>();
var set2 = new HashSet<int>();
set1.Add(1);
set1.Add(2);
set1.Add(3);
set2.Add(1);
set2.Add(5);
set2.Add(6);
var intersect = set1.Intersect(set2);
Assert.IsTrue(intersect.Find(1));
Assert.AreEqual(1, intersect.Count);
}
示例14: Main
public static void Main()
{
var firstSet = new HashSet<int>();
var secondSet = new HashSet<int>();
firstSet.Add(1);
firstSet.Add(2);
firstSet.Add(7);
secondSet.Add(7);
secondSet.Add(13);
secondSet.Add(19);
firstSet.Remove(2);
var unionSet = firstSet.Union(secondSet);
Console.WriteLine("Union set = {0}", unionSet);
var intersectSet = firstSet.Intersect(secondSet);
Console.WriteLine("Intersect set = {0}", intersectSet);
}
示例15: OptionsViewModel_ReconcilePlatformsWithBackendAsync_ReturnsInvalidPlatforms
public async Task OptionsViewModel_ReconcilePlatformsWithBackendAsync_ReturnsInvalidPlatforms()
{
int numberOfPlatforms = 6;
var serverPlatformNames = new string[] { "valid1", "valid2" };
var apiPort = ApiPortWhichReturnsPlatforms(serverPlatformNames);
var invalidLocalPlatforms = (from n in Enumerable.Range(1, numberOfPlatforms)
select new TargetPlatform { Name = string.Format("invalid{0}", n), Selected = n % 2 == 0 })
.ToList();
var viewModel = new TestableOptionsViewModel(apiPort, invalidLocalPlatforms);
var result = await viewModel.ReconcilePlatformsWithBackendAsync();
var localHash = new HashSet<TargetPlatform>(invalidLocalPlatforms);
var resultHash = new HashSet<TargetPlatform>(result);
Assert.IsTrue(localHash.Intersect(resultHash).Count() == localHash.Count);
}