Re: Apr array functions
On Mon, 8 Nov 2004, Nathanael Noblet wrote: so that someone can use the array elements without removing them? Is there something I don't know that gives a reason why this is bad? You don't have to remove elements to get at them... the array header structure is visible to the application, so it can just do arr-elts[index] and be done with it (insert bounds checking if necessary). APR_DECLARE(void *) apr_array_get_element(aprt_array_header_t *arr, int index) { if(apr_is_empty_array(arr)) { return NULL; } if(index arr-nelts) { return NULL; } if(index == 0) { return arr-elts; } return arr-elts+(arr-elt_size * (arr-nelts-1)); } Anyway, if this function is supposed to do what I expect it's supposed to do, it seems to have several important bugs. I assume you just typed this up off the top of your head and never actually tried to compile it or use it. :) * apr_array_header_t, not aprt_array_header_t * the index==0 thing is superfluous * ..except that otherwise index is never used and so you can only ever return NULL, the first element, or the last element. :) * you perform arithmetic on a void pointer Anyway like I said it was probably just off the top of your head and it was obviously enough to get the point across, so no big deal. Also, of course it's not your bug, but: * apr_is_empty_array ... ooh, that's annoying. Why did we not rename that to apr_array_is_empty prior to APR 1.0? :-( --Cliff
Re: Apr array functions
Btw: didn't notice ahead of time that this discussion was on [EMAIL PROTECTED] instead of [EMAIL PROTECTED] Oh well. On Mon, 8 Nov 2004, Cliff Woolley wrote: You don't have to remove elements to get at them... the array header structure is visible to the application, so it can just do arr-elts[index] and be done with it (insert bounds checking if necessary). To be more precise, since arr-elts is a void pointer, you have to insert a cast here. But it doesn't have to be as complex looking as asking the array structure how big each element is and explicitly multiplying. Presumably the application code knows what actualy type the data stored in the array is, and thus a cast and pointer arithmetic will do the job for you. ((foo_t *)arr-elts)[index] Or, even better: foo_t *f = arr-elts; f[index] ... --Cliff
Re: Apr array functions
On Nov 8, 2004, at 5:50 PM, Cliff Woolley wrote: Btw: didn't notice ahead of time that this discussion was on [EMAIL PROTECTED] instead of [EMAIL PROTECTED] Oh well. I didn't know there was one specifically for that, I had noticed a good deal of message dealing with APR so figured it would be as good as any place to ask. On Mon, 8 Nov 2004, Cliff Woolley wrote: You don't have to remove elements to get at them... the array header structure is visible to the application, so it can just do arr-elts[index] and be done with it (insert bounds checking if necessary). To be more precise, since arr-elts is a void pointer, you have to insert a cast here. But it doesn't have to be as complex looking as asking the array structure how big each element is and explicitly multiplying. Presumably the application code knows what actualy type the data stored in the array is, and thus a cast and pointer arithmetic will do the job for you. ((foo_t *)arr-elts)[index] Or, even better: foo_t *f = arr-elts; f[index] ... Even better solution I admit. Though your assumption about the function I typed in my email, I did type it off the top of my head thus the typos, lots of logic in there is actually from the apr_array_pop() function. If you didn't like the arithmetic on those structure members, perhaps someone should look there. I'm not saying this because I'm hurt or angry, just to inform you of the fact that it exists in the apr source... Thank you for the response and the better array indexing above.. -- Nathanael D. Noblet Gnat Solutions 204 - 131 Gorge Road E Victoria, BC V9A 1L1 T/F 250.385.4613 http://www.gnat.ca/
Re: Apr array functions
On Mon, 8 Nov 2004, Nathanael Noblet wrote: Though your assumption about the function I typed in my email, I did type it off the top of my head thus the typos, lots of logic in there is actually from the apr_array_pop() function. If you didn't like the arithmetic on those structure members, perhaps someone should look there. I'm not saying this because I'm hurt or angry, just to inform you of the fact that it exists in the apr source... Oh sure. Well in that case, it makes sense, because the apr function doesn't know what the underlying data type is and so it *has to* do the size multiplication itself. I was just trying to give you the cleanest version you could use if you did it in your own code that *did* know the actual datatype. :) Anyway glad that will work for you. Apache does tons of that kind of direct indexing into APR arrays, or at least it used to. That's why we never thought to add such a function. I'm not saying it's necessarily a bad idea to add one from an API-completeness point of view. :) But if we're going to have that discussion, let's do it on [EMAIL PROTECTED] Thanks! --Cliff