March 27, 2002 Re: String Comparison and Dynamic Arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | "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... > > 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. > 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--; > ? Because then I'd be spending my life explaining why ++ is not implemented but -- is ?? <g> |
March 27, 2002 Re: String Comparison and Dynamic Arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | 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... > > 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. |
March 27, 2002 Re: String Comparison and Dynamic Arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "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 ~=. |
March 27, 2002 Re: String Comparison and Dynamic Arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | 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))) ] |
March 27, 2002 Re: String Comparison and Dynamic Arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | > array.autoextend(3)[] ... // int argument, autoextends by tripling > length Oops, 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))) ] |
March 27, 2002 Re: String Comparison and Dynamic Arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | "Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3CA1E868.4BF36E49@deming-os.org... > > array.autoextend(3)[] ... // int argument, autoextends by tripling > > length > > Oops, 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))) ] > > Don't push your luck... :) -- Stijn OddesE_XYZ@hotmail.com http://OddesE.cjb.net __________________________________________ Remove _XYZ from my address when replying by mail |
March 27, 2002 Re: String Comparison and Dynamic Arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean L. Palmer | "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++ 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 Delphi 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 |
March 27, 2002 Re: String Comparison and Dynamic Arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to OddesE | "OddesE" <OddesE_XYZ@hotmail.com> wrote in message news:a7ti96$2qni$1@digitaldaemon.com... > > Capacity - Count = WastedSpace :) > Delphi typo... :) Should be: Capacity - Count == WastedSpace |
March 29, 2002 Re: String Comparison and Dynamic Arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | "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... > > > 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 ~=. That 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 ++. |
March 29, 2002 Re: String Comparison and Dynamic Arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "Walter" <walter@digitalmars.com> wrote in message news:a82km4$2m2l$2@digitaldaemon.com... > > 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 ~=. > > That 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 ++. It wouldn't be slower than ~=, right? And ~= is there... |
Copyright © 1999-2021 by the D Language Foundation