die gleiche Struktur in Array und Mappings

Oct 06 2020

Nehmen wir an, ich habe eine struct.

struct test {
  uint x;
}

test[] all;
mappings(uint => test) map;

function bla(){
   test memory structTest = test(5);
   all.push(structTest);
   map[10] = structTest;
}

Wird der Code nach dem Code structTestan einem Ort gespeichert und dann speichern mein Array und meine Karte die Verweise darauf oder werden sie separat in Karte und Array gespeichert?

Wenn letzteres der Fall ist, wie kann ich eine Struktur erstellen und in Mapping und Array speichern, damit ich keinen Speicherplatz verschwende?

Antworten

2 goodvibration Oct 06 2020 at 20:58

Eine Kopie von structTestwird separat im Array und im Mapping gespeichert.

Um diese Redundanz zu beseitigen, können Sie anstelle von Werten ein Array von Schlüsseln verwalten:

uint[] all; // instead of `test[] all;`
mappings (uint => test) map;

function bla() {
    test memory structTest = test(5);
    all.push(10); // instead of `all.push(structTest);`
    map[10] = structTest;
}