/// <summary> /// Removes the key and its associated value from the map /// </summary> public bool RemovePair(IntPtr keyPtr) { IntPtr localKeyPropForCapture = keyProp; HashDelegates.GetKeyHash keyHash = delegate(IntPtr elementKey) { return(Native_UProperty.GetValueTypeHash(localKeyPropForCapture, elementKey)); }; HashDelegates.Equality keyEquality = delegate(IntPtr a, IntPtr b) { return(Native_UProperty.Identical(localKeyPropForCapture, a, b, 0)); }; IntPtr entry = map->FindValue(keyPtr, ref mapLayout, keyHash, keyEquality); if (entry != IntPtr.Zero) { int idx = (int)((entry.ToInt64() - map->GetData(0, ref mapLayout).ToInt64()) / mapLayout.SetLayout.Size); RemoveAt(idx); return(true); } else { return(false); } }
public void AddPair(IntPtr keyPtr, IntPtr valuePtr) { IntPtr localKeyPropForCapture = keyProp; IntPtr localValuePropForCapture = valueProp; HashDelegates.GetKeyHash keyHash = delegate(IntPtr elementKey) { return(Native_UProperty.GetValueTypeHash(localKeyPropForCapture, elementKey)); }; HashDelegates.Equality keyEquality = delegate(IntPtr a, IntPtr b) { return(Native_UProperty.Identical(localKeyPropForCapture, a, b, 0)); }; HashDelegates.ConstructAndAssign keyConstructAndAssign = delegate(IntPtr newElementKey) { if (Native_UProperty.HasAnyPropertyFlags(localKeyPropForCapture, EPropertyFlags.ZeroConstructor)) { FMemory.Memzero(newElementKey, Native_UProperty.GetSize(localKeyPropForCapture)); } else { Native_UProperty.InitializeValue(localKeyPropForCapture, newElementKey); } Native_UProperty.CopySingleValue(localKeyPropForCapture, newElementKey, keyPtr); }; HashDelegates.ConstructAndAssign valueConstructAndAssign = delegate(IntPtr newElementValue) { if (Native_UProperty.HasAnyPropertyFlags(localValuePropForCapture, EPropertyFlags.ZeroConstructor)) { FMemory.Memzero(newElementValue, Native_UProperty.GetSize(localValuePropForCapture)); } else { Native_UProperty.InitializeValue(localValuePropForCapture, newElementValue); } Native_UProperty.CopySingleValue(localValuePropForCapture, newElementValue, valuePtr); }; HashDelegates.Assign valueAssign = delegate(IntPtr existingElementValue) { Native_UProperty.CopySingleValue(localValuePropForCapture, existingElementValue, valuePtr); }; HashDelegates.Destruct keyDestruct = delegate(IntPtr elementKey) { if (!Native_UProperty.HasAnyPropertyFlags(localKeyPropForCapture, EPropertyFlags.IsPlainOldData | EPropertyFlags.NoDestructor)) { Native_UProperty.DestroyValue(localKeyPropForCapture, elementKey); } }; HashDelegates.Destruct valueDestruct = delegate(IntPtr elementValue) { if (!Native_UProperty.HasAnyPropertyFlags(localValuePropForCapture, EPropertyFlags.IsPlainOldData | EPropertyFlags.NoDestructor)) { Native_UProperty.DestroyValue(localValuePropForCapture, elementValue); } }; map->Add(keyPtr, valuePtr, ref mapLayout, keyHash, keyEquality, keyConstructAndAssign, valueConstructAndAssign, valueAssign, keyDestruct, valueDestruct); }
/// <summary> /// Finds key index from hash, rather than linearly searching /// </summary> public int FindPairIndexFromHash(IntPtr keyToFind) { IntPtr localKeyPropForCapture = keyProp; HashDelegates.GetKeyHash keyHash = delegate(IntPtr elementKey) { return(Native_UProperty.GetValueTypeHash(localKeyPropForCapture, elementKey)); }; HashDelegates.Equality keyEquality = delegate(IntPtr a, IntPtr b) { return(Native_UProperty.Identical(localKeyPropForCapture, a, b, 0)); }; return(map->FindPairIndex(keyToFind, ref mapLayout, keyHash, keyEquality)); }
/// <summary> /// Finds element index from hash, rather than linearly searching /// </summary> public int FindElementIndexFromHash(IntPtr elementToFind) { IntPtr localElementPropForCapture = elementProp; HashDelegates.GetKeyHash elementHash = delegate(IntPtr elementKey) { return(Native_UProperty.GetValueTypeHash(localElementPropForCapture, elementKey)); }; HashDelegates.Equality elementEquality = delegate(IntPtr a, IntPtr b) { return(Native_UProperty.Identical(localElementPropForCapture, a, b, 0)); }; return(set->FindIndex(elementToFind, ref setLayout, elementHash, elementEquality)); }
/// <summary> /// Adds the element to the set, returning true if the element was added, or false if the element was already present /// </summary> public void AddElement(IntPtr elementToAdd) { IntPtr localElementPropForCapture = elementProp; HashDelegates.GetKeyHash elementHash = delegate(IntPtr elementKey) { return(Native_UProperty.GetValueTypeHash(localElementPropForCapture, elementKey)); }; HashDelegates.Equality elementEquality = delegate(IntPtr a, IntPtr b) { return(Native_UProperty.Identical(localElementPropForCapture, a, b, 0)); }; HashDelegates.Construct elementConstruct = delegate(IntPtr newElement) { if (Native_UProperty.HasAnyPropertyFlags(localElementPropForCapture, EPropertyFlags.ZeroConstructor)) { FMemory.Memzero(newElement, Native_UProperty.GetSize(localElementPropForCapture)); } else { Native_UProperty.InitializeValue(localElementPropForCapture, newElement); } Native_UProperty.CopySingleValue(localElementPropForCapture, newElement, elementToAdd); }; HashDelegates.Destruct elementDestruct = delegate(IntPtr element) { if (!Native_UProperty.HasAnyPropertyFlags(localElementPropForCapture, EPropertyFlags.IsPlainOldData | EPropertyFlags.NoDestructor)) { Native_UProperty.DestroyValue(localElementPropForCapture, element); } }; set->Add( elementToAdd, ref setLayout, elementHash, elementEquality, elementConstruct, elementDestruct); }
/// <summary> /// Finds the index of an element in a map which matches the key in another pair. /// </summary> /// <param name="pairWithKeyToFind">The address of a map pair which contains the key to search for.</param> /// <param name="indexHint">The index to start searching from.</param> /// <returns>The index of an element found in MapHelper, or -1 if none was found.</returns> public int FindMapIndexWithKey(IntPtr pairWithKeyToFind, int indexHint = 0) { int mapMax = GetMaxIndex(); if (mapMax == 0) { return(-1); } IntPtr localKeyProp = keyProp;// prevent aliasing in loop below int index = indexHint; for (;;) { if (IsValidIndex(index)) { IntPtr pairToSearch = GetPairPtrWithoutCheck(index); if (Native_UProperty.Identical(localKeyProp, pairWithKeyToFind, pairToSearch, 0)) { return(index); } } ++index; if (index == mapMax) { index = 0; } if (index == indexHint) { return(-1); } } }
/// <summary> /// Removes the element from the set /// </summary> public bool RemoveElement(IntPtr elementToRemove) { IntPtr localElementPropForCapture = elementProp; HashDelegates.GetKeyHash elementHash = delegate(IntPtr elementKey) { return(Native_UProperty.GetValueTypeHash(localElementPropForCapture, elementKey)); }; HashDelegates.Equality elementEquality = delegate(IntPtr a, IntPtr b) { return(Native_UProperty.Identical(localElementPropForCapture, a, b, 0)); }; int foundIndex = set->FindIndex(elementToRemove, ref setLayout, elementHash, elementEquality); if (foundIndex != -1) { RemoveAt(foundIndex); return(true); } else { return(false); } }
/// <summary> /// Finds the index of an element in a set /// </summary> /// <param name="elementToFind">The address of an element to search for.</param> /// <param name="indexHint">The index to start searching from.</param> /// <returns>The index of an element found in SetHelper, or -1 if none was found.The index of an element found in SetHelper, or -1 if none was found.</returns> public int FindElementIndex(IntPtr elementToFind, int indexHint = 0) { int setMax = GetMaxIndex(); if (setMax == 0) { return(-1); } IntPtr localKeyProp = elementProp;// prevent aliasing in loop below int index = indexHint; for (;;) { if (IsValidIndex(index)) { IntPtr elementToCheck = GetElementPtrWithoutCheck(index); if (Native_UProperty.Identical(localKeyProp, elementToFind, elementToCheck, 0)) { return(index); } } ++index; if (index == setMax) { index = 0; } if (index == indexHint) { return(-1); } } }