本文整理汇总了C#中HashSet.First方法的典型用法代码示例。如果您正苦于以下问题:C# HashSet.First方法的具体用法?C# HashSet.First怎么用?C# HashSet.First使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HashSet
的用法示例。
在下文中一共展示了HashSet.First方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
/* The words of the dictionary this evil hangman example works with are supplied in the linear array below.
* For a more sophisticated implementation loading from an external file is obviously possible, but the idea here
* was to provide a simple solution to the problem, so beginner programmers could understand how they could solve
* it themselves.
*/
string[] dict = {"bakalava", "balamata", "balerina", "balirina", "baniceta", "kalotina", "kolibata", "korubata"};
HashSet<string> words = new HashSet<string>(dict);
char[] seq = {'l', 'r', 'i', 'o', 'e', 'n', 'm', 'k', 'v', 't', 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'p', 'r', 's', 'u', 'w', 'x', 'y', 'z'};
HashSet<char> toGuess = new HashSet<char>(seq);
Random rand = new Random();
Console.WriteLine("Pick a word: (1-" + words.Count + ")");
int ind = int.Parse(Console.ReadLine());
Console.WriteLine("The word you chose is " + answer + ". Let's see whether the computer can guess it...");
answer = dict[ind - 1];
guessed.Add(answer[0]);
guessed.Add(answer[answer.Length - 1]);
while (words.Count != 1)
{
words.RemoveWhere(Remover);
PrintGuessed(guessed);
Console.WriteLine(string.Join(", ", words));
guessed.Add(toGuess.First());
toGuess.Remove(toGuess.First());
}
Console.WriteLine("The word is: " + words.First());
Console.ReadLine();
}
示例2: Execute
public void Execute()
{
var mask = new NodeMask();
mask.Label = "Cycles";
mask.IsShowMask = true;
try
{
if (!myPresentation.Graph.Nodes.Any())
{
return;
}
var unvisited = new HashSet<Node>(myPresentation.Graph.Nodes);
unvisited.RemoveWhere(n => n.In.Count == 0 || n.Out.Count == 0);
while (unvisited.Count > 0)
{
var current = unvisited.First();
unvisited.Remove(current);
foreach (var node in FindCycles(unvisited, current, new HashSet<Node> { current }))
{
mask.Set(node);
}
}
}
finally
{
var module = myPresentation.GetModule<INodeMaskModule>();
module.Push(mask);
}
}
示例3: TransitiveClosure
public IEnumerable<State> TransitiveClosure (
Func<SimpleTransition, bool> selector,
Func<SimpleTransition, bool> cancel)
{
var done = new HashSet<State> ();
var work = new HashSet<State> ();
work.Add (this);
while (work.Any ()) {
var q = work.First ();
work.Remove (q);
done.Add (q);
if (q.Delta == null) {
continue;
}
var ts = q.Delta
.Where (t => selector == null || selector (t))
.ToArray ();
if (cancel != null && ts.Any (cancel)) {
return null;
}
foreach (var qn in ts.Select (t => t.Next)) {
if (!done.Contains (qn)) {
work.Add (qn);
}
}
}
return done;
}
示例4: LongestConsecutiveSequenceLength
/// <summary>
/// Find Longest Consequitive Sequence
/// </summary>
/// <param name="arr">Source Array</param>
/// <returns>Length of longest consecutive numbers</returns>
public static int LongestConsecutiveSequenceLength(int[] arr)
{
HashSet<int> allElements = new HashSet<int>(arr);
int maxConsecutiveLength = 1;
while (allElements.Count > 0)
{
int firstValue = allElements.First();
int leftElement = firstValue;
int currentLength = 1;
while (allElements.Contains(leftElement - 1))
{
allElements.Remove(leftElement - 1);
leftElement--;
currentLength++;
}
int rightElement = firstValue;
while (allElements.Contains(rightElement + 1))
{
allElements.Remove(rightElement + 1);
rightElement++;
currentLength++;
}
if (currentLength > maxConsecutiveLength)
{
maxConsecutiveLength = currentLength;
}
allElements.Remove(firstValue);
}
return maxConsecutiveLength;
}
示例5: PrintGuessed
// This method prints the guessed letters from the guessed hash and then reveals the guessed positionfs of the sought
// word, whilst still keeping the non-guessed ones hidden.
public static void PrintGuessed(HashSet<char> input)
{
HashSet<char> h = new HashSet<char>(input);
Console.Write("Guessed so far:");
while (h.Count != 0)
{
Console.Write(" " + h.First());
h.Remove(h.First());
}
Console.Write("\n Hangman: ");
for (int i = 0; i < answer.Length; i++)
{
if (input.Contains(answer[i])) Console.Write(answer[i]); else Console.Write('_');
}
Console.Write("\n\n");
}
示例6: BuildNode
public override void BuildNode (ITreeBuilder treeBuilder, object dataObject, NodeInfo nodeInfo)
{
//modules do no have names/IDs, but genrally the only reason they exist
//is because they have additional, optional dependencies
//so find the dependencies that are not referenced in other modules
//and use one as the label
var module = (ModuleDescription)dataObject;
var deps = new HashSet<string> ();
foreach (Dependency dep in module.Dependencies) {
deps.Add (dep.Name);
}
foreach (ModuleDescription other in module.ParentAddinDescription.AllModules) {
if (other == module) {
continue;
}
foreach (Dependency dep in other.Dependencies) {
deps.Remove (dep.Name);
}
}
if (deps.Count > 0) {
nodeInfo.Label = deps.First ().Split (new[] { ' '})[0];
} else {
nodeInfo.Label = "Module";
}
}
示例7: Main
private static void Main()
{
var input = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
var n = input[0];
var coord0 = new Coord(input[1], input[2]);
var rgcoord = new HashSet<Coord>();
for (var i = 0; i < n; i++)
{
input = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
rgcoord.Add(new Coord(input[0], input[1]));
}
var d = 0;
while (rgcoord.Any())
{
d++;
var coord = rgcoord.First();
var vX = coord.x - coord0.x;
var vY = coord.y - coord0.y;
foreach (var coordT in rgcoord.ToList())
{
if (vY*(coordT.x - coord0.x) == vX*(coordT.y - coord0.y))
rgcoord.Remove(coordT);
}
}
Console.WriteLine(d);
}
示例8: FeatureInteraction
public FeatureInteraction(HashSet<XgbTreeNode> interaction, double gain, double cover, double pathProbability, double depth, double treeIndex, double fScore = 1)
{
SplitValueHistogram = new SplitValueHistogram();
List<string> features = interaction.OrderBy(x => x.Feature).Select(y => y.Feature).ToList();
Name = string.Join("|", features);
Depth = interaction.Count - 1;
Gain = gain;
Cover = cover;
FScore = fScore;
FScoreWeighted = pathProbability;
AverageFScoreWeighted = FScoreWeighted / FScore;
AverageGain = Gain / FScore;
ExpectedGain = Gain * pathProbability;
TreeIndex = treeIndex;
TreeDepth = depth;
AverageTreeIndex = TreeIndex / FScore;
AverageTreeDepth = TreeDepth / FScore;
HasLeafStatistics = false;
if (Depth == 0)
{
SplitValueHistogram.AddValue(interaction.First().SplitValue);
}
}
示例9: FillBackGround
public static Bitmap FillBackGround(Point e, Bitmap bmpSource, int partition,Color fillColor)
{
int c;
Bitmap bmp = new Bitmap(bmpSource);
HashSet<Point> pts = new HashSet<Point>();
HashSet<Point> result = new HashSet<Point>();
pts.Add(e);
result.Add(e);
while (pts.Count > 0)
{
Point p = pts.First();
int x = p.X;
int y = p.Y;
c = bmpSource.GetPixel(x, y).R;
bmp.SetPixel(x, y, fillColor);
for (int i = -1; i < 2; i++)
for (int j = -1; j < 2; j++)
{
int a = x + i;
int b = y + j;
if (a > -1 && a < bmp.Width && b > -1 && b < bmp.Height && bmpSource.GetPixel(a, b).R > c - partition && bmpSource.GetPixel(a, b).R < c + partition && !result.Contains(new Point(a, b)))
{
pts.Add(new Point(a, b));
result.Add(new Point(a, b));
}
}
pts.Remove(p);
}
return bmp;
}
示例10: CalculateBodyAndPivots
private void CalculateBodyAndPivots()
{
var parentCfg = Scope.Parent.LocalCfg;
var cflow = new List<ControlFlowBlock>();
var pivots = new List<ControlFlowBlock>();
var todo = new HashSet<ControlFlowBlock>{Head};
while (todo.IsNotEmpty())
{
var v = todo.First();
cflow.Add(v);
todo.Remove(v);
var h_pivots = Scope.Hierarchy().SelectMany(s => s.Pivots);
if (h_pivots.Contains(v))
{
pivots.Add(v);
}
else
{
var inEdges = parentCfg.TreeVedges(null, v);
var innerEdges = parentCfg.Edges(cflow, cflow);
var rootEdge = parentCfg.Vedge(Root, v);
inEdges.Except(innerEdges).Except(rootEdge).AssertEmpty();
var outEdges = parentCfg.Vedges(v, null);
var pending = outEdges.Select(e => e.Target).Where(v1 => !cflow.Contains(v1));
pending.ForEach(v1 => todo.Add(v1));
}
}
Body = cflow.Except(pivots).ToReadOnly();
Pivots = pivots.ToReadOnly();
}
示例11: getConnectedPicture
public static HashSet<Point> getConnectedPicture(Point e, Bitmap bmpSource)
{
Color c = bmpSource.GetPixel(e.X, e.Y);
Bitmap bmp = new Bitmap(bmpSource.Width, bmpSource.Height);
HashSet<Point> pts = new HashSet<Point>();
HashSet<Point> result = new HashSet<Point>();
pts.Add(e);
result.Add(e);
while (pts.Count > 0)
{
Point p = pts.First();
int x = p.X;
int y = p.Y;
bmp.SetPixel(x, y, c);
for (int i = -1; i < 2; i++)
for (int j = -1; j < 2; j++)
{
int a = x + i;
int b = y + j;
if (a > -1 && a < bmp.Width && b > -1 && b < bmp.Height && bmpSource.GetPixel(a, b) == c && bmp.GetPixel(a, b) != c)
{
pts.Add(new Point(a, b));
result.Add(new Point(a, b));
}
}
pts.Remove(p);
}
return result;
}
示例12: FindBest
public int[] FindBest()
{
var remaining = new HashSet<Point>(_cities);
var first = remaining.First();
var route = new List<Point> { first };
remaining.Remove(first);
var numericRoute = new List<int>{_cities.IndexOf(first)};
var distance = 0.0d;
while (remaining.Any())
{
var shortest = double.MaxValue;
Point next = null;
foreach (var p in remaining)
{
var d = Distance(route.Last(), p);
if (d < shortest)
{
shortest = d;
next = p;
}
}
route.Add(next);
numericRoute.Add(_cities.IndexOf(next));
remaining.Remove(next);
distance += shortest;
}
distance += Distance(route.First(), route.Last());
Console.WriteLine("Distance calculated in closestneighbour: " + distance);
return numericRoute.ToArray();
}
示例13: DefineContact
/// <summary>
/// When the tessellated solid is sliced at the specified plane, the contact surfaces are
/// described by the return ContactData object. This is a non-destructive function typically
/// used to find the shape and size of 2D surface on the prescribed plane..
/// </summary>
/// <param name="plane">The plane.</param>
/// <param name="ts">The ts.</param>
/// <returns>ContactData.</returns>
/// <exception cref="System.Exception">Contact Edges found that are not contained in loop.</exception>
public static ContactData DefineContact(Flat plane, TessellatedSolid ts)
{
var vertexDistancesToPlane = new double[ts.NumberOfVertices];
for (int i = 0; i < ts.NumberOfVertices; i++)
vertexDistancesToPlane[i] = ts.Vertices[i].Position.dotProduct(plane.Normal) - plane.DistanceToOrigin;
// the edges serve as the easiest way to identify where the solid is interacting with the plane.
// Instead of a foreach, the while loop lets us look ahead to known edges that are irrelevant.
var edgeHashSet = new HashSet<Edge>(ts.Edges);
// Contact elements are constructed and then later arranged into loops. Loops make up the returned object, ContactData.
var straddleContactElts = new List<ContactElement>();
var inPlaneContactElts = new List<CoincidentEdgeContactElement>();
while (edgeHashSet.Any())
{
// instead of the foreach, we have this while statement and these first 2 lines to enumerate over the edges.
var edge = edgeHashSet.First();
edgeHashSet.Remove(edge);
var toDistance = vertexDistancesToPlane[edge.To.IndexInList];
var fromDistance = vertexDistancesToPlane[edge.From.IndexInList];
if (StarMath.IsNegligible(toDistance) && StarMath.IsNegligible(fromDistance))
ContactElement.MakeInPlaneContactElement(plane, edge, edgeHashSet, vertexDistancesToPlane,
inPlaneContactElts);
else if ((toDistance > 0 && fromDistance < 0)
|| (toDistance < 0 && fromDistance > 0))
straddleContactElts.Add(new ThroughFaceContactElement(plane, edge, toDistance));
}
foreach (var contactElement in inPlaneContactElts)
{
// next, we find any additional vertices that just touch the plane but don't have in-plane edges
// to facilitate this we negate all vertices already captures in the inPlaneContactElts
vertexDistancesToPlane[contactElement.StartVertex.IndexInList] = double.NaN;
vertexDistancesToPlane[contactElement.EndVertex.IndexInList] = double.NaN;
}
for (int i = 0; i < ts.NumberOfVertices; i++)
{
if (!StarMath.IsNegligible(vertexDistancesToPlane[i])) continue;
var v = ts.Vertices[i];
PolygonalFace negativeFace, positiveFace;
if (ThroughVertexContactElement.FindNegativeAndPositiveFaces(plane, v, vertexDistancesToPlane,
out negativeFace, out positiveFace))
straddleContactElts.Add(new ThroughVertexContactElement(v, negativeFace, positiveFace));
}
straddleContactElts.AddRange(inPlaneContactElts);
var loops = new List<Loop>();
var numberOfTries = 0;
while (straddleContactElts.Any() && numberOfTries < straddleContactElts.Count)
{
// now build loops from stringing together contact elements
var loop = FindLoop(plane, straddleContactElts, vertexDistancesToPlane);
if (loop != null)
{
Debug.WriteLine(loops.Count + ": " + loop.MakeDebugContactString() + " ");
loops.Add(loop);
numberOfTries = 0;
}
else numberOfTries++;
}
if (straddleContactElts.Any()) Debug.WriteLine("Contact Edges found that are not contained in loop.");
return new ContactData(loops);
}
示例14: QueryTerm_bases_hashing_on_escaped_value
public void QueryTerm_bases_hashing_on_escaped_value() {
var t1 = new QueryTerm("a", "x", false, false);
var t2 = new QueryTerm("a", "y", true, false);
var set = new HashSet<QueryTerm>();
set.Add(t1);
set.Add(t2);
Assert.AreEqual(1, set.Count);
Assert.AreEqual("a", set.First().Escaped);
}
示例15: ControllerNameByClassConvention
public void ControllerNameByClassConvention()
{
var testedType = typeof (TestClassEndingController);
var result = GetScanner().Scan(testedType).ToArray();
var expectation = typeof(TestClassEndingController).Name.Substring(0, typeof(TestClassEndingController).Name.Length - 10);
var results = new HashSet<string>(result.Select(s => s.ControllerName));
Assert.That(results.Count, Is.EqualTo(1));
Assert.That(results.First(), Is.EqualTo(expectation));
}