C
C#5mo ago
Tofaa

Array resizing issues

I'm so lost, i've been trying to resize my array Here's the code
internal class SingleThreadObjectArray<T> : IObjectArray<T>
{
private T[] array;
private int max;

internal SingleThreadObjectArray(int size)
{
array = new T[size];
max = size;
}

public T? Get(int index)
{
return index < array.Length ? array[index] : default(T);
}

public void Set(int index, T? value)
{
if (index >= array.Length)
{
var newLength = index * 2 + 1;
Array.Resize(ref array, newLength);
}
array[index] = value!;
max = Math.Max(max, index);
}

public void Trim()
{
array.CopyTo(array, max + 1);
}
}
internal class SingleThreadObjectArray<T> : IObjectArray<T>
{
private T[] array;
private int max;

internal SingleThreadObjectArray(int size)
{
array = new T[size];
max = size;
}

public T? Get(int index)
{
return index < array.Length ? array[index] : default(T);
}

public void Set(int index, T? value)
{
if (index >= array.Length)
{
var newLength = index * 2 + 1;
Array.Resize(ref array, newLength);
}
array[index] = value!;
max = Math.Max(max, index);
}

public void Trim()
{
array.CopyTo(array, max + 1);
}
}
However, whenever i try to set a higher index using Set, it throws an index out of bounds exception
18 Replies
Tofaa
TofaaOP5mo ago
I tried using Array.CopyTo and then value = the array given by CopyTo, but nothing works
Angius
Angius5mo ago
Use the debugger to see what the values are after resizing?
Angius
Angius5mo ago
Resizing seems to work fine on my end
No description
SpReeD
SpReeD5mo ago
Stupid question, but, why not use List instead?
Angius
Angius5mo ago
You can't set list items at arbitrary indexes, the above is probably supposed to allow setting the 8376th element of a 3-element array I guess depending how large the gaps are supposed to be, though, I would maybe consider a dictionary though
Tofaa
TofaaOP5mo ago
yup theyre not that drastic but i've found out this is much more performance efficient in my scenario atleast in my java iteration of this project, havent tested the c# one but im guessing it'd be similar
SpReeD
SpReeD5mo ago
Or giving the member an Id property to access the object directly?
Tofaa
TofaaOP5mo ago
im far less familiar with the toolchain in the language to be able to do this This is used as a registry
Angius
Angius5mo ago
It's nothing complex. Set a breakpoint, run the debug, watch the values $debug if you want a full tutorial
MODiX
MODiX5mo ago
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
Tofaa
TofaaOP5mo ago
I dont particularly know what id will be set up until the startup of the project Thank you very much
Angius
Angius5mo ago
So it is possible that you'll end up with large gaps? If so, then I would really just consider a dictionary
Tofaa
TofaaOP5mo ago
welp the debuggger has spewed a LOT of stuff i do not understand
Angius
Angius5mo ago
Like what?
Tofaa
TofaaOP5mo ago
Kinda everything
No description
Tofaa
TofaaOP5mo ago
it's intimidating at first Might just end up doing this
Angius
Angius5mo ago
Seeing how the name of this class says "single thread", I assume some threading is involved somewhere. There's also a ConcurrentDictionary that's thread-safe, should you need it
Tofaa
TofaaOP5mo ago
yup there's one thats volatile and locked and one that's just plain old array without anything to synchroizeit
Want results from more Discord servers?
Add your server