Open tobiasholt opened 2 years ago
Considering the usage case as below:
UvmArray test = UvmArray.create();
test.add("1");
test.size(); // should be 1
test.set(10000, "set test"); // it will work?
test.size(); // the size is still 1?
This behavior doesn't look like a common Aarray to me. Should we add some restrictions when using UvmArray? At least we should make the the size right so that we can loop all the elements in the array.
In Lua, the default index of an array starts with 1, but it can be explicitly defined as any integer, particularly it can be a negative number. In order to be compatible with Lua, the UvmArray in Java is implemented with two basic types: a List and a Hashmap, if the index is between 1 and List.size(), it indicates the elements in the List: (index >= 1 && index <= items.size()), while the index is out of the range of the List(), i.e. any negative numbers, it can be stored in the hashmap.
However, there is a protential issue for the UvmArray. As seen in the picture, the method UvmArray.size() returns just the item's size, but in the UvmArray.set(), the index can be any number, even if the size of the array is 0.
The index needs to be checked if it is valid or not when setting a value.
We have also considered to let the UvmArray support only positive number and starts with 0, i.e. just the same the native Java, but then we need to add the restrictions for Lua also, which could be very complex. So we'd rather keep the current design.