Skip to content

Commit

Permalink
Refactor to single handed class
Browse files Browse the repository at this point in the history
  • Loading branch information
dalkia committed Jan 23, 2025
1 parent e0a5d5e commit d687467
Showing 1 changed file with 43 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,66 @@

namespace DCL.Landscape.NoiseGeneration
{
public class NoiseNativeArrayProvider : IDisposable
internal class NoiseNativeArray : IDisposable
{
private readonly Dictionary<int, Queue<NativeArray<float>>> noiseNativeArrayDictionary = new();
private readonly List<NativeArray<float>> allNativeArrays = new();
private int currentIndexUsed;
private readonly List<NativeArray<float>> nativeArrays = new ();
private readonly int size;

public void Reset()
public NoiseNativeArray(int size)
{
foreach (var nativeArray in allNativeArrays)
{
int size = (int)Math.Sqrt(nativeArray.Length);
if (!noiseNativeArrayDictionary.ContainsKey(size))
noiseNativeArrayDictionary[size] = new Queue<NativeArray<float>>();
noiseNativeArrayDictionary[size].Enqueue(nativeArray);
}
this.size = size;
}

allNativeArrays.Clear();
public NativeArray<float> GetNativeArray()
{
if (currentIndexUsed >= nativeArrays.Count)
nativeArrays.Add(new NativeArray<float>(size * size, Allocator.Persistent));
return nativeArrays[currentIndexUsed++];
}

public NativeArray<float> GetNoiseNativeArray(int key)
public void Reset()
{
// Check if there's a reusable array in the queue for the key
if (noiseNativeArrayDictionary.TryGetValue(key, out var queue) && queue.Count > 0)
return queue.Dequeue();

// If no reusable array, create a new one
var newArray = new NativeArray<float>(key * key, Allocator.Persistent);
allNativeArrays.Add(newArray);
return newArray;
currentIndexUsed = 0;
}

public void Dispose()
{
foreach (var queue in noiseNativeArrayDictionary.Values)
foreach (var nativeArray in nativeArrays)
{
while (queue.Count > 0)
{
var nativeArray = queue.Dequeue();
if (nativeArray.IsCreated)
nativeArray.Dispose();
}
nativeArray.Dispose();
}
}
}

public class NoiseNativeArrayProvider : IDisposable
{
private readonly Dictionary<int, NoiseNativeArray> noiseNativeArrayDictionary = new();

foreach (var nativeArray in allNativeArrays)
public void Reset()
{
foreach (var noiseNativeArray in noiseNativeArrayDictionary.Values)
{
if (nativeArray.IsCreated)
nativeArray.Dispose();
noiseNativeArray.Reset();
}
}

public NativeArray<float> GetNoiseNativeArray(int key)
{
if (noiseNativeArrayDictionary.TryGetValue(key, out var noiseArray))
return noiseArray.GetNativeArray();

noiseNativeArrayDictionary.Clear();
allNativeArrays.Clear();
var newArray = new NoiseNativeArray(key);
noiseNativeArrayDictionary[key] = newArray;
return newArray.GetNativeArray();
}

public void Dispose()
{
foreach (var noiseNativeArray in noiseNativeArrayDictionary.Values)
{
noiseNativeArray.Dispose();
}
}
}
}

0 comments on commit d687467

Please sign in to comment.