What are the desired semantics of a variable length array? My vague thoughts are:
(1) initially empty, with push_back/pop_back functions to add and remove elements from the end (2) reserve_memory(n), reserve_addresses(n), bound(n) (3) lock(n) reserve_memory allocates, or promises to allocate, enough memory for n slots. reserve_addresses reserves address space (using mmap or equivalent) without necessarily promising to reserve storage. bound just sets the maximum possible size of the array, if exceeded the program aborts. lock hints that pages should be locked into memory. When reserved address space is exceeded, one of two things happens: (a) the program aborts (b) the array is reallocated. Reallocation leads to some problems. First, we need an extra level of indirection to refer to an array. Secondly, either the objects are POD, so we can use realloc, or we need to make a new extent and move stuff into it -- which requires the shape have at least a move function. Next, we have the problem that any objects moved will require pointers into them to be adjusted, which can be expensive (the code for doing this already exists though). Of course that will not work for raw C++ types, which require the move function to do this. Generally, realloc could be very efficient .. it need not actually copy store even when a reallocation is required -- it can sometimes just remap pages to a new address. A good realloc will do that for large stores. In addition, what do we do with writes 'off the end'? For that matter reads? A read can always fetch the default value (all C++ objects have default constructors, even if they do nothing and don't ensure the object is initialised). Although 'in theory' one could insist on using 'push_back' to add storage, it is sometimes convenient to initialise an array with a loop, and this could even scan backwards through the array. Finally, there is the issue of whether varrays are first class objects (passed by value) or not. In theory, they should be .. since you can always use a pointer to pass references. An post-finally (lol): I am working on an associative sparse array implemented as a trie. -- John Skaller <skaller at users dot sf dot net> Felix, successor to C++: http://felix.sf.net ------------------------------------------------------------------------- Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV _______________________________________________ Felix-language mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/felix-language
