ZScript dynamic array suggestions

Post a reply

Smilies
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :geek: :ugeek: :!: :?: :idea: :arrow: :| :mrgreen: :3: :wub: >:( :blergh:
View more smilies

BBCode is ON
[img] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: ZScript dynamic array suggestions

Re: ZScript dynamic array suggestions

by Graf Zahl » Thu Oct 12, 2017 5:26 am

It's a known issue biut not easy to fix because the VM doesn't know anything about array references.

Re: ZScript dynamic array suggestions

by Nash » Thu Oct 12, 2017 4:44 am

Passing dynamic arrays around is currently VERY hacky; you make a "wrapper" class declaring your dynamic array in there, then pass the array by doing

Code: Select all

class WrapperClass
{
    Array<someType> contents;
}

let arr = new("WrapperClass");
arr.contents[i] = whatever;
 
I mean, do-able but as I said, hacky and makes code harder to read and maintain.

Re: ZScript dynamic array suggestions

by Sarah » Thu Oct 12, 2017 2:02 am

For suggestion 1, I'm a bit surprised this isn't already possible. If I'm stuck with just arrays, Z-Windows will need this in order to process groups of elements; from reading the page on pointers it doesn't appear I can create linked lists which is a bit of a downer. Be nice to have this and actual *pointers.

This page gives a nice overview of the various sorting methods out there. My vote would be on a merge sort as the time complexity seems to remain fairly constant with large arrays. Basically sorting often results in O(n^2) for a worst case scenario regardless of algorithm. I think what I would do, if I knew my arrays were likely to contain thousands of items, I would implement some sort of pre-sorting as part of array insertion.

Counting occurrences is probably doomed to be a linear method. If you could pass the array into a method it wouldn't be hard to implement in your own code.

ZScript dynamic array suggestions

by Nash » Sat Oct 07, 2017 11:45 am

1) Pass arrays into functions directly.

2) Array sorting (preferably a fast algorithm that's not bubble sort cuz that's quite slow when dealing with thousands of items).

3) Count how many occurrences of something specific is in the array.

Thanks!

Top