Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 18, 2002 Array slicing Associative and Rectangular arrays? | ||||
---|---|---|---|---|
| ||||
* Can you array slice associative arrays? IMHO, this is an almost must-have feature...but be careful, Pavel (playful grin), because it won't look right to have end-exclusive syntax here, either * What happens if you array slice a rectangular array and try to set it "by reference" to another array? int[5][5] start = ....; int[3][3] copyByValue,copyByReference; copyByValue[] = start[1..4][1..4]; // doesn't seem like too much trouble, // copies the values over copyByReference = start[1..4][1..4]; // how in the world do you do this? -- The Villagers are Online! villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ] |
February 18, 2002 Re: Array slicing Associative and Rectangular arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | "Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3C711ADA.15D57A6C@deming-os.org... > * Can you array slice associative arrays? IMHO, this is an almost must-have feature...but be careful, Pavel (playful grin), because it won't look right to have end-exclusive syntax here, either How exactly do you slice an associative array? With item indices? Then the same rules would apply as for normal arrays, including end-exclusive syntax... > * What happens if you array slice a rectangular array and try to set it > "by reference" to another array? > int[5][5] start = ....; > int[3][3] copyByValue,copyByReference; > > copyByValue[] = start[1..4][1..4]; // doesn't seem like too much > trouble, > // copies the > values over > copyByReference = start[1..4][1..4]; // how in the world do you > do this? I believe there's no such thing as "rectangular slice" in D. start[1..4] is an array of 3 elements of type int[5]. Now you slice it once again with [1..4] and get an out-of-range exception because there's no element with index 3. You could, however, slice it as start[1..4][1..3], which is absolutely the same as start[2..3]. |
February 18, 2002 Re: Array slicing Associative and Rectangular arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | Pavel Minayev wrote: > "Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3C711ADA.15D57A6C@deming-os.org... > > > * Can you array slice associative arrays? IMHO, this is an almost must-have feature...but be careful, Pavel (playful grin), because it won't look right to have end-exclusive syntax here, either > > How exactly do you slice an associative array? With item indices? Then the same rules would apply as for normal arrays, including end-exclusive syntax... This is kind of what I'm thinking about: class Book {...}; Book[] booksOnShelf; ... Book[] booksToCheckOut = booksOnShelf["Anne of Green Gables" .. "Farenheit 451"]; You see, I'm trying a slice here to include the range of books, assuming that this associative array was indexed by title. I think this feature would be very useful for people using associative arrays. But presumably, I want to make sure that I get my copy of "Farenheit 451" (but nothing after it). How do I do that? You can't really do +1 to string. Of course, I could include up to "G" and then cut the extras off of the tail, but then the usefulness of slicing is greatly diminished. Today, my record on questions relating to arrays is about 0-2, so maybe there's some easy way to do this that I've missed (smirk) > > * What happens if you array slice a rectangular array and try to set it > > "by reference" to another array? > > int[5][5] start = ....; > > int[3][3] copyByValue,copyByReference; > > > > copyByValue[] = start[1..4][1..4]; // doesn't seem like too much > > trouble, > > // copies the > > values over > > copyByReference = start[1..4][1..4]; // how in the world do you > > do this? > > I believe there's no such thing as "rectangular slice" in D. start[1..4] is an array of 3 elements of type int[5]. Now you slice it once again with [1..4] and get an out-of-range exception because there's no element with index 3. You could, however, slice it as start[1..4][1..3], which is absolutely the same as start[2..3]. d'oh! You're right, of course. The "array slicing operator" returns an array with as many dimensions as it started with...so rectangular slicing isn't even an issue. Do it with a for loop :p -- The Villagers are Online! villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ] |
February 18, 2002 Re: Array slicing Associative and Rectangular arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | "Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3C713B38.3099017D@deming-os.org... > Book[] booksOnShelf; > ... > > Book[] booksToCheckOut = booksOnShelf["Anne of Green Gables" .. "Farenheit 451"]; > > You see, I'm trying a slice here to include the range of books, assuming that > this associative array was indexed by title. I think this feature would be > very useful for people using associative arrays. The key word here is "indexed". I'm not sure that D associative arrays are indexed - in fact, I'm not sure that there's _any_ strictly defined order of keys. As Walter has said before, the exact implementation of associative array is left to the concrete compiler. |
February 20, 2002 Re: Array slicing Associative and Rectangular arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | "Pavel Minayev" <evilone@omen.ru> wrote in message news:a4rij2$26n8$1@digitaldaemon.com... > The key word here is "indexed". I'm not sure that D associative arrays are > indexed - in fact, I'm not sure that there's _any_ strictly defined > order of keys. As Walter has said before, the exact implementation of > associative array is left to the concrete compiler. Correct, there is not a defined order to the values, and they are not "indexed" in the sense of looping through the members of an array by an index. The way to enumerate all the members is by converting the associative array to a dynamic array. |
February 20, 2002 Re: Array slicing Associative and Rectangular arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "Walter" <walter@digitalmars.com> wrote in message news:a4vpei$ng8$2@digitaldaemon.com... > Correct, there is not a defined order to the values, and they are not "indexed" in the sense of looping through the members of an array by an index. The way to enumerate all the members is by converting the associative array to a dynamic array. I'd still request some form of loop to scan through the entire associative array without converting it to dynamic key/value arrays. Let the order be "implementation-defined", there are many situations where it just doesn't matter. |
February 20, 2002 Re: Array slicing Associative and Rectangular arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | Pavel Minayev wrote:
> "Walter" <walter@digitalmars.com> wrote in message
> news:a4vpei$ng8$2@digitaldaemon.com...
>
>
>>Correct, there is not a defined order to the values, and they are not
>>"indexed" in the sense of looping through the members of an array by an
>>index. The way to enumerate all the members is by converting the
>>associative array to a dynamic array.
>>
>
> I'd still request some form of loop to scan through the entire
> associative array without converting it to dynamic key/value arrays.
> Let the order be "implementation-defined", there are many situations
> where it just doesn't matter.
Seconded.
-RB
|
February 21, 2002 Re: Array slicing Associative and Rectangular arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | Pavel Minayev wrote: > "Walter" <walter@digitalmars.com> wrote in message news:a4vpei$ng8$2@digitaldaemon.com... > > > Correct, there is not a defined order to the values, and they are not "indexed" in the sense of looping through the members of an array by an index. The way to enumerate all the members is by converting the associative array to a dynamic array. > > I'd still request some form of loop to scan through the entire associative array without converting it to dynamic key/value arrays. Let the order be "implementation-defined", there are many situations where it just doesn't matter. Is the result of strcmp() defined or implementation dependent? If the former, then strcmp()'s comparison algorithm would work fine for me.... -- The Villagers are Online! villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ] |
February 21, 2002 Re: Array slicing Associative and Rectangular arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | "Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3C75179F.EF2FE1BF@deming-os.org... > Is the result of strcmp() defined or implementation dependent? If the > former, then strcmp()'s comparison algorithm would work fine for me.... Don't forget that D associative arrays don't necessary have string keys. It's perfectly valid to have an int, float, or even Object as a key. Also, any "comparison algorithm" sorta limits the implementation - it either has to store the keys in that order in memory, or the iteration process will be quite slow (because a look-up is performed for each key in the table). How do you implement an ordered scan for a hash table, for example? |
February 21, 2002 Re: Array slicing Associative and Rectangular arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | > I believe there's no such thing as "rectangular slice" in D. start[1..4] is an array of 3 elements of type int[5].
Is that the second through fourth elements of start?
Or the first through third?
Neither seems to be what [1..4] is saying!
[1..4] clearly shows either a 1 and a 4 or a 'first' and a 'fourth'
inside the brackets (array).
Karl Bochert
|
Copyright © 1999-2021 by the D Language Foundation