internal uint malloc(TypeInfo type, int size) { _usedMem += size; // NOTE: really important aspect here, we return slot index + 1 because we need 0 to still represent "null" // also super important to consider that in an interpreted context, these aren't real pointers. they're slot indices. // you can't retrieve a raw pointer to heap allocations in Cozi anyway though, so that's fine I think. // scan for an existing slot to reuse first for (int i = 0; i < _heap.Count; i++) { if (_heap[i].Memory.Memory.IsEmpty) { _heap[i].Type = type; _heap[i].Memory = _allocator.Alloc(size); return(_heap[i].Handle); } } // no slot found, just append a new one var heapRef = new HeapValue(this) { Type = type, Handle = (uint)(_heap.Count + 1), Memory = _allocator.Alloc(size) }; _heap.Add(heapRef); return(heapRef.Handle); }
void heapUpdate(int index) { HeapValue x = heapValue[index]; while (index > 0 && x.value < heapValue[index / 2].value) { heapAssign(index, heapValue[index / 2]); index /= 2; } int leaf = index + index + 1; while (leaf < heapSize) { if (leaf + 1 < heapSize && heapValue[leaf].value > heapValue[leaf + 1].value) { ++leaf; } if (x.value > heapValue[leaf].value) { heapAssign(index, heapValue[leaf]); index = leaf; leaf = index + index + 1; } else { break; } } heapAssign(index, x); }
private void Swap(int c, int p) { HeapValue temp = heap[p]; heap[p] = heap[c]; heap[c] = temp; }
public HeapValue Pop() { HeapValue ret = heap[0]; heap[0] = heap[heap.Count - 1]; heap.RemoveAt(heap.Count - 1); HeapifyDOWN(0); return(ret); }
void heapAssign(int index, HeapValue x) { heapValue[index] = x; heapIndex[heapValue[index].index] = index; }
//IThreadPoolWorkBatcherMember public void PerformWork(object context) { WorkContext con = (WorkContext)context; PathFinderAgent agent = con.agent; AgentProperties properties = agent.properties; List <NodeCoverPoint> tempResult = Pool.GenericPool <List <NodeCoverPoint> > .Take(); Vector3 start = agent.positionVector3; Cell cellStart; if (PathFinder.TryGetClosestCell(start, agent.properties, out cellStart) == false) { if (con.callBack != null) { con.callBack.Invoke(); } agent.ReceiveCovers(tempResult); tempResult.Clear(); Pool.GenericPool <List <NodeCoverPoint> > .ReturnToPool(ref tempResult); return; } //base.SetBase(con.agent); //base.GetStartValues(); float maxMoveCost = con.maxMoveCost; //int maxChunkDepth = con.maxChunkDepth; bool ignoreCrouchCost = con.ignoreCrouchCost; //checkedGraphs.Add(startGraph); //lastIteration.Add(startGraph); ////graphs //for (int i = 0; i < maxChunkDepth; i++) { // curIteration.Clear(); // foreach (var lastGraph in lastIteration) { // for (int n = 0; n < 4; n++) { // Graph neighbourGraph; // while (true) { // if (PathFinder.GetGraphFrom(lastGraph.gridPosition, (Directions)n, properties, out neighbourGraph) && neighbourGraph.canBeUsed) // break; // Thread.Sleep(20); // } // if (neighbourGraph != null && checkedGraphs.Add(neighbourGraph)) // curIteration.Add(neighbourGraph); // } // } // lastIteration.Clear(); // foreach (var item in curIteration) { // lastIteration.Add(item); // } //} //actual cover search //just use Dijkstra to find nearest cover //CellPath path = new CellPath(startCell, start_v3); if (cellStart.covers != null) { tempResult.AddRange(cellStart.covers); } excludedCells.Add(cellStart); foreach (var connection in cellStart.connections) { heap.HeapAdd(new HeapValue(connection, connection.Cost(start, properties, ignoreCrouchCost))); } while (true) { if (heap.count == 0) { break; } HeapValue cur = heap.HeapRemoveFirst(); if (cur.value > maxMoveCost) { break; } Cell currentCell = cur.content.connection; if (excludedCells.Contains(currentCell)) { continue; } else { excludedCells.Add(currentCell); } #if UNITY_EDITOR if (Debuger_K.debugPath) { Debuger_K.AddPath(cur.content.from.centerVector3, cur.content.connection.centerVector3, Color.green); } #endif if (currentCell.covers != null) { tempResult.AddRange(currentCell.covers); } foreach (var connection in currentCell.connections) { heap.HeapAdd(new HeapValue(connection, cur.value + connection.Cost(properties, ignoreCrouchCost))); } } agent.ReceiveCovers(tempResult); tempResult.Clear(); Pool.GenericPool <List <NodeCoverPoint> > .ReturnToPool(ref tempResult); if (con.callBack != null) { con.callBack.Invoke(); } }
public void Insert(HeapValue hv) { heap.Add(hv); HeapifyUP(heap.Count - 1); }