本文整理汇总了C#中SortedList.IndexOfValue方法的典型用法代码示例。如果您正苦于以下问题:C# SortedList.IndexOfValue方法的具体用法?C# SortedList.IndexOfValue怎么用?C# SortedList.IndexOfValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SortedList
的用法示例。
在下文中一共展示了SortedList.IndexOfValue方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ShortestPath
public Stack ShortestPath(GameObject start, GameObject end)
{
InitializeNodesForShortestPath();
start.GetComponent<NavpointScript>().SetDist(0f);
SortedList nodes = new SortedList();
nodes.Add (0f, start);
while(nodes.Count > 0){
if(end.Equals ((GameObject)nodes.GetByIndex (0))){
break;
}
NavpointScript u = ((GameObject)nodes.GetByIndex (0)).GetComponent<NavpointScript>();
nodes.RemoveAt (0);
u.SetVisited();
GameObject[] adj = u.GetAdjacentNodes();
for(int i = 0; i < adj.Length; i++){
NavpointScript v = adj[i].GetComponent<NavpointScript>();
float alt = u.GetDistByIndex (i) + u.GetDist ();
if(alt < v.GetDist () && !v.Visited ()){
v.SetDist(alt);
v.SetPrev(u.gameObject);
if(nodes.ContainsValue (v))
nodes.RemoveAt (nodes.IndexOfValue (v));
nodes.Add (alt, v.gameObject);
}
}
}
Stack s = new Stack();
GameObject node = end;
while(node != null){
s.Push(node);
node = node.GetComponent<NavpointScript>().GetPrev();
}
return s;
}
示例2: runTest
public virtual bool runTest()
{
Console.Error.WriteLine( "Co4331IndexOfKey runTest() started." );
String strLoc="Loc_000oo";
StringBuilder sblMsg = new StringBuilder( 99 );
int iCountErrors = 0;
int iCountTestcases = 0;
SortedList sl2 = null;
StringBuilder sbl3 = new StringBuilder( 99 );
StringBuilder sbl4 = new StringBuilder( 99 );
StringBuilder sblWork1 = new StringBuilder( 99 );
String str5 = null;
String str6 = null;
String str7 = null;
String s1 = null;
String s2 = null;
String s3 = null;
String s4 = null;
int[] in4a = new int[9];
int nCapacity = 100;
bool bol = false;
int i = 0;
int j = 0;
try
{
LABEL_860_GENERAL:
do
{
strLoc="100cc";
iCountTestcases++;
sl2 = new SortedList( this );
iCountTestcases++;
if ( sl2 == null )
{
Console.WriteLine( strTest+ "E_101" );
Console.WriteLine( strTest+ "SortedList creation failure" );
++iCountErrors;
break;
}
iCountTestcases++;
if ( sl2.Count != 0 )
{
Console.WriteLine( strTest+ "E_102" );
Console.WriteLine( strTest+ "New SortedList is not empty" );
++iCountErrors;
}
iCountTestcases++;
try
{
j = sl2.IndexOfKey((String)null);
++iCountErrors;
Console.Error.WriteLine( "POINTTOBREAK: Error Err_23dg! - ArgExc expected..." );
}
catch (ArgumentException aexc) {}
iCountTestcases++;
j = sl2.IndexOfKey("No_Such_Key");
if (j != -1)
{
++iCountErrors;
Console.Error.WriteLine( "POINTTOBREAK: Error Err_658d! - should return -1..." );
Console.Error.WriteLine( "returned: " + j );
}
strLoc="Loc_141aa";
for (i=0; i<100; i++)
{
++iCountTestcases;
sblMsg.Length = 0 ;
sblMsg.Append("key_");
sblMsg.Append(i);
s1 = sblMsg.ToString();
sblMsg.Length = 0 ;
sblMsg.Append("val_");
sblMsg.Append(i);
s2 = sblMsg.ToString();
sl2.Add (s1, s2);
}
strLoc="Loc_151aa";
for (i=0; i < sl2.Count ; i++)
{
sblMsg.Length = 0 ;
sblMsg.Append("key_");
sblMsg.Append(i);
s1 = sblMsg.ToString();
sblMsg.Length = 0 ;
sblMsg.Append("val_");
sblMsg.Append(i);
s2 = sblMsg.ToString();
++iCountTestcases;
s3 = (String) sl2.GetByIndex(sl2.IndexOfKey(s1)) ;
s4 = (String) sl2.GetByIndex(sl2.IndexOfValue(s2)) ;
strLoc="Loc_161aa";
if (!s3.Equals (s4))
{
++iCountErrors;
sblMsg.Length = 0 ;
sblMsg.Append( "POINTTOBREAK: Error Err_1032bc! - s4 == " );
sblMsg.Append( s4 );
sblMsg.Append( "; s3 == " );
sblMsg.Append( s3 );
Console.Error.WriteLine( sblMsg.ToString() );
//.........这里部分代码省略.........
示例3: TestIndexOfKeyBasic
public void TestIndexOfKeyBasic()
{
StringBuilder sblMsg = new StringBuilder(99);
//
SortedList sl2 = null;
StringBuilder sbl3 = new StringBuilder(99);
StringBuilder sbl4 = new StringBuilder(99);
StringBuilder sblWork1 = new StringBuilder(99);
string s1 = null;
string s2 = null;
string s3 = null;
string s4 = null;
int i = 0;
int j = 0;
//
// Constructor: Create SortedList using this as IComparer and default settings.
//
sl2 = new SortedList(this);
// Verify that the SortedList is not null.
Assert.NotNull(sl2);
// Verify that the SortedList is empty.
Assert.Equal(0, sl2.Count);
// boundary
// null key
Assert.Throws<ArgumentNullException>(() =>
{
j = sl2.IndexOfKey((string)null);
}
);
// invalid key
j = sl2.IndexOfKey("No_Such_Key");
Assert.Equal(-1, j);
// Testcase: add few key-val pairs
for (i = 0; i < 100; i++)
{
sblMsg.Length = 0;
sblMsg.Append("key_");
sblMsg.Append(i);
s1 = sblMsg.ToString();
sblMsg.Length = 0;
sblMsg.Append("val_");
sblMsg.Append(i);
s2 = sblMsg.ToString();
sl2.Add(s1, s2);
}
//
// Testcase: test IndexOfKey
//
for (i = 0; i < sl2.Count; i++)
{
sblMsg.Length = 0;
sblMsg.Append("key_"); //key
sblMsg.Append(i);
s1 = sblMsg.ToString();
sblMsg.Length = 0;
sblMsg.Append("val_"); //value
sblMsg.Append(i);
s2 = sblMsg.ToString();
// now use IndexOfKey and IndexOfVal to obtain the same object through GetByIndex and check
s3 = (string)sl2.GetByIndex(sl2.IndexOfKey(s1)); //Get the index of this key and then get object
s4 = (string)sl2.GetByIndex(sl2.IndexOfValue(s2)); //Get the index of this val and then get object
Assert.True(s3.Equals(s4));
// now Get using the index obtained thru IndexOfKey () and compare it with s2
s3 = (string)sl2.GetByIndex(sl2.IndexOfKey(s1));
Assert.True(s3.Equals(s2));
}
//
// Remove a key and then check
//
sblMsg.Length = 0;
sblMsg.Append("key_50");
s1 = sblMsg.ToString();
sl2.Remove(s1); //removes "Key_50"
j = sl2.IndexOfKey(s1);
Assert.Equal(-1, j);
}
示例4: MeshNodeHunt
/// <summary>
/// Helper method for GetNearestFreeNodes().
/// </summary>
private void MeshNodeHunt(List<Polygon> sector, Vector2 blockedPoint, ref double minDistance, SortedList<double, MeshNode> nodes, PointSet<float> nodesExist)
{
// We accept nodes that have a distance <= minDistance+1 to the blockedPoint
foreach (Polygon polygon in sector)
{
foreach (MeshNode node in polygon.GetNodes())
{
double distance = HelperMethods.EuklidDistance(node.mVector, blockedPoint);
// ReSharper disable CompareOfFloatsByEqualityOperator
// I make minDistance have the largest double value by simply assigning it. Simple
// assignment won't introduce rounding errors -> I may use exact equality test here.
if (minDistance == double.MaxValue || distance <= minDistance + 1)
// ReSharper restore CompareOfFloatsByEqualityOperator
{
// Decide if we have to add or to update a node:
if (nodesExist.Contains(node.mVector.X, node.mVector.Y))
{
int nodeIndex = nodes.IndexOfValue(node);
if (distance < nodes.Keys[nodeIndex])
nodes.Keys[nodeIndex] = distance;
}
else
{
nodes.Add(distance, node);
nodesExist.Add(node.mVector.X, node.mVector.Y);
}
}
// Look if we can even update minDistance and throw out nodes too far away
if (distance < minDistance)
{
minDistance = distance;
while (nodes.Keys[nodes.Count-1] > minDistance + 1)
{
MeshNode nodeToDelete = nodes.Values[nodes.Count - 1];
nodesExist.Remove(nodeToDelete.mVector.X, nodeToDelete.mVector.Y);
nodes.RemoveAt(nodes.Count - 1);
}
}
}
}
}
示例5: maxSlidingWindow02
//.........这里部分代码省略.........
*
* Therefore, return the max sliding window as [3,3,5,5,6,7].
*
* Note:
* You may assume k is always valid, ie: 1 ≤ k ≤ input array's size for non-empty
* array.
*
* Follow up:
* Could you solve it in linear time?
*
* How about using a data structure such as deque (double-ended queue)?
* The queue size need not be the same as the window’s size.
* Remove redundant elements and the queue should store only elements that need to be
* considered.
*
**********************************************************************************/
/*
* source code from blog:
* https://github.com/haoel/leetcode/commit/74d83796aa48cc55d7995b1f9e61db40759204bb
*
* comment from julia:
* 1. Find C++ multiset analog in C#:
* read the webpage:
* 1. https://msdn.microsoft.com/en-us/library/6tc79sx1
* understand C# choices for container classes
* 2. http://www.ccplusplus.com/2014/01/stl-multiset-example-c.html
* Go through C++ multiset and try to understand the author's implementation, things I
* should learn if I work with the C++ code.
* Based on the review of the above two web pages, my idea is to use C# SortedList<int>
* to replace the multiset.
*
* SortedList coding example:
* https://msdn.microsoft.com/en-us/library/e7a8xew6
* http://www.c-sharpcorner.com/UploadFile/c25b6d/working-on-sortedlist-using-C-Sharp/
*
* another example about IComparer:
* https://github.com/jianminchen/MaxBoxes/blob/master/Program.cs
*
* http://www.codeproject.com/Articles/42839/Sorting-Lists-using-IComparable-and-IComparer-Inte
*
*/
//O(nlog(k)
/* julia's comment: implemented in C++, need to convert it to C# code
* Read the code and try to expand the knowledge with C++/ C#
*
public static int[] maxSlidingWindow02(int[] nums, int k) {
int[] result;
//using multiset for collecting the window data (O(nlog(k) time complexity)
multiset<int> w;
for(int i=0; i<nums.Length; i++) {
//remove the left item which leaves window
if (i >= k) {
w.erase(w.find(nums[i-k]));
}
//insert the right itme which enter the window
w.insert(nums[i]);
if (i>=k-1) {
result.push_back(*w.rbegin());
}
}
return result;
}
* */
/*
* julia's comment: try to figure out how to use SortedList here.
* come back to revisit later.
*/
public static int[] maxSlidingWindow02(int[] nums, int k)
{
int len = nums.Length;
if(k==0 || k>len) return nums;
int[] result = new int[len-k+1];
//using multiset for collecting the window data (O(nlog(k) time complexity)
SortedList<int, int> w = new SortedList<int, int>();
for (int i = 0; i < nums.Length; i++)
{
//remove the left item which leaves window
if (i >= k)
{
w.Remove(w.IndexOfValue(nums[i - k])); // ?
}
//insert the right itme which enter the window
w.Add(i, nums[i]); // ?
int index = i-k+1;
if (i >= k - 1)
{
result[index] = w.IndexOfKey(0); //?
}
}
return result;
}
示例6: TestIndexOfValueBasic
public void TestIndexOfValueBasic()
{
StringBuilder sblMsg = new StringBuilder(99);
//
SortedList sl2 = null;
StringBuilder sbl3 = new StringBuilder(99);
StringBuilder sbl4 = new StringBuilder(99);
StringBuilder sblWork1 = new StringBuilder(99);
string s1 = null;
string s2 = null;
string s3 = null;
string s4 = null;
int i = 0;
int j = 0;
//
// Constructor: Create SortedList using this as IComparer and default settings.
//
sl2 = new SortedList(this);
// Verify that the SortedList is not null.
Assert.NotNull(sl2);
// Verify that the SortedList is empty.
Assert.Equal(0, sl2.Count);
// with null - should return -1
// null val
j = sl2.IndexOfValue((string)null);
Assert.Equal(-1, j);
// invalid val - should return -1
j = sl2.IndexOfValue("No_Such_Val");
Assert.Equal(-1, j);
// null is a valid value
sl2.Add("Key_0", null);
j = sl2.IndexOfValue(null);
Assert.NotEqual(-1, j);
// first occurrence check
sl2.Add("Key_1", "Val_Same");
sl2.Add("Key_2", "Val_Same");
j = sl2.IndexOfValue("Val_Same");
Assert.Equal(1, j);
sl2.Clear();
// Testcase: add few key-val pairs
for (i = 0; i < 100; i++)
{
sblMsg.Length = 0;
sblMsg.Append("key_");
sblMsg.Append(i);
s1 = sblMsg.ToString();
sblMsg.Length = 0;
sblMsg.Append("val_");
sblMsg.Append(i);
s2 = sblMsg.ToString();
sl2.Add(s1, s2);
}
//
// Testcase: test IndexOfVal
//
for (i = 0; i < sl2.Count; i++)
{
sblMsg.Length = 0;
sblMsg.Append("key_"); //key
sblMsg.Append(i);
s1 = sblMsg.ToString();
sblMsg.Length = 0;
sblMsg.Append("val_"); //value
sblMsg.Append(i);
s2 = sblMsg.ToString();
// now use IndexOfKey and IndexOfVal to obtain the same object and check
s3 = (string)sl2.GetByIndex(sl2.IndexOfKey(s1)); //Get the index of this key and then get object
s4 = (string)sl2.GetByIndex(sl2.IndexOfValue(s2)); //Get the index of this val and then get object
Assert.True(s3.Equals(s4));
// now Get using the index obtained thru IndexOfKey () and compare it with s2
s3 = (string)sl2.GetByIndex(sl2.IndexOfKey(s1));
Assert.True(s3.Equals(s2));
}
//
// Remove a key and then check
//
sblMsg.Length = 0;
sblMsg.Append("key_50");
s1 = sblMsg.ToString();
//.........这里部分代码省略.........
示例7: AddSegmentAnalyses
//.........这里部分代码省略.........
var twficElementListForCurrentSegment = new List<byte[]>();
var twficMap = new Dictionary<string, byte[]>();
foreach (var twficKvp in from xficKvp in xficsForCurrentOldSegment
where GetAnnotationTypeGuid(xficKvp.Value) == DataMigrationServices.kTwficAnnDefnGuid
select xficKvp)
{
twficBeginOffsetListForCurrentSegment.Add(twficKvp.Key);
var twficGuid = GetGuid(twficKvp.Value);
twficGuidListForCurrentSegment.Add(twficGuid);
twficElementListForCurrentSegment.Add(twficKvp.Value);
twficMap.Add(twficGuid, twficKvp.Value);
}
var textTagElementsForCurrentSegment = new List<byte[]>();
foreach (var oldTextTagAnnElement in oldTextTags)
{
// Find the ones that are used in the current segment,
// and add them to textTagElementsForCurrentSegment.
var appliesToGuids = new List<string>();
foreach (var guid in GetAppliesToObjsurGuids(oldTextTagAnnElement))
{
if (twficGuidListForCurrentSegment.Contains(guid))
appliesToGuids.Add(guid);
}
if (appliesToGuids.Count() <= 0)
continue;
// Store them for a while.
textTagElementsForCurrentSegment.Add(oldTextTagAnnElement);
// Get the index of the First twfic in appliesToGuids collection (which may hold pfics)
// and the index of the Last twfic in appliesToGuids collection (which may hold pfics).
var beginIdx = 0;
for (var i = 0; i < appliesToGuids.Count(); ++i)
{
var currentXfixGuid = appliesToGuids[i].ToLower();
byte[] currentTwficElement;
if (!twficMap.TryGetValue(currentXfixGuid, out currentTwficElement))
continue;
beginIdx = xficsForCurrentOldSegment.IndexOfValue(currentTwficElement);
break;
}
var endIdx = 0;
for (var i = appliesToGuids.Count() - 1; i > -1; --i)
{
var currentXfixGuid = appliesToGuids[i].ToLower();
byte[] currentTwficElement;
if (!twficMap.TryGetValue(currentXfixGuid, out currentTwficElement))
continue;
endIdx = xficsForCurrentOldSegment.IndexOfValue(currentTwficElement);
break;
}
var owningStText = dtoRepos.GetOwningDTO(paraDto);
var newTextTagGuid = Guid.NewGuid().ToString().ToLower();
var newTextTagElement = new XElement("rt",
new XAttribute("class", "TextTag"),
new XAttribute("guid", newTextTagGuid),
new XAttribute("ownerguid", owningStText.Guid.ToLower()),
new XElement("CmObject"),
new XElement("TextTag",
new XElement("BeginSegment", DataMigrationServices.CreateReferenceObjSurElement(newSegmentGuid)),
new XElement("EndSegment", DataMigrationServices.CreateReferenceObjSurElement(newSegmentGuid)),
new XElement("BeginAnalysisIndex", new XAttribute("val", beginIdx)),
new XElement("EndAnalysisIndex", new XAttribute("val", endIdx)),
new XElement("Tag", DataMigrationServices.CreateReferenceObjSurElement(GetInstanceOfGuid(oldTextTagAnnElement)))));
// Add new DTO to repos.
var newTextTagDto = new DomainObjectDTO(newTextTagGuid, "TextTag", newTextTagElement.ToString());
dtoRepos.Add(newTextTagDto);
// Add new TextTag to owning prop on owner as objsur element.
var owningStTextElement = XElement.Parse(owningStText.Xml);
var innerStTextElement = owningStTextElement.Element("StText");
var tagsPropElement = innerStTextElement.Element("Tags");
if (tagsPropElement == null)
{
tagsPropElement = new XElement("Tags");
innerStTextElement.Add(tagsPropElement);
}
tagsPropElement.Add(DataMigrationServices.CreateOwningObjSurElement(newTextTagGuid));
// Tell repos of the modification.
owningStText.Xml = owningStTextElement.ToString();
dtoRepos.Update(owningStText);
}
// Remove current text tags from input list
foreach (var currentTextTagElement in textTagElementsForCurrentSegment)
oldTextTags.Remove(currentTextTagElement);
}
//else
//{
// // No xfics at all, so make sure para is set to be tokenized again.
// // Done globally for each Para in ProcessParagraphs
// //MarkParaAsNeedingTokenization(dtoRepos, paraDto, paraElement);
//}
return retval;
}