www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Difference between concatenation and appendation

reply "WhatMeWorry" <kc_heaser yahoo.com> writes:
Ok, I just made up that word. But what is the difference between 
appending and concatenating?  Page 100 of TPDL says "The result 
of the concatenation is a new array..." and the section on 
appending talks about possibly needing expansion and reallocation 
of memory.

But I still don't feel like I have a grasp on the subtleties 
between them. Can someone give a short and sweet "rule of thumb"?

It might be so obvious that I'll regret posting this.

Thanks.
Jan 25 2015
next sibling parent =?UTF-8?B?IuWyqeWAiSDmvqoi?= <mio.iwakura gmail.com> writes:
On Monday, 26 January 2015 at 01:17:17 UTC, WhatMeWorry wrote:
 Ok, I just made up that word. But what is the difference 
 between appending and concatenating?  Page 100 of TPDL says 
 "The result of the concatenation is a new array..." and the 
 section on appending talks about possibly needing expansion and 
 reallocation of memory.

 But I still don't feel like I have a grasp on the subtleties 
 between them. Can someone give a short and sweet "rule of 
 thumb"?

 It might be so obvious that I'll regret posting this.

 Thanks.
I'm no expert, so take what I say with a grain of salt. That said, here is my understanding: When you append to an array with ~=, it attempts to reallocate the array in-place, meaning it allocates on top of the already used space, but grabs some more space past the end of the array. If there isn't enough space after the array then obviously it can't do that, so it allocates memory somewhere else that it can fit and then it copies the contents of the array to the new location. If you were to do myArray = myArray ~ moreStuff; I assume this is no different from ~=. Conceptually ~= is just syntactic sugar in the same way that += or -= is, you are just doing a concatenation and then updating the array to point to the new result. The fact that it can reallocate in place if there is enough space is just like an optimization, in my mind.
Jan 25 2015
prev sibling next sibling parent reply "Laeeth Isharc" <Laeeth.nospam nospam-laeeth.com> writes:
On Monday, 26 January 2015 at 01:17:17 UTC, WhatMeWorry wrote:
 Ok, I just made up that word. But what is the difference 
 between appending and concatenating?  Page 100 of TPDL says 
 "The result of the concatenation is a new array..." and the 
 section on appending talks about possibly needing expansion and 
 reallocation of memory.

 But I still don't feel like I have a grasp on the subtleties 
 between them. Can someone give a short and sweet "rule of 
 thumb"?

 It might be so obvious that I'll regret posting this.

 Thanks.
At the risk of the blind leading the blind (I am no expert), I think concatenation and append are used as synonyms (the same meaning is meant). a~=b or a=a~b If there isn't enough space then the whole array is reallocated. You can see this/change this property by reading capacity or calling reserve. If you want to do lots of appends / concatenates then use appender in std.array which is faster and more efficient.
Jan 25 2015
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Laeeth Isharc:

 I think concatenation and append are used as synonyms (the same 
 meaning is meant).  a~=b or a=a~b
a=a~b always allocates a new array, while a~=b sometimes re-allocates in place. Bye, bearophile
Jan 25 2015
next sibling parent "Laeeth Isharc" <Laeeth.nospam nospam-laeeth.com> writes:
On Monday, 26 January 2015 at 01:57:04 UTC, bearophile wrote:
 Laeeth Isharc:

 I think concatenation and append are used as synonyms (the 
 same meaning is meant).  a~=b or a=a~b
a=a~b always allocates a new array, while a~=b sometimes re-allocates in place. Bye, bearophile
Thanks. That makes sense.
Jan 25 2015
prev sibling parent "WhatMeWorry" <kc_heaser yahoo.com> writes:
On Monday, 26 January 2015 at 01:57:04 UTC, bearophile wrote:
 Laeeth Isharc:

 I think concatenation and append are used as synonyms (the 
 same meaning is meant).  a~=b or a=a~b
a=a~b always allocates a new array, while a~=b sometimes re-allocates in place. Bye, bearophile
Perfect! Thank you. I'll scribble your quote you in the margin of my TDPL book.
Jan 25 2015
prev sibling parent Jonathan M Davis via Digitalmars-d-learn writes:
On Monday, January 26, 2015 01:17:15 WhatMeWorry via Digitalmars-d-learn wrote:
 Ok, I just made up that word. But what is the difference between
 appending and concatenating?  Page 100 of TPDL says "The result
 of the concatenation is a new array..." and the section on
 appending talks about possibly needing expansion and reallocation
 of memory.

 But I still don't feel like I have a grasp on the subtleties
 between them. Can someone give a short and sweet "rule of thumb"?

 It might be so obvious that I'll regret posting this.
~ is the concatenation operator. ~= is the append operator. ~ takes two objects (most typically arrays, but user-defined types can define the same operators) and returns a new one which contains the data from the first one followed by the data from the second without affecting either object. ~= takes two objects and adds the data from the second one to the end of the first one (without affecting the second object). ~ is forced to allocate, because it's creating a new object. Whether ~= allocates depends on the data involved and the implementation, though ideally, it would avoid allocation if it can. In the case of arrays rather than user-defined objects, ~= is managed by the GC. So, whether ~= allocates or not depends on whether there's room/capacity in the block of memory that the array is a slice of after the array. If there's enough room, then it will just increase the size of the array, and put the new data in that memory, but if there isn't enough room (e.g. because the memory block doesn't have enough room or because another array refers to a pointer farther in the block of memory than the one being appended to), then a new block of memory is allocated, the data is then assigned to there, and the array is changed to be a slice of that memory block. For user-defined types, it depends entirely on how ~= is implemented, but it's probably going to either be doing something similar or be forced to reallocate every time. In any case, the main difference between ~ and ~= is that ~ creates a new array or object _every_ time, and ~= mutates the first argument and will generally only result in an allocation if it has to (particularly if you're dealing with arrays). I'd sugges that you read this article on D arrays: http://dlang.org/d-array-article.html It mixes up its terminology a bit with regards to dynamic arrays (the language considers int[] to be a dynamic array, whereas the article refers to the GC-allocated block of memory that the array refers to as being the dynamic array), but it should definitely clarify a lot about D arrays for you. - Jonathan M Davis
Jan 25 2015