D - String Comparison and Dynamic Arrays?
- Gary (19/19) Mar 26 2002 Walter,
- Pavel Minayev (7/17) Mar 26 2002 Currently, you cannot use == and friends to compare strings, and
- Gary (5/23) Mar 26 2002 Walter... Any comments?
- Walter (10/16) Mar 26 2002 I disabled setting array.length for operators other than assignment beca...
- Gary (5/22) Mar 26 2002 lvalue)
- Walter (18/23) Mar 26 2002 need,
- Gary (6/29) Mar 26 2002 input
- Walter (4/6) Mar 26 2002 was
- Russ Lewis (27/30) Mar 27 2002 How about some sort of array index syntax that automatically doubles the...
- Russ Lewis (7/9) Mar 27 2002 Oops, meant to type
- OddesE (10/19) Mar 27 2002 tripling
- Gary (5/22) Mar 26 2002 Also... when do you expect string comparison to be incorporated?
- Pavel Minayev (10/14) Mar 26 2002 because
- Walter (4/18) Mar 26 2002 Because then I'd be spending my life explaining why ++ is not implemente...
- Pavel Minayev (5/7) Mar 27 2002 Then, implement ++ as if a new element with default value of desired typ...
- Walter (6/13) Mar 29 2002 implemented
- Pavel Minayev (6/12) Mar 29 2002 type
- Walter (4/11) Mar 29 2002 Yes, but ~= is useful for concatenating strings with more than one entry...
- Pavel Minayev (13/15) Mar 29 2002 ++
- Walter (4/14) Apr 07 2002 That turned out to have problems implementing it. The trouble was if som...
- Pavel Minayev (4/19) Apr 07 2002 someone
- Walter (7/26) Apr 07 2002 char[] a = new char[10];
- Pavel Minayev (3/8) Apr 08 2002 Hm... I thought that, when length increases, the memory is reallocated!
- Walter (3/11) Apr 12 2002 Yes, and it must be because of that scenario above.
- Sean L. Palmer (11/28) Mar 27 2002 Why not just double the darray's allocated space when array.length++ exc...
Walter, I can successfully use the switch/case construct to compare equality of a string, but the same can't be said for using the "if" statement. For example: if (a == "Hello") ... doesn't succeed. whereas: switch (a) { case "Hello": ... } does work. I'm also getting an error during compile ('array.length' is not an lvalue) with the following: int[] array; array[array.length++] = 0; array[array.length++] = 1; array[array.length] = 2; Gary.
Mar 26 2002
"Gary" <gedumer bcpl.net> wrote in message news:a7qll4$1870$1 digitaldaemon.com...I can successfully use the switch/case construct to compare equality of a string, but the same can't be said for using the "if" statement. For example: if (a == "Hello") ... doesn't succeed.Currently, you cannot use == and friends to compare strings, and arrays in general. You have to use cmp(): if (!cmp(a, "Hello")) ... Hopefully some operator to compare arrays will be added.I'm also getting an error during compile ('array.length' is not an lvalue) with the following: int[] array; array[array.length++] = 0; array[array.length++] = 1;This one (and array.length--) I was asking once, but no luck =)
Mar 26 2002
Walter... Any comments? Pavel Minayev <evilone omen.ru> wrote in message news:a7qnim$19c9$1 digitaldaemon.com..."Gary" <gedumer bcpl.net> wrote in message news:a7qll4$1870$1 digitaldaemon.com...aI can successfully use the switch/case construct to compare equality oflvalue)string, but the same can't be said for using the "if" statement. For example: if (a == "Hello") ... doesn't succeed.Currently, you cannot use == and friends to compare strings, and arrays in general. You have to use cmp(): if (!cmp(a, "Hello")) ... Hopefully some operator to compare arrays will be added.I'm also getting an error during compile ('array.length' is not anwith the following: int[] array; array[array.length++] = 0; array[array.length++] = 1;This one (and array.length--) I was asking once, but no luck =)
Mar 26 2002
"Gary" <gedumer bcpl.net> wrote in message news:a7qll4$1870$1 digitaldaemon.com...I'm also getting an error during compile ('array.length' is not an lvalue) with the following: int[] array; array[array.length++] = 0; array[array.length++] = 1; array[array.length] = 2;I disabled setting array.length for operators other than assignment because it reallocates the array, which is an expensive operation. Adding array elements one by one, incrementing the array length each time, will have terrible runtime performance. The way to do it is to preallocate your best guess as to what you'll need, stuff the array elements, and then do an assignment to set the final size. If, as you're stuffing array elements, you run out of length, doubling the existing array length is a good strategy.
Mar 26 2002
Walter <walter digitalmars.com> wrote in message news:a7r3am$1gcu$1 digitaldaemon.com..."Gary" <gedumer bcpl.net> wrote in message news:a7qll4$1870$1 digitaldaemon.com...lvalue)I'm also getting an error during compile ('array.length' is not anbecausewith the following: int[] array; array[array.length++] = 0; array[array.length++] = 1; array[array.length] = 2;I disabled setting array.length for operators other than assignmentit reallocates the array, which is an expensive operation. Adding array elements one by one, incrementing the array length each time, will have terrible runtime performance. The way to do it is to preallocate your best guess as to what you'll need, stuff the array elements, and then do an assignment to set the final size. If, as you're stuffing array elements, you run out of length, doubling the existing array length is a good strategy.Exactly... how would you do that?
Mar 26 2002
"Gary" <gedumer bcpl.net> wrote in message news:a7r5iq$1hg6$1 digitaldaemon.com...need,The way to do it is to preallocate your best guess as to what you'llsize.stuff the array elements, and then do an assignment to set the finaltheIf, as you're stuffing array elements, you run out of length, doublingarray.length = 100; // guess for (i = 0; 1; i++) { c = getinput(); if (!c) break; if (i == array.length) array.length = array.length * 2; array[i] = c; } array.length = i; Picking a good initial guess is an art, but you usually can pick a value covering 99% of the cases. For example, suppose you are gathering user input from the console - it's unlikely to be longer than 80.existing array length is a good strategy.Exactly... how would you do that?
Mar 26 2002
Walter <walter digitalmars.com> wrote in message news:a7r7ok$1ing$1 digitaldaemon.com..."Gary" <gedumer bcpl.net> wrote in message news:a7r5iq$1hg6$1 digitaldaemon.com...inputneed,The way to do it is to preallocate your best guess as to what you'llsize.stuff the array elements, and then do an assignment to set the finaltheIf, as you're stuffing array elements, you run out of length, doublingarray.length = 100; // guess for (i = 0; 1; i++) { c = getinput(); if (!c) break; if (i == array.length) array.length = array.length * 2; array[i] = c; } array.length = i; Picking a good initial guess is an art, but you usually can pick a value covering 99% of the cases. For example, suppose you are gathering userexisting array length is a good strategy.Exactly... how would you do that?from the console - it's unlikely to be longer than 80.WOW!!! That's a far cry from the example in the docs... which is what I was trying to follow. I LIKE IT:) Thanks... Gary.
Mar 26 2002
"Gary" <gedumer bcpl.net> wrote in message news:a7r8b3$1j12$1 digitaldaemon.com...WOW!!! That's a far cry from the example in the docs... which is what Iwastrying to follow. I LIKE IT:)Looks like I should amend the docs <g>.
Mar 26 2002
Walter wrote:Picking a good initial guess is an art, but you usually can pick a value covering 99% of the cases. For example, suppose you are gathering user input from the console - it's unlikely to be longer than 80.How about some sort of array index syntax that automatically doubles the array length if the index is beyond the length? Ok, I know the syntax I have below is terrible, but what if you could write something like: array.length = 100; // guess for(i=0; 1; i++) { c = getinput(); if(!c) break; array.autoextend[i] = c; // if i >= array.length, doubles array.length automatically } array.length = i; This doesn't add any code to the generated output (since we'd have to do the test anyhow), but something like this would allow for a MUCH cleaneer syntax. NOTE: I'm not suggesting that we automatically extend on ALL index operations...only on specially marked ones. Also, you could, potentially, allow an argument on the autoextend to tell the compiler by what factor to increase the length: array.autoextend(1.2)[i] ... // float argument, autoextends by 20% array.autoextend(3)[] ... // int argument, autoextends by tripling length -- 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))) ]
Mar 27 2002
array.autoextend(3)[] ... // int argument, autoextends by tripling lengthOops, meant to type array.autoextend(3)[i] -- 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))) ]
Mar 27 2002
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message news:3CA1E868.4BF36E49 deming-os.org...triplingarray.autoextend(3)[] ... // int argument, autoextends byDon't push your luck... :) -- Stijn OddesE_XYZ hotmail.com http://OddesE.cjb.net __________________________________________ Remove _XYZ from my address when replying by maillengthOops, meant to type array.autoextend(3)[i] -- 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))) ]
Mar 27 2002
Also... when do you expect string comparison to be incorporated? Walter <walter digitalmars.com> wrote in message news:a7r3am$1gcu$1 digitaldaemon.com..."Gary" <gedumer bcpl.net> wrote in message news:a7qll4$1870$1 digitaldaemon.com...lvalue)I'm also getting an error during compile ('array.length' is not anbecausewith the following: int[] array; array[array.length++] = 0; array[array.length++] = 1; array[array.length] = 2;I disabled setting array.length for operators other than assignmentit reallocates the array, which is an expensive operation. Adding array elements one by one, incrementing the array length each time, will have terrible runtime performance. The way to do it is to preallocate your best guess as to what you'll need, stuff the array elements, and then do an assignment to set the final size. If, as you're stuffing array elements, you run out of length, doubling the existing array length is a good strategy.
Mar 26 2002
"Walter" <walter digitalmars.com> wrote in message news:a7r3am$1gcu$1 digitaldaemon.com...I disabled setting array.length for operators other than assignmentbecauseit reallocates the array, which is an expensive operation. Adding array elements one by one, incrementing the array length each time, will have terrible runtime performance.You're probably right... but what about -- and -=? They sound like a replacement for pop_back() of STL containers - a thing that is of common use. Currently, I have to use s.length = s.length - 1; Why not s.length--; ?
Mar 26 2002
"Pavel Minayev" <evilone omen.ru> wrote in message news:a7rirf$1ofi$1 digitaldaemon.com..."Walter" <walter digitalmars.com> wrote in message news:a7r3am$1gcu$1 digitaldaemon.com...Because then I'd be spending my life explaining why ++ is not implemented but -- is ?? <g>I disabled setting array.length for operators other than assignmentbecauseit reallocates the array, which is an expensive operation. Adding array elements one by one, incrementing the array length each time, will have terrible runtime performance.You're probably right... but what about -- and -=? They sound like a replacement for pop_back() of STL containers - a thing that is of common use. Currently, I have to use s.length = s.length - 1; Why not s.length--; ?
Mar 26 2002
"Walter" <walter digitalmars.com> wrote in message news:a7rq5d$1s0b$3 digitaldaemon.com...Because then I'd be spending my life explaining why ++ is not implemented but -- is ?? <g>Then, implement ++ as if a new element with default value of desired type is added. That is, for int[], ++ adds -, for float[] it will be NAN etc... the code would be essentially the same as for ~=.
Mar 27 2002
"Pavel Minayev" <evilone omen.ru> wrote in message news:a7sde6$26vb$1 digitaldaemon.com..."Walter" <walter digitalmars.com> wrote in message news:a7rq5d$1s0b$3 digitaldaemon.com...implementedBecause then I'd be spending my life explaining why ++ is notThat would work, but then I'm back to the original point that code that uses ++ on the .length most likely has a severe performance problem, so it's discouraged by not supporting ++.but -- is ?? <g>Then, implement ++ as if a new element with default value of desired type is added. That is, for int[], ++ adds -, for float[] it will be NAN etc... the code would be essentially the same as for ~=.
Mar 29 2002
"Walter" <walter digitalmars.com> wrote in message news:a82km4$2m2l$2 digitaldaemon.com...typeThen, implement ++ as if a new element with default value of desiredetc...is added. That is, for int[], ++ adds -, for float[] it will be NANusesthe code would be essentially the same as for ~=.That would work, but then I'm back to the original point that code that++ on the .length most likely has a severe performance problem, so it's discouraged by not supporting ++.It wouldn't be slower than ~=, right? And ~= is there...
Mar 29 2002
"Pavel Minayev" <evilone omen.ru> wrote in message news:a82n7g$oq9$1 digitaldaemon.com..."Walter" <walter digitalmars.com> wrote in message news:a82km4$2m2l$2 digitaldaemon.com...Yes, but ~= is useful for concatenating strings with more than one entry. ++ by definition only adds one, and so is the worst case solution :-(That would work, but then I'm back to the original point that code thatuses++ on the .length most likely has a severe performance problem, so it's discouraged by not supporting ++.It wouldn't be slower than ~=, right? And ~= is there...
Mar 29 2002
"Walter" <walter digitalmars.com> wrote in message news:a82sab$2fjh$1 digitaldaemon.com...Yes, but ~= is useful for concatenating strings with more than one entry.++by definition only adds one, and so is the worst case solution :-(I often use ~= to build arrays element-by-element: char[] token; ... do { token ~= c; in.read(c); } while (isalpha(c)); The same "worst" case. I thought arrays try to compensate it by allocating memory in blocks?
Mar 29 2002
"Pavel Minayev" <evilone omen.ru> wrote in message news:a83g36$1sns$1 digitaldaemon.com...I often use ~= to build arrays element-by-element: char[] token; ... do { token ~= c; in.read(c); } while (isalpha(c)); The same "worst" case. I thought arrays try to compensate it by allocating memory in blocks?That turned out to have problems implementing it. The trouble was if someone pointed to a slice adjacent to it at a higher memory address.
Apr 07 2002
"Walter" <walter digitalmars.com> wrote in message news:a8p4l7$105m$1 digitaldaemon.com..."Pavel Minayev" <evilone omen.ru> wrote in message news:a83g36$1sns$1 digitaldaemon.com...someoneI often use ~= to build arrays element-by-element: char[] token; ... do { token ~= c; in.read(c); } while (isalpha(c)); The same "worst" case. I thought arrays try to compensate it by allocating memory in blocks?That turned out to have problems implementing it. The trouble was ifpointed to a slice adjacent to it at a higher memory address.I can't understand... what do you mean by "adjancent slice"?
Apr 07 2002
"Pavel Minayev" <evilone omen.ru> wrote in message news:a8pask$1d5t$1 digitaldaemon.com..."Walter" <walter digitalmars.com> wrote in message news:a8p4l7$105m$1 digitaldaemon.com...char[] a = new char[10]; char[] b = a[5..10]; a.length = 5; a.length = 10; Oops! I just overwrote b!"Pavel Minayev" <evilone omen.ru> wrote in message news:a83g36$1sns$1 digitaldaemon.com...someoneI often use ~= to build arrays element-by-element: char[] token; ... do { token ~= c; in.read(c); } while (isalpha(c)); The same "worst" case. I thought arrays try to compensate it by allocating memory in blocks?That turned out to have problems implementing it. The trouble was ifpointed to a slice adjacent to it at a higher memory address.I can't understand... what do you mean by "adjancent slice"?
Apr 07 2002
"Walter" <walter digitalmars.com> wrote in message news:a8r7up$2nc$1 digitaldaemon.com...char[] a = new char[10]; char[] b = a[5..10]; a.length = 5; a.length = 10; Oops! I just overwrote b!Hm... I thought that, when length increases, the memory is reallocated!
Apr 08 2002
"Pavel Minayev" <evilone omen.ru> wrote in message news:a8rnff$14to$1 digitaldaemon.com..."Walter" <walter digitalmars.com> wrote in message news:a8r7up$2nc$1 digitaldaemon.com...Yes, and it must be because of that scenario above.char[] a = new char[10]; char[] b = a[5..10]; a.length = 5; a.length = 10; Oops! I just overwrote b!Hm... I thought that, when length increases, the memory is reallocated!
Apr 12 2002
Why not just double the darray's allocated space when array.length++ exceeds the allocated space? Oh, because you're only storing the length, assuming the allocated size == the length, always. Think of it like a std::vector<>::push_back(). Yeah it's inefficient but sometimes it's the most elegant way to code an algorithm, and not everybody is always primarily concerned with speed. Think college D courses. ;) Sean "Walter" <walter digitalmars.com> wrote in message news:a7r3am$1gcu$1 digitaldaemon.com..."Gary" <gedumer bcpl.net> wrote in message news:a7qll4$1870$1 digitaldaemon.com...lvalue)I'm also getting an error during compile ('array.length' is not anbecausewith the following: int[] array; array[array.length++] = 0; array[array.length++] = 1; array[array.length] = 2;I disabled setting array.length for operators other than assignmentit reallocates the array, which is an expensive operation. Adding array elements one by one, incrementing the array length each time, will have terrible runtime performance. The way to do it is to preallocate your best guess as to what you'll need, stuff the array elements, and then do an assignment to set the final size. If, as you're stuffing array elements, you run out of length, doubling the existing array length is a good strategy.
Mar 27 2002
"Sean L. Palmer" <spalmer iname.com> wrote in message news:a7s781$23gk$1 digitaldaemon.com...Why not just double the darray's allocated space when array.length++exceedsthe allocated space? Oh, because you're only storing the length, assuming the allocated size == the length, always. Think of it like a std::vector<>::push_back(). Yeah it's inefficient but sometimes it's the most elegant way to code an algorithm, and noteverybodyis always primarily concerned with speed. Think college D courses. ;) SeanDelphi TList uses a Capacity property. Count is the exact number of elements in the list, Capacity is the amount of allocated elements. Capacity - Count = WastedSpace :) -- Stijn OddesE_XYZ hotmail.com http://OddesE.cjb.net __________________________________________ Remove _XYZ from my address when replying by mail
Mar 27 2002
"OddesE" <OddesE_XYZ hotmail.com> wrote in message news:a7ti96$2qni$1 digitaldaemon.com...Capacity - Count = WastedSpace :)Delphi typo... :) Should be: Capacity - Count == WastedSpace
Mar 27 2002