/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* Licensed under the Oculus SDK License Agreement (the "License");
* you may not use the Oculus SDK except in compliance with the License,
* which is provided at the time of installation or download, or which
* otherwise accompanies this software in either electronic or hard copy form.
*
* You may obtain a copy of the License at
*
* https://developer.oculus.com/licenses/oculussdk/
*
* Unless required by applicable law or agreed to in writing, the Oculus SDK
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
namespace Oculus.Interaction
{
///
/// Non-allocating HashSet extension methods mirroring MS implementation
/// https://referencesource.microsoft.com/#system.core/system/Collections/Generic/HashSet.cs
///
public static class HashSetExtensions
{
///
/// Take the union of this HashSet with other. Modifies this set.
///
/// HashSet with items to add
public static void UnionWithNonAlloc(this HashSet hashSetToModify, HashSet other)
{
if (hashSetToModify == null)
{
throw new ArgumentNullException(nameof(hashSetToModify));
}
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
foreach (T item in other)
{
hashSetToModify.Add(item);
}
}
///
/// Take the union of this HashSet with other. Modifies this set.
///
/// IList with items to add
public static void UnionWithNonAlloc(this HashSet hashSetToModify, IList other)
{
if (hashSetToModify == null)
{
throw new ArgumentNullException(nameof(hashSetToModify));
}
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
for (int i = 0; i < other.Count; ++i)
{
hashSetToModify.Add(other[i]);
}
}
///
/// Remove items in other from this set. Modifies this set
///
/// HashSet with items to remove
public static void ExceptWithNonAlloc(this HashSet hashSetToModify, HashSet other)
{
if (hashSetToModify == null)
{
throw new ArgumentNullException(nameof(hashSetToModify));
}
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
if (hashSetToModify.Count == 0)
{
return;
}
if (other == hashSetToModify)
{
hashSetToModify.Clear();
return;
}
foreach (T element in other)
{
hashSetToModify.Remove(element);
}
}
///
/// Remove items in other from this set. Modifies this set
///
/// IList with items to remove
public static void ExceptWithNonAlloc(this HashSet hashSetToModify, IList other)
{
if (hashSetToModify == null)
{
throw new ArgumentNullException(nameof(hashSetToModify));
}
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
if (hashSetToModify.Count == 0)
{
return;
}
for (int i = 0; i < other.Count; ++i)
{
hashSetToModify.Remove(other[i]);
}
}
///
/// Checks if this set overlaps other (i.e. they share at least one item)
///
/// HashSet to check overlap against
/// true if these have at least one common element; false if disjoint
public static bool OverlapsNonAlloc(this HashSet hashSetToCheck, HashSet other)
{
if (hashSetToCheck == null)
{
throw new ArgumentNullException(nameof(hashSetToCheck));
}
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
if (hashSetToCheck.Count == 0)
{
return false;
}
foreach (T element in other)
{
if (hashSetToCheck.Contains(element))
{
return true;
}
}
return false;
}
///
/// Checks if this set overlaps other (i.e. they share at least one item)
///
/// IList to check overlap against
/// true if these have at least one common element; false if disjoint
public static bool OverlapsNonAlloc(this HashSet hashSetToCheck, IList other)
{
if (hashSetToCheck == null)
{
throw new ArgumentNullException(nameof(hashSetToCheck));
}
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
if (hashSetToCheck.Count == 0)
{
return false;
}
for (int i = 0; i < other.Count; ++i)
{
if (hashSetToCheck.Contains(other[i]))
{
return true;
}
}
return false;
}
}
}