www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - An struct copy constructor that can cope with an AA?

reply mark <mark qtrac.eu> writes:
I have this struct:

struct Deb {
     string name;
     ...
     Unit[string] tags; // set of tags

     Deb copy() const {
         Deb deb;
         ...
         foreach (key, value; tags) // XXX
             deb.tags[key] = value;
         return deb;
     }

     void clear() {
         name = "";
         ...
         tags.clear;
     }

...
}

I am populating an AA with these structs:

Deb[string] debForName;

I'm using this approach (pseudo-code):

Deb deb;
foreach (datum; data) {
     populateDeb(datum, deb);
     debForName[deb.name] = deb.copy; // YYY
     deb.clear;
}

(1) XXX Is there a nicer way to copy an AA?
(2) YYY Is there a nicer way to copy a struct? Use a copy 
constructor or implement a dup or idup?
Mar 09 2020
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 3/9/20 9:23 AM, mark wrote:
 I have this struct:
 
 struct Deb {
      string name;
      ...
      Unit[string] tags; // set of tags
 
      Deb copy() const {
          Deb deb;
          ...
          foreach (key, value; tags) // XXX
              deb.tags[key] = value;
          return deb;
      }
 
      void clear() {
          name = "";
          ...
          tags.clear;
      }
 
 ....
 }
 
 I am populating an AA with these structs:
 
 Deb[string] debForName;
 
 I'm using this approach (pseudo-code):
 
 Deb deb;
 foreach (datum; data) {
      populateDeb(datum, deb);
      debForName[deb.name] = deb.copy; // YYY
      deb.clear;
 }
 
 (1) XXX Is there a nicer way to copy an AA?
aa.dup. However, this isn't going to work in a const member function (though technically there shouldn't be anything wrong with it).
 (2) YYY Is there a nicer way to copy a struct? Use a copy constructor or 
 implement a dup or idup?
I would name it dup instead of copy for consistency with D. A copy constructor is pretty heavy for a struct to do a complete duplication of the AA. You should have to opt-in to that. -Steve
Mar 09 2020
parent reply mark <mark qtrac.eu> writes:
On Monday, 9 March 2020 at 14:45:15 UTC, Steven Schveighoffer 
wrote:
 On 3/9/20 9:23 AM, mark wrote:
 I have this struct:
[snip]
 I would name it dup instead of copy for consistency with D. A 
 copy constructor is pretty heavy for a struct to do a complete 
 duplication of the AA. You should have to opt-in to that.

 -Steve
Thanks, I'd already realised I ought to rename it .dup. Also, I only actually need to create it from the keys since I'm using the tags AA as a set so every value is the same. I did try tags.dup but got this: src/deb.d(25,24): Error: cannot implicitly convert expression dup(this.tags) of type const(void[0])[string] to void[0][string]
Mar 09 2020
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 3/9/20 11:49 AM, mark wrote:
 On Monday, 9 March 2020 at 14:45:15 UTC, Steven Schveighoffer wrote:
 On 3/9/20 9:23 AM, mark wrote:
 I have this struct:
[snip]
 I would name it dup instead of copy for consistency with D. A copy 
 constructor is pretty heavy for a struct to do a complete duplication 
 of the AA. You should have to opt-in to that.
Thanks, I'd already realised I ought to rename it .dup. Also, I only actually need to create it from the keys since I'm using the tags AA as a set so every value is the same. I did try tags.dup but got this: src/deb.d(25,24): Error: cannot implicitly convert expression dup(this.tags) of type const(void[0])[string] to void[0][string]
Right, because it's inside a const member function, the tags member is const. Right now, it looks like dup does not work even for value types (e.g. const int[int] -> int[int]) let alone types with indirection (it should also work for string keys). If there's not an issue already, someone should file it. -Steve
Mar 09 2020