本文整理汇总了C#中MemorySlice.PrepareForSearching方法的典型用法代码示例。如果您正苦于以下问题:C# MemorySlice.PrepareForSearching方法的具体用法?C# MemorySlice.PrepareForSearching怎么用?C# MemorySlice.PrepareForSearching使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MemorySlice
的用法示例。
在下文中一共展示了MemorySlice.PrepareForSearching方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SearchPrefixed
private NodeHeader* SearchPrefixed( MemorySlice key )
{
key.PrepareForSearching();
int numberOfEntries = NumberOfEntries;
if (numberOfEntries == 0)
{
LastSearchPosition = 0;
LastMatch = 1;
return null;
}
switch (key.Options)
{
case SliceOptions.Key:
{
var pageKey = CreateNewEmptyKey();
if (numberOfEntries == 1)
{
var node = GetNode(0);
SetNodeKey(node, ref pageKey);
LastMatch = key.Compare(pageKey);
LastSearchPosition = LastMatch > 0 ? 1 : 0;
return LastSearchPosition == 0 ? node : null;
}
int low = IsLeaf ? 0 : 1;
int high = numberOfEntries - 1;
int position = 0;
while (low <= high)
{
position = (low + high) >> 1;
var node = (NodeHeader*)(_base + KeysOffsets[position]);
SetNodeKey(node, ref pageKey);
LastMatch = key.Compare(pageKey);
if (LastMatch == 0)
break;
if (LastMatch > 0)
low = position + 1;
else
high = position - 1;
}
if (LastMatch > 0) // found entry less than key
{
position++; // move to the smallest entry larger than the key
}
Debug.Assert(position < ushort.MaxValue);
LastSearchPosition = position;
if (position >= numberOfEntries)
return null;
return GetNode(position);
}
case SliceOptions.BeforeAllKeys:
{
LastSearchPosition = 0;
LastMatch = 1;
return GetNode(0);
}
case SliceOptions.AfterAllKeys:
{
LastMatch = -1;
LastSearchPosition = numberOfEntries - 1;
return GetNode(LastSearchPosition);
}
default:
throw new NotSupportedException("This SliceOptions is not supported. Make sure you have updated this code when adding a new one.");
}
}
示例2: Search
public NodeHeader* Search(MemorySlice key)
{
if(KeysPrefixed)
key.PrepareForSearching();
if (NumberOfEntries == 0)
{
LastSearchPosition = 0;
LastMatch = 1;
return null;
}
if (key.Options == SliceOptions.BeforeAllKeys)
{
LastSearchPosition = 0;
LastMatch = 1;
return GetNode(0);
}
if (key.Options == SliceOptions.AfterAllKeys)
{
LastMatch = -1;
LastSearchPosition = NumberOfEntries - 1;
return GetNode(LastSearchPosition);
}
var pageKey = CreateNewEmptyKey();
if (NumberOfEntries == 1)
{
SetNodeKey(GetNode(0), ref pageKey);
LastMatch = key.Compare(pageKey);
LastSearchPosition = LastMatch > 0 ? 1 : 0;
return LastSearchPosition == 0 ? GetNode(0) : null;
}
int low = IsLeaf ? 0 : 1;
int high = NumberOfEntries - 1;
int position = 0;
while (low <= high)
{
position = (low + high) >> 1;
var node = (NodeHeader*)(_base + KeysOffsets[position]);
SetNodeKey(node, ref pageKey);
LastMatch = key.Compare(pageKey);
if (LastMatch == 0)
break;
if (LastMatch > 0)
low = position + 1;
else
high = position - 1;
}
if (LastMatch > 0) // found entry less than key
{
position++; // move to the smallest entry larger than the key
}
Debug.Assert(position < ushort.MaxValue);
LastSearchPosition = position;
if (position >= NumberOfEntries)
return null;
return GetNode(position);
}