本文整理汇总了C#中Stack.Clear方法的典型用法代码示例。如果您正苦于以下问题:C# Stack.Clear方法的具体用法?C# Stack.Clear怎么用?C# Stack.Clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stack
的用法示例。
在下文中一共展示了Stack.Clear方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetWork
private Dictionary<int, string> GetWork()
{
TimeSpan hold = TimeSpan.FromMinutes(90);
Dictionary<int, string> all = new Dictionary<int, string>();
var messages = Queue.GetMessages(32, hold).ToList();
Stack<Task> tasks = new Stack<Task>(2000);
while (messages.Count > 0)
{
foreach (var message in messages)
{
string s = message.AsString;
JObject json = JObject.Parse(s);
int curId = json["cantonCommitId"].ToObject<int>();
if (!all.ContainsKey(curId))
{
all.Add(curId, s);
}
else
{
Console.WriteLine("Dupe id!");
}
tasks.Push(Queue.DeleteMessageAsync(message));
if (all.Count % 10000 == 0)
{
Console.WriteLine(all.Count);
}
}
// stop if we aren't maxing out
if (messages.Count == 32)
{
messages = Queue.GetMessages(32, hold).ToList();
}
else
{
messages = new List<CloudQueueMessage>();
}
if (tasks.Count > 2000)
{
Task.WaitAll(tasks.ToArray());
tasks.Clear();
}
}
Task.WaitAll(tasks.ToArray());
tasks.Clear();
return all;
}
示例2: DumpHumanReadable
public static void DumpHumanReadable(Transaction tx, string path, Page start)
{
using (var writer = File.CreateText(path))
{
var stack = new Stack<Page>();
stack.Push(start);
writer.WriteLine("Root page #{0}",start.PageNumber);
while (stack.Count > 0)
{
var currentPage = stack.Pop();
if (currentPage.IsLeaf)
{
writer.WriteLine();
writer.WriteLine("Page #{0}, NumberOfEntries = {1}, Flags = {2} (Leaf), Used: {3} : {4}", currentPage.PageNumber,currentPage.NumberOfEntries,currentPage.Flags, currentPage.SizeUsed, currentPage.CalcSizeUsed());
if(currentPage.NumberOfEntries <= 0)
writer.WriteLine("Empty page (tree corrupted?)");
for (int nodeIndex = 0; nodeIndex < currentPage.NumberOfEntries;nodeIndex++)
{
var node = currentPage.GetNode(nodeIndex);
var key = currentPage.GetNodeKey(node);
writer.WriteLine("Node #{0}, Flags = {1}, {4} = {2}, Key = {3}, Entry Size: {5}", nodeIndex, node->Flags, node->DataSize, MaxString(key.ToString(), 25), node->Flags == NodeFlags.Data ? "Size" : "Page",
SizeOf.NodeEntry(node));
}
writer.WriteLine();
}
else if(currentPage.IsBranch)
{
writer.WriteLine();
writer.WriteLine("Page #{0}, NumberOfEntries = {1}, Flags = {2} (Branch), Used: {3} : {4}", currentPage.PageNumber, currentPage.NumberOfEntries, currentPage.Flags, currentPage.SizeUsed, currentPage.SizeUsed);
var key = new Slice(SliceOptions.Key);
for (int nodeIndex = 0; nodeIndex < currentPage.NumberOfEntries; nodeIndex++)
{
var node = currentPage.GetNode(nodeIndex);
writer.WriteLine("Node #{2}, {0} / to page #{1}, Entry Size: {3}", GetBranchNodeString(nodeIndex, key, currentPage, node), node->PageNumber, nodeIndex,
SizeOf.NodeEntry(node));
}
for (int nodeIndex = 0; nodeIndex < currentPage.NumberOfEntries; nodeIndex++)
{
var node = currentPage.GetNode(nodeIndex);
if (node->PageNumber < 0 || node->PageNumber > tx.State.NextPageNumber)
{
writer.Write("Found invalid reference to page #{0}", currentPage.PageNumber);
stack.Clear();
break;
}
var child = tx.GetReadOnlyPage(node->PageNumber);
stack.Push(child);
}
writer.WriteLine();
}
}
}
}
示例3: check
static bool check(string arg)
{
char[] arrChar = arg.ToCharArray();
if ((arrChar[0] == ')') || (arrChar.Length % 2 != 0)) return false;
else
{
Stack<char> st = new Stack<char>();
foreach(char c in arrChar)
{
if (c == ')' && st.Count != 0) st.Pop();
else st.Push(c);
}
if (st.Count == 0) { st.Clear(); return true; }
else { st.Clear(); return false; }
}
}
示例4: Main
static void Main()
{
Stack<int> myStack = new Stack<int>();
myStack.Push(1);
myStack.Push(2);
myStack.Push(3);
myStack.Push(4);
myStack.Push(5);
myStack.Push(6);
int[] stackToArray = myStack.ToArray();
Console.Write("{");
for (int i = 0; i < stackToArray.Length-1; i++)
{
Console.Write(stackToArray[i] + ", ");
}
Console.Write(stackToArray[stackToArray.Length-1]+"}");
Console.WriteLine();
Console.WriteLine("the count="+myStack.Count);
Console.WriteLine();
Console.WriteLine(myStack.Peek());
Console.WriteLine(myStack.Pop());
Console.WriteLine(myStack.Peek());
Console.WriteLine(myStack.Pop());
Console.WriteLine(myStack.Peek());
Console.WriteLine("the count="+myStack.Count);
Console.WriteLine(myStack.Contains(111));
myStack.Clear();
Console.WriteLine("the count="+myStack.Count);
}
示例5: FindPath
public static IEnumerable<String> FindPath(string start, string end, HashSet<string> words)
{
//build graph in O(N^2)
var graph = BuildGraph(words);
//bfs in O(V+E)
var edgeTo = BuildEdgeTo(graph, start, end);
//build a stack with path going from end to start
var path = new Stack<string>();
string word = end;
while (!word.Equals(start))
{
path.Push(word);
if (!edgeTo.ContainsKey(word))
{
path.Clear();
break;
}
word = edgeTo[word];
}
path.Push(start);
return path;
}
示例6: Main
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
int[] arr = Console.ReadLine().Split(' ').Select(int.Parse).Distinct().ToArray();
bool flag = false;
int possibleSubsets = (int)Math.Pow(2, arr.Length);
for (int mask = 0; mask <= possibleSubsets; mask++)
{
Stack<int> subset = new Stack<int>();
for (int i = 0; i < arr.Length; i++)
{
if ((mask & (1 << i)) != 0)
{
subset.Push(arr[i]);
}
}
if (subset.Sum() == n && subset.Count != 0)
{
Console.WriteLine(String.Join(" + ", subset) + " = " + n);
flag = true;
}
subset.Clear();
}
if (!flag)
{
Console.WriteLine("No matching subsets.");
}
}
示例7: BruteForce
static long BruteForce(int times)
{
long i = 2 * 3 * 5 * 7;
var primeFactors = GetPrimeFactors(100);
var stack = new Stack<long>();
while (true)
{
if (HasDistinctPrimeFactors(i, primeFactors, times))
{
stack.Push(i);
}
else
{
stack.Clear();
}
if (stack.Count == times)
{
break;
}
i++;
}
var count = stack.Count - 1;
for (int s = 0; s < count; s++)
{
stack.Pop();
}
return stack.Pop();
}
示例8: Apply
public void Apply(Menu m)
{
m.Name = Name;
m.Items = new List<MenuItem>();
var j = new Stack<MenuItem>();
foreach (var i in Items)
{
var a = new MenuItem {Name = i.Name, Type = i.Type, Location = i.Location};
if (i.Indent == 0)
{
j.Clear();
j.Push(a);
m.AddItem(a);
}
else
{
// rewind the that stack until we reach the parent node. creates the
// effect the stack always represents a 'stair-step' pattern
while (j.Count > i.Indent) j.Pop();
j.Peek().AddItem(a);
j.Push(a);
}
}
}
示例9: PosTest2
public bool PosTest2()
{
bool retVal = true;
string errorDesc;
int[] operands = new int[] { };
TestLibrary.TestFramework.BeginScenario("PosTest2: remove all the elements from an empty stack.");
try
{
Stack<int> operandStack = new Stack<int>((IEnumerable<int>)operands);
operandStack.Clear();
if (0 != operandStack.Count)
{
errorDesc = "Failed to clear stack " + this.GetStackData(operandStack);
TestLibrary.TestFramework.LogError("P002.1", errorDesc);
retVal = false;
}
}
catch (Exception e)
{
errorDesc = "Unexpected exception: " + e;
TestLibrary.TestFramework.LogError("P002.2", errorDesc);
retVal = false;
}
return retVal;
}
示例10: LargestRectangleArea
private int LargestRectangleArea(int[] height)
{
int[] answers = new int[height.Length];
Stack<int> stack = new Stack<int>();
for (int i = 0; i < height.Length; i++)
{
while (stack.Count > 0 && height[stack.Peek()] >= height[i])
{
stack.Pop();
}
int leftEdge = stack.Count > 0 ? stack.Peek() : -1;
answers[i] = i - leftEdge - 1;
stack.Push(i);
}
int max = 0;
stack.Clear();
for (int i = height.Length - 1; i >= 0; i--)
{
while (stack.Count > 0 && height[stack.Peek()] >= height[i])
{
stack.Pop();
}
int rightEdge = stack.Count > 0 ? stack.Peek() : height.Length;
answers[i] += rightEdge - i - 1;
stack.Push(i);
int current = height[i] * (answers[i] + 1);
max = Math.Max(current, max);
}
return max;
}
示例11: Main
static void Main(string[] args)
{
Stack<int> numbers = new Stack<int>();
numbers.Push(4);
numbers.Push(5);
numbers.Push(5);
numbers.Push(6);
numbers.Push(7);
numbers.Push(2);
numbers.Push(22);
numbers.Push(33);
numbers.Push(14);
Console.WriteLine(numbers.Count);
Console.WriteLine(numbers.Pop());
Console.WriteLine(numbers.Pop());
Console.WriteLine(numbers.Peek());
Console.WriteLine(numbers.Count);
Console.WriteLine(numbers.Contains(5));
Console.WriteLine(numbers.Contains(55));
numbers.Clear();
numbers.Push(44);
Console.WriteLine(numbers.Count);
}
示例12: Main
static void Main()
{
var test = new Stack<int>();
test.Push(1);
Console.WriteLine(string.Join(" ", test.ToArray()));
test.Push(2);
Console.WriteLine(string.Join(" ", test.ToArray()));
test.Push(3);
Console.WriteLine(string.Join(" ", test.ToArray()));
test.Push(4);
Console.WriteLine(string.Join(" ", test.ToArray()));
test.Push(5);
Console.WriteLine(string.Join(" ", test.ToArray()));
test.Push(6);
Console.WriteLine(string.Join(" ", test.ToArray()));
test.Push(7);
Console.WriteLine(string.Join(" ", test.ToArray()));
Console.WriteLine("The stack count: {0}", test.Count);
test.Pop();
test.Pop();
Console.WriteLine(string.Join(" ", test.ToArray()));
Console.WriteLine("The stack count: {0}", test.Count);
Console.WriteLine(test.Peek());
Console.WriteLine(string.Join(" ", test.ToArray()));
Console.WriteLine("The stack count: {0}", test.Count);
test.Clear();
Console.WriteLine(string.Join(" ", test.ToArray()));
Console.WriteLine("The stack count: {0}", test.Count);
//test.Pop(); //if uncommented this line should throw exception
}
示例13: MoveTo
public bool MoveTo(int destinationX, int destinationY, int requiredDistance = 0)
{
if (destinationX == _client.PlayerX && destinationY == _client.PlayerY)
{
return true;
}
Node node = FindPath(_client.PlayerX, _client.PlayerY, _client.IsOnGround, _client.IsSurfing, destinationX, destinationY, requiredDistance);
if (node != null)
{
Stack<Direction> directions = new Stack<Direction>();
while (node.Parent != null)
{
if (!node.Parent.IsSurfing && node.IsSurfing)
{
directions.Clear();
_client.UseSurfAfterMovement();
}
else
{
directions.Push(node.FromDirection);
}
node = node.Parent;
}
while (directions.Count > 0)
{
_client.Move(directions.Pop());
}
return true;
}
return false;
}
示例14: Main
static void Main()
{
var stack = new Stack<int>();
stack.Push(1);
Console.WriteLine(stack);
stack.Push(2);
Console.WriteLine(stack);
stack.Push(3);
Console.WriteLine(stack);
Console.WriteLine("Last: {0}", stack.Peek());
Console.WriteLine("Count: {0}", stack.Count);
Console.WriteLine("Contains 2: {0}", stack.Contains(2));
Console.WriteLine("Capacity: {0}", stack.Capacity);
stack.TrimExcess();
Console.WriteLine("Capacity: {0}", stack.Capacity);
while (stack.Count != 0)
Console.WriteLine("Pop: {0}", stack.Pop());
Console.WriteLine("Count: {0}", stack.Count);
try
{
stack.Clear();
stack.Pop();
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
}
try
{
stack.Clear();
stack.Peek();
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
}
}
示例15: ConvertRomanNumberToArabic
public int ConvertRomanNumberToArabic(string romanFormatNumber)
{
var result = 0;
var stack = new Stack<int>();
var listOfArabicDigits = new List<int>();
foreach (var currentCharOfRomanNumber in romanFormatNumber)
{
var currentArabicDigit = ConvertRomanDigitToArabic(currentCharOfRomanNumber);
listOfArabicDigits.Add(currentArabicDigit);
}
foreach (var currentArabicDigit in listOfArabicDigits)
{
if (stack.Count != 0)
{
if (currentArabicDigit < stack.Peek())
{
result += stack.Sum();
stack.Clear();
stack.Push(currentArabicDigit);
}
else if (currentArabicDigit == stack.Peek())
{
stack.Push(currentArabicDigit);
}
else
{
var subtrahend = stack.Sum();
var minuend = currentArabicDigit;
var currentArabicDigitAfterSubtraction = minuend - subtrahend;
result += currentArabicDigitAfterSubtraction;
stack.Clear();
}
}
else
{
stack.Push(currentArabicDigit);
}
}
var remainingDigits = stack.Sum();
result += remainingDigits;
return result;
}