3 ms·
The problem with 1-based arrays is that it makes the offset and location be different. For example, we want to place an item into a simple circular buffer with
by cyberax 3y ago
The problem with 1-based arrays is that it makes the offset and location be different. For example, we want to place an item into a simple circular buffer with zero-based indexing:
arr[idx mod capacity] = 123
Now with 1-based indexing:
arr[(idx-1) mod capacity + 1] = 123
This is absolutely makes it a PITA if you're working with things like bitmaps, blending, or sound mixing. If you only need a language for high-level scripting, then it indeed is not such a big deal.
- teamonkey 3y agoLua’s arrays can start at index 0 if you like, there’s no real reason why the first line wouldn’t work. Starting at 1 is a convention: array = {“hello”, “world”} print(array[1]) —- displays “hello” array[0] = “why” array[2] = “there” for i=0,2 do print(array[i]) end —- “why” “hello” “there”
- dicytea 3y agoIt's cute that you can do this, but it's not a practical solution, so I don't really know why people keep bring this up. As shown by your example, arrays constructed the native way will start at 1. So you're going to need to make your own custom constructor which means additional overhead. Also, all built-in functions that expects 1-based arrays will completely break: for _, value in ipairs({[0]="foo", [1]="bar"}) do print(value) end This will print "bar" And even if you still insist on pushing through all of this friction by handrolling your own stdlib, everything is out of the window once you interact with any external library that you don't control. There's a very good reason why no one is using this trick. Anything other than 1-based indexing is just undefined behavior.
- andrewmcwatters 3y agoThere are different uses for `for’, and you’re supposed to use `for i=0, n do’ for the use case you’re describing. People absolutely do this, and you have to for things like using LuaJIT’s FFI and doing work across C.
- bdowling 3y agoLua tables support do 0 as an index. You can also use negative numbers or floating point numbers, which can be convenient for some applications. One issue with using them that way is that some Lua idioms don't work as expected. For example, "for i,v in ipairs(t) do ... end" will start iteration with i=1 and stop when it reaches an index that isn't present. Also the length operator (#) won't work as expected.
- FireInsight 3y agoWhat do you get when you get the 0-index? Does x[0]=x[1]?
- sgtnoodle 3y agoJust reading the comments here, x seems to always be a hashmap. It is implied that there's no fundamental array type in the language. So no, x[0] and x[1] do not refer to the same variable, although they certainly could compare the same depending on their specific values.
- orhmeh09 3y agoWorks fine in Fortran, R, and Julia. People do pretty complex stuff in these languages and two of them are compiled.
- cyberax 3y agoIt's not an insurmountable barrier, for sure. Just something that makes it very annoying in a lot of code. While not really making it easier anywhere.
- NeuroCoder 3y agoI've been working on a PR to document and make atomic pointer interface public in Julia. It's really not that hard to handle it under the hood so that array indexing and translates to LLVM and C zero indexing.
- goatlover 3y agoWeird then that scientific computing languages prefer 1 based arrays. Maybe can show how it's very annoying in lots of Fortran or Julia code?
- cyberax 3y agoPython is as much a "scientific computing language" as anything, and it's zero-based.
- mananaysiempre 3y ago
- gerdesj 3y agoIt is potentially odd (or even) but nothing compared to everything else when it comes to programming oddities or quirks. What is the first item in this collection? Oh that would be the one in the box marked zero unless it should be in the one marked ... one! You mention a circular buffer as an outlier but you simply have to decide where it (the buffer) started off life - 0 or 1 or something else. Make sure that when it is defined, everyone agrees on offsets and off you trot. No it isn't particularly easy but then bitmaps and blending and sound mixing involve some very involved concepts that are way more complicated than sorting array bounds and worrying about where you are on the Magic Roundabout.
- cyberax 3y ago> You mention a circular buffer as an outlier but you simply have to decide where it (the buffer) started off life - 0 or 1 or something else. No. A circular buffer is just a simple example where you need to go between offsets and locations. The same theme happens in many similar cases, like directly manipulating bitmaps. E.g. with zero-based indexing into an RGB bitmap: bitmap[y*width*3 + x*3] = 1 vs. 1-based indexes: bitmap[(y-1)*width*3 + (x-1)*3 + 1] = 1
- pmontra 3y agoThe point here is that with the Lua code the CPU is doing some extra work because mod is 0 based because of mathematics and Lua is 1 based by design. However mod itself is doing extra work compared to incrementing a counter by one. This is pseudocode because I don't use Lua much and I don't have the time to check the syntax now, but it should give the general idea class CircBuf(size) buf = new Array(size) i = 1 add(value) buf[i] = value if i == size i = 1 else i = i + 1 The relative efficiency boils down to a comparison+increment vs mod on the particular CPU the code is running on. Mod should be a division, unless size is a power of two.