public unsafe void UnsafeList_Allocate_Deallocate_Read_Write() { var list = new UnsafeList(Allocator.Persistent); list.Add(1); list.Add(2); Assert.AreEqual(2, list.Length); Assert.AreEqual(1, UnsafeUtility.ReadArrayElement <int>(list.Ptr, 0)); Assert.AreEqual(2, UnsafeUtility.ReadArrayElement <int>(list.Ptr, 1)); list.Dispose(); }
public virtual void SortSystemUpdateList() { CheckCreated(); if (!UseLegacySortOrder) { throw new InvalidOperationException("UseLegacySortOrder must be true to use the SortSystemUpdateList() legacy API"); } if (!m_systemSortDirty) { return; } m_systemSortDirty = false; RemovePending(); foreach (var sys in m_systemsToUpdate) { if (TypeManager.IsSystemAGroup(sys.GetType())) { RecurseUpdate((ComponentSystemGroup)sys); } } var elems = new List <ComponentSystemSorter.SystemElement>(m_systemsToUpdate.Count); for (int i = 0; i < m_systemsToUpdate.Count; ++i) { elems.Add(new ComponentSystemSorter.SystemElement { Index = new UpdateIndex(i, true), Type = m_systemsToUpdate[i].GetType() }); } ComponentSystemSorter.Sort(elems, this.GetType()); var oldSystems = m_systemsToUpdate; m_systemsToUpdate = new List <ComponentSystemBase>(oldSystems.Count); m_MasterUpdateList.Clear(); for (int i = 0; i < elems.Count; ++i) { var index = elems[i].Index; m_systemsToUpdate.Add(oldSystems[index.Index]); m_MasterUpdateList.Add(new UpdateIndex(i, true)); } }
public unsafe void UnsafeUtility_ReadArrayElementBoundsChecked_Performance() { const int numElements = 16 << 10; var sizeOf = UnsafeUtility.SizeOf <TestStruct>(); var alignOf = UnsafeUtility.AlignOf <TestStruct>(); var list = new UnsafeList(sizeOf, alignOf, numElements, Allocator.Persistent, NativeArrayOptions.ClearMemory); for (int i = 0; i < numElements; ++i) { list.Add(new TestStruct { x = i, y = (short)(i + 1), z = true }); } Measure.Method(() => { for (int i = 0; i < numElements; ++i) { UnsafeUtilityExtensions.ReadArrayElementBoundsChecked <TestStruct>(list.Ptr, i, numElements); } }) .WarmupCount(100) .MeasurementCount(1000) .Run(); list.Dispose(); }
/// <summary> /// Initializes a new instance of the <see cref="Scene" /> class. /// </summary> public Scene() { _perFrameData = new CBPerFrame(); this.TransformMode2D = Graphics2DTransformMode.Custom; this.CustomTransform2D = Matrix3x2.Identity; this.VirtualScreenSize2D = new SizeF(); _sceneComponents = new SceneComponentFlyweight(this); _sceneLayers = new UnsafeList <SceneLayer>(); _sceneLayers.Add(new SceneLayer(DEFAULT_LAYER_NAME, this)); this.Layers = new ReadOnlyCollection <SceneLayer>(_sceneLayers); _drawing2DLayers = new List <Custom2DDrawingLayer>(); _asyncInvokesBeforeUpdate = new ConcurrentQueue <Action>(); _asyncInvokesUpdateBesideRendering = new ConcurrentQueue <Action>(); _registeredResourceDicts = new IndexBasedDynamicCollection <ResourceDictionary>(); _registeredViews = new IndexBasedDynamicCollection <ViewInformation>(); _renderParameters = new IndexBasedDynamicCollection <SceneRenderParameters>(); this.CachedUpdateState = new SceneRelatedUpdateState(); // Try to initialize this scene object this.InitializeResourceDictionaries(); }
internal void RemoveUnmanagedSystemFromUpdateList(SystemRefUntyped sys) { CheckCreated(); m_systemSortDirty = true; m_UnmanagedSystemsToRemove.Add(sys); }
public unsafe void UnsafeUtility_ReadArrayElement_Performance() { const int numElements = 16 << 10; var list = new UnsafeList <TestStruct>(numElements, Allocator.Persistent, NativeArrayOptions.ClearMemory); for (int i = 0; i < numElements; ++i) { list.Add(new TestStruct { x = i, y = (short)(i + 1), z = true }); } Measure.Method(() => { for (int i = 0; i < numElements; ++i) { UnsafeUtility.ReadArrayElement <TestStruct>(list.Ptr, i); } }) .WarmupCount(100) .MeasurementCount(1000) .Run(); list.Dispose(); }
public void UpdateChunkCollections() { ActiveArchetypeChunks.Clear(); ActiveFullArchetypeChunks.Clear(); ActivePartialArchetypeChunk.Clear(); InactiveFullArchetypeChunks.Clear(); InactivePartialArchetypeChunk.Clear(); for (int i = 0; i < ActiveChunks.ChunkCount; i++) { var archetypeChunk = ActiveChunks.GetArchetypeChunk(i); if (archetypeChunk.Full) { ActiveFullArchetypeChunks.Add(archetypeChunk); } else { ActivePartialArchetypeChunk.Add(archetypeChunk); } ActiveArchetypeChunks.Add(archetypeChunk); } for (int i = 0; i < InactiveChunks.ChunkCount; i++) { var archetypeChunk = InactiveChunks.GetArchetypeChunk(i); if (archetypeChunk.Full) { InactiveFullArchetypeChunks.Add(archetypeChunk); } else { InactivePartialArchetypeChunk.Add(archetypeChunk); } } }
public void RemoveTest() { var arr = UnsafeList.Allocate <int>(10); for (int i = 1; i <= 10; i++) { UnsafeList.Add(arr, i); } Assert.AreEqual(10, UnsafeList.GetCount(arr)); UnsafeList.RemoveAt(arr, 4); //Remove number 5 Assert.AreEqual(9, UnsafeList.GetCount(arr)); int offs = 0; for (int i = 1; i < 10; i++) { if (i == 5) { offs++; //Skip previously removed 5 } var num = UnsafeList.Get <int>(arr, i - 1); Assert.AreEqual(i + offs, num); } }
public ListIndexOf() { for (int i = 0; i < COUNT; i++) { list.Add(i); nList.Add(i); UnsafeList.Add(uList, i); } }
public void UnsafeListAdd() { for (int i = 0; i < COUNTMAX; i++) { UnsafeList.Add(uList, i); } UnsafeList.Clear(uList); }
/// <summary> /// Adds the given object to the layer. /// </summary> /// <param name="sceneObject">Object to add.</param> internal void AddObject(SceneObject sceneObject) { if (_isInUpdate || _isInUpdateBeside) { throw new InvalidOperationException("Unable to manipulate object list while SceneLayout is on updating!"); } if (sceneObject == null) { throw new ArgumentNullException(nameof(sceneObject)); } if (sceneObject.Scene == this.Scene) { return; } if (sceneObject.Scene != null) { throw new ArgumentException("Given object does already belong to another scene!", nameof(sceneObject)); } if (sceneObject.SceneLayer == this) { return; } if (sceneObject.SceneLayer != null) { throw new ArgumentException("Given object does already belong to another scene layer!", nameof(sceneObject)); } this.ObjectsInternal.Add(sceneObject); sceneObject.SetSceneAndLayer(this.Scene, this); // Append object to specialized collections if (sceneObject is SceneSpacialObject spacialObject) { this.SpacialObjects.Add(spacialObject); } else { _sceneObjectsNotSpacial.Add(sceneObject); } // Handle static / non static objects if (sceneObject.IsStatic) { _sceneObjectsForSingleUpdateCall.Enqueue(sceneObject); } else { _sceneObjectsNotStatic.Add(sceneObject); } // Register the given object on all view subsets foreach (var actViewSubset in _viewSubsets) { actViewSubset.RegisterObject(sceneObject); } }
internal void RemoveUnmanagedSystemFromUpdateList(SystemHandleUntyped sys) { CheckCreated(); if (m_UnmanagedSystemsToUpdate.Contains(sys) && !m_UnmanagedSystemsToRemove.Contains(sys)) { m_systemSortDirty = true; m_UnmanagedSystemsToRemove.Add(sys); } }
public unsafe void UnsafeList_Resize_Zero() { var sizeOf = UnsafeUtility.SizeOf <int>(); var alignOf = UnsafeUtility.AlignOf <int>(); UnsafeList list = new UnsafeList(sizeOf, alignOf, 5, Allocator.Persistent, NativeArrayOptions.ClearMemory); var capacity = list.Capacity; list.Add(1); list.Resize <int>(0); Assert.AreEqual(0, list.Length); Assert.AreEqual(capacity, list.Capacity); // list capacity should not change on resize list.Add(2); list.Clear(); Assert.AreEqual(0, list.Length); Assert.AreEqual(capacity, list.Capacity); // list capacity should not change on resize list.Dispose(); }
internal int AddSafetyHandle(AtomicSafetyHandle handle) { if (!NeedsUseAfterFreeTracking()) { return(InvalidChildSafetyHandleIndex); } var result = ChildSafetyHandles.Length; ChildSafetyHandles.Add(handle); return(result); }
public UnsafeList <Cluster> GetAllCluster(int[] indices, Vector3[] vertices) { Triangle *triangles; int length; GetAllTriangles(indices, out triangles, out length); UnsafeList <Cluster> clusters = new UnsafeList <Cluster>(length); bool *clustedFlags = (bool *)UnsafeUtility.Malloc(UnsafeUtility.SizeOf <bool>() * length, 16, Allocator.Temp); for (int i = 0; i < length; ++i) { clustedFlags[i] = false; } for (int i = 0; i < length; ++i) { for (int j = i + 1; j < length; ++j) { if (clustedFlags[j] || clustedFlags[i]) { continue; } Cluster currentCluster; if (SimilarTriangle(triangles + i, triangles + j, out currentCluster, vertices)) { clustedFlags[j] = true; clustedFlags[i] = true; clusters.Add(ref currentCluster); } } } for (int i = 0; i < length; ++i) { if (!clustedFlags[i]) { clusters.Add(DegenerateCluster(triangles + i)); } } UnsafeUtility.Free(clustedFlags, Allocator.Temp); return(clusters); }
public void IndexOfTest() { var arr = UnsafeList.Allocate <int>(10); for (int i = 1; i <= 10; i++) { UnsafeList.Add(arr, i); } var index = UnsafeList.IndexOf(arr, 5); Assert.AreEqual(4, index); }
public void LastIndexOfTest() { var arr = UnsafeList.Allocate <int>(10); for (int i = 1; i <= 10; i++) { UnsafeList.Add(arr, i); } Assert.AreEqual(4, UnsafeList.LastIndexOf(arr, 5)); Assert.AreEqual(-1, UnsafeList.LastIndexOf(arr, 645)); Assert.AreEqual(9, UnsafeList.LastIndexOf(arr, 10)); }
internal int AddChildAllocator(AllocatorHandle handle) { if (!NeedsUseAfterFreeTracking()) { return(InvalidChildAllocatorIndex); } var result = ChildAllocators.Length; ChildAllocators.Add(handle); handle.Parent = this; handle.IndexInParent = result; return(result); }
internal void RemoveUnmanagedSystemFromUpdateList(SystemHandleUntyped sys) { CheckCreated(); if (!EnableSystemSorting) { throw new InvalidOperationException("Removing systems from a group is not supported if group.EnableSystemSorting is false."); } if (m_UnmanagedSystemsToUpdate.Contains(sys) && !m_UnmanagedSystemsToRemove.Contains(sys)) { m_systemSortDirty = true; m_UnmanagedSystemsToRemove.Add(sys); } }
public static bool AddReaderTypeIndex(int typeIndex, ref UnsafeList reading, ref UnsafeList writing) { if (reading.Contains(typeIndex)) { return(false); } if (writing.Contains(typeIndex)) { return(false); } reading.Add(typeIndex); return(true); }
public void MutateTest() { var arr = UnsafeList.Allocate <int>(10); for (int i = 0; i < 10; i++) { UnsafeList.Add(arr, i); } for (int i = 0; i < 10; i++) { Assert.AreEqual(i, UnsafeList.Get <int>(arr, i)); } UnsafeList.Free(arr); }
internal void AddUnmanagedSystemToUpdateList(SystemHandleUntyped sysHandle) { CheckCreated(); if (-1 != UnmanagedSystemIndex(sysHandle)) { int index = m_UnmanagedSystemsToRemove.IndexOf(sysHandle); if (-1 != index) { m_UnmanagedSystemsToRemove.RemoveAt(index); } return; } m_UnmanagedSystemsToUpdate.Add(sysHandle); m_systemSortDirty = true; }
internal void AddUnmanagedSystemToUpdateList(SystemRefUntyped sysRef) { CheckCreated(); if (-1 != UnmanagedSystemIndex(sysRef)) { return; } if (UseLegacySortOrder) { throw new InvalidOperationException("ISystemBase systems are not compatible with legacy sort order. Set UseLegacySortOrder to false to use ISystemBase systems."); } m_UnmanagedSystemsToUpdate.Add(sysRef); m_systemSortDirty = true; }
public static bool AddWriterTypeIndex(int typeIndex, ref UnsafeList reading, ref UnsafeList writing) { if (writing.Contains(typeIndex)) { return(false); } var readingIndex = reading.IndexOf(typeIndex); if (readingIndex != -1) { reading.RemoveAtSwapBack <int>(readingIndex); } writing.Add(typeIndex); return(true); }
internal void RemapInstanceId(int previousId, int newId) { var dependents = new UnsafeList <int>(0, Allocator.Temp); foreach (var v in _dependentsByInstanceId.GetValuesForKey(previousId)) { dependents.Add(v); _dependenciesByInstanceId.Remove(v, previousId); _dependenciesByInstanceId.Add(v, newId); } for (int i = 0; i < dependents.Length; i++) { _dependentsByInstanceId.Add(newId, dependents[i]); } _dependentsByInstanceId.Remove(previousId); }
/** * Deserialize an array into the array buffer. */ JsonKeyHandle DeserializeToArrayBuffer(SerializedArrayView arrayView) { // construct NativeList<JsonValueRef> then add to buffer var values = new UnsafeList <JsonKeyHandle>(10, m_Allocator); var arrayEnum = arrayView.GetEnumerator(); while (arrayEnum.MoveNext()) { var view = arrayEnum.Current; values.Add(DeserializeValueView(view)); } arrayEnum.Dispose(); m_ArrayBuffer.Add(values); return(new JsonKeyHandle(ref this, JsonValueType.Array, m_ArrayBuffer.Length - 1)); }
public static bool Add(ComponentType type, ref UnsafeList reading, ref UnsafeList writing) { if (!type.RequiresJobDependency) { return(false); } // If any other dependency is added the Entity type dependency is removed to avoid the overhead of having all jobs // depend on this. if ((reading.m_size == 1) && reading.Contains(TypeManager.GetTypeIndex <Entity>())) { reading.m_size = 0; } if (type.AccessModeType == ComponentType.AccessMode.ReadOnly) { if (reading.Contains(type.TypeIndex)) { return(false); } if (writing.Contains(type.TypeIndex)) { return(false); } reading.Add(type.TypeIndex); return(true); } else { if (writing.Contains(type.TypeIndex)) { return(false); } var readingIndex = reading.IndexOf(type.TypeIndex); if (readingIndex != -1) { reading.RemoveAtSwapBack <int>(readingIndex); } writing.Add(type.TypeIndex); return(true); } }
/// <summary> /// Modifies this container to keep only values that are present in both containers. /// </summary> /// <typeparam name="T">Source type of elements</typeparam> /// <param name="container">Container to modify.</param> /// <param name="other">The container to compare to this container.</param> public static void IntersectWith <T>(this NativeHashSet <T> container, FixedList32 <T> other) where T : unmanaged, IEquatable <T> { var result = new UnsafeList <T>(container.Count(), Allocator.Temp); foreach (var item in other) { if (container.Contains(item)) { result.Add(item); } } container.Clear(); container.UnionWith(result); result.Dispose(); }
public static BattleEntity find_battleEntity_ofTeam_random(BattleResolutionStep p_battle, BattleEntity_Team p_team) { using (UnsafeList <int> l_targettableEntities = new UnsafeList <int>(0, Unity.Collections.Allocator.Temp)) { for (int i = 0; i < p_battle.BattleEntities.Count; i++) { if (p_battle.BattleEntities[i].Team == p_team) { l_targettableEntities.Add(i); } } if (l_targettableEntities.Length > 0) { return(p_battle.BattleEntities[l_targettableEntities[Random.Range(0, l_targettableEntities.Length)]]); } } return(null); }
/// <summary> /// Adds all vertices and surfaces of the given geometry to this one. /// All surfaces of the given geometry are merged to this single surface. /// </summary> /// <param name="geometry">The geometry.</param> public void AddGeometry(Geometry geometry) { var baseIndex = this.Owner.CountVertices; // AddObject all vertices to local geometry this.Owner.AddVertices(geometry); // AddObject all corners to local surface foreach (var actSurface in geometry.Surfaces) { var corners = actSurface._corners; var cornerCount = corners.Count; for (var loop = 0; loop < cornerCount; loop++) { var cornerToAdd = corners[loop]; cornerToAdd.Index = cornerToAdd.Index + baseIndex; _corners.Add(cornerToAdd); } } }