digitalmars.D - in operator for dynamic arrays
- Ben Hinkle (15/15) Jun 04 2005 In case it hasn't been requested before, it would be nice if the 'in'
- Michael Butscher (8/23) Jun 07 2005 This would mean that in behaves differently for associative and dynamic ...
- Ben Hinkle (8/35) Jun 07 2005 Why would you prefer a pointer? Is it to match AA's? I'm curious why.
- Kris (12/54) Jun 07 2005 While perhaps nice on the surface, I'm thinking this idea has some hidde...
- Ben Hinkle (4/63) Jun 07 2005 I would it expect it to behave like std.string.find (and the rest of the...
- Michael Butscher (13/57) Jun 08 2005 I think that a pointer would be more practical (see below) and I persona...
In case it hasn't been requested before, it would be nice if the 'in' operator found the first occurrence in a dynamic array if present. So char[] str; ... int n = ':' in str; returned the index of the first ':' or -1 if not present (generalizing std.string.find to any array). One could even imagine making 'in' work for sub-arrays: int n = "::" in str; Note that 'key in AA' returns a pointer to the value or null so potentially one could use the same return values for dynamic arrays but it seems like the index is more useful even though it means conditionals will look like if (0 in x == -1) instead of if (0 in x)
Jun 04 2005
Ben Hinkle wrote:In case it hasn't been requested before, it would be nice if the 'in' operator found the first occurrence in a dynamic array if present. So char[] str; ... int n = ':' in str; returned the index of the first ':' or -1 if not present (generalizing std.string.find to any array). One could even imagine making 'in' work for sub-arrays: int n = "::" in str; Note that 'key in AA' returns a pointer to the value or null so potentially one could use the same return values for dynamic arrays but it seems like the index is more useful even though it means conditionals will look like if (0 in x == -1) instead of if (0 in x)This would mean that in behaves differently for associative and dynamic (why not also static?) arrays. I would prefer that a pointer is returned: char* p = ':' in str; The index can then be retrieved by (p - str.ptr) after checking for a null pointer. This expression also works if the base type has a size greater than 1 (e. g. for int pointer/array). Michael
Jun 07 2005
"Michael Butscher" <mbutscher gmx.de> wrote in message news:MPG.1d100a65d86fcd69989689 news.digitalmars.com...Ben Hinkle wrote:Why would you prefer a pointer? Is it to match AA's? I'm curious why. In terms of practical use I still believe an index is more useful. The argument that a pointer would facilitate generic code isn't practical since using 'in' to find the first value in a dynamic array is significantly different enough from AA key lookups. I can't imagine much generic code that would use 'in' for both AAs and dynamic arrays.In case it hasn't been requested before, it would be nice if the 'in' operator found the first occurrence in a dynamic array if present. So char[] str; ... int n = ':' in str; returned the index of the first ':' or -1 if not present (generalizing std.string.find to any array). One could even imagine making 'in' work for sub-arrays: int n = "::" in str; Note that 'key in AA' returns a pointer to the value or null so potentially one could use the same return values for dynamic arrays but it seems like the index is more useful even though it means conditionals will look like if (0 in x == -1) instead of if (0 in x)This would mean that in behaves differently for associative and dynamic (why not also static?) arrays. I would prefer that a pointer is returned: char* p = ':' in str; The index can then be retrieved by (p - str.ptr) after checking for a null pointer. This expression also works if the base type has a size greater than 1 (e. g. for int pointer/array). Michael
Jun 07 2005
While perhaps nice on the surface, I'm thinking this idea has some hidden gotcha's in it when it comes to UTF encoding. Would this be expected to perform hidden utf conversion in any shape or form? To address Ben's question about indexing: would the function return the character index, or the array index? They are often not the same in UTF. "Ben Hinkle" <ben.hinkle gmail.com> wrote in message news:d859fb$adl$1 digitaldaemon.com..."Michael Butscher" <mbutscher gmx.de> wrote in message news:MPG.1d100a65d86fcd69989689 news.digitalmars.com...likeBen Hinkle wrote:In case it hasn't been requested before, it would be nice if the 'in' operator found the first occurrence in a dynamic array if present. So char[] str; ... int n = ':' in str; returned the index of the first ':' or -1 if not present (generalizing std.string.find to any array). One could even imagine making 'in' work for sub-arrays: int n = "::" in str; Note that 'key in AA' returns a pointer to the value or null so potentially one could use the same return values for dynamic arrays but it seemslikethe index is more useful even though it means conditionals will looknullif (0 in x == -1) instead of if (0 in x)This would mean that in behaves differently for associative and dynamic (why not also static?) arrays. I would prefer that a pointer is returned: char* p = ':' in str; The index can then be retrieved by (p - str.ptr) after checking for asincepointer. This expression also works if the base type has a size greater than 1 (e. g. for int pointer/array). MichaelWhy would you prefer a pointer? Is it to match AA's? I'm curious why. In terms of practical use I still believe an index is more useful. The argument that a pointer would facilitate generic code isn't practicalusing 'in' to find the first value in a dynamic array is significantly different enough from AA key lookups. I can't imagine much generic codethatwould use 'in' for both AAs and dynamic arrays.
Jun 07 2005
"Kris" <fu bar.com> wrote in message news:d85d7n$d4r$1 digitaldaemon.com...While perhaps nice on the surface, I'm thinking this idea has some hidden gotcha's in it when it comes to UTF encoding. Would this be expected to perform hidden utf conversion in any shape or form? To address Ben's question about indexing: would the function return the character index, or the array index? They are often not the same in UTF.I would it expect it to behave like std.string.find (and the rest of the char[] functions) and work with array indices. Using 'in' with dchars or UTF8 fragments would be nice, too."Ben Hinkle" <ben.hinkle gmail.com> wrote in message news:d859fb$adl$1 digitaldaemon.com..."Michael Butscher" <mbutscher gmx.de> wrote in message news:MPG.1d100a65d86fcd69989689 news.digitalmars.com...likeBen Hinkle wrote:In case it hasn't been requested before, it would be nice if the 'in' operator found the first occurrence in a dynamic array if present. So char[] str; ... int n = ':' in str; returned the index of the first ':' or -1 if not present (generalizing std.string.find to any array). One could even imagine making 'in' work for sub-arrays: int n = "::" in str; Note that 'key in AA' returns a pointer to the value or null so potentially one could use the same return values for dynamic arrays but it seemslikethe index is more useful even though it means conditionals will looknullif (0 in x == -1) instead of if (0 in x)This would mean that in behaves differently for associative and dynamic (why not also static?) arrays. I would prefer that a pointer is returned: char* p = ':' in str; The index can then be retrieved by (p - str.ptr) after checking for asincepointer. This expression also works if the base type has a size greater than 1 (e. g. for int pointer/array). MichaelWhy would you prefer a pointer? Is it to match AA's? I'm curious why. In terms of practical use I still believe an index is more useful. The argument that a pointer would facilitate generic code isn't practicalusing 'in' to find the first value in a dynamic array is significantly different enough from AA key lookups. I can't imagine much generic codethatwould use 'in' for both AAs and dynamic arrays.
Jun 07 2005
Ben Hinkle wrote:"Michael Butscher" <mbutscher gmx.de> wrote in message news:MPG.1d100a65d86fcd69989689 news.digitalmars.com...I think that a pointer would be more practical (see below) and I personally prefer that the same keyword behaves the same way as far as possible.Ben Hinkle wrote:Why would you prefer a pointer? Is it to match AA's? I'm curious why.In case it hasn't been requested before, it would be nice if the 'in' operator found the first occurrence in a dynamic array if present. So char[] str; ... int n = ':' in str; returned the index of the first ':' or -1 if not present (generalizing std.string.find to any array). One could even imagine making 'in' work for sub-arrays: int n = "::" in str; Note that 'key in AA' returns a pointer to the value or null so potentially one could use the same return values for dynamic arrays but it seems like the index is more useful even though it means conditionals will look like if (0 in x == -1) instead of if (0 in x)This would mean that in behaves differently for associative and dynamic (why not also static?) arrays. I would prefer that a pointer is returned: char* p = ':' in str; The index can then be retrieved by (p - str.ptr) after checking for a null pointer. This expression also works if the base type has a size greater than 1 (e. g. for int pointer/array). MichaelIn terms of practical use I still believe an index is more useful. The argument that a pointer would facilitate generic code isn't practical since using 'in' to find the first value in a dynamic array is significantly different enough from AA key lookups. I can't imagine much generic code that would use 'in' for both AAs and dynamic arrays.I agree with you here. For AA's the "in" searches the keys while for dynamic arrays it searches the values (the keys would be the indexes, searching them wouldn't be senseful). But I can't see why indexes should be more practical. I think, there are two main uses for "in" on dynamic arrays: - To test if a value is in the array or not. Testing a pointer would be shorter for that. - To modify the found value in the array. Your proposal would mean that the retrieved index must be first converted to a pointer (in the machine code) to access the value.
Jun 08 2005