Menu

#include “twodlinkedlist.h” #include <map> #include <algorithm>   /**  *  Deallocates any associated dynamic memory and sets private attributes appropriately  *  *  @post this …

#include “twodlinkedlist.h”#include

#include
 /** *  Deallocates any associated dynamic memory and sets private attributes appropriately * *  @post this list is empty */void TwoDLinkedList::Clear() {    TwoDNode* row = northwest;    while (row != nullptr) {        TwoDNode* col = row;        row = row->south;        while (col != nullptr) {            TwoDNode* next = col->east;            delete col;            col = next;        }    }    northwest = nullptr;    southeast = nullptr;    blockdimx = 0;    blockdimy = 0;}
 /** *  Copies the data and structure of otherlist into this list * *  @post this list is a physically separate copy of otherlist */void TwoDLinkedList::Copy(const TwoDLinkedList& otherlist) {    if (this == &otherlist) {        return; // Prevent self-assignment    }
     Clear();
     if (otherlist.northwest == nullptr) {        northwest = nullptr;        southeast = nullptr;        return;    }
     blockdimx = otherlist.blockdimx;    blockdimy = otherlist.blockdimy;
     std::map node_map;
     TwoDNode* other_current = otherlist.northwest;    TwoDNode* prev_row = nullptr;    TwoDNode* current = nullptr;    TwoDNode* first_in_row = nullptr;
     while (other_current != nullptr) {        if (first_in_row == nullptr) {            first_in_row = new TwoDNode();            current = first_in_row;        } else {            current->south = new TwoDNode();            current = current->south;        }        current->data = other_current->data;        node_map[other_current] = current;
         TwoDNode* other_row_current = other_current->east;        TwoDNode* row_current = current;        while (other_row_current != nullptr) {            row_current->east = new TwoDNode();            row_current = row_current->east;            row_current->data = other_row_current->data;            node_map[other_row_current] = row_current;            other_row_current = other_row_current->east;        }
         if (prev_row != nullptr) {            TwoDNode* temp_prev = prev_row;            TwoDNode* temp_curr = first_in_row;            while (temp_prev != nullptr && temp_curr != nullptr) {                temp_prev->south = temp_curr;                temp_curr->north = temp_prev;                temp_prev = temp_prev->east;                temp_curr = temp_curr->east;            }        }
         prev_row = first_in_row;        first_in_row = nullptr;        other_current = other_current->south;    }
     northwest = node_map[otherlist.northwest];    southeast = node_map[otherlist.southeast];}
 /** *  Default constructor creates an empty list */TwoDLinkedList::TwoDLinkedList() {    northwest = nullptr;    southeast = nullptr;    blockdimx = 0;    blockdimy = 0;}
 /** *  Builds the structure of this list from the provided parameters. *  The dimension of individual blocks will be an even division *  of the input PNG’s dimensions by blockdimx or blockdimy. *  Assume that supplied PNG dimensions will be divisible by *  blockdimx and blockdimy. * *  @pre PNG horizontal dimension is divisible by blockdimx *  @pre PNG vertical dimension is divisible by blockdimy * *  Example: PNG dimensions 320×240 *           blockdimx = 4 *           blockdimy = 2 *           The constructed list will have the following Block and node structure: * *                +———–+     +———–+     +———–+     +———–+ *  northwest –> |           | –> |           | –> |           | –> |           | *                |  80 x 120 |     |  80 x 120 |     |  80 x 120 |     |  80 x 120 | *                |           | <-- |           | <-- |           | <-- |           | *                +-----------+     +-----------+     +-----------+     +-----------+ *                   |     ^           |     ^           |     ^           |     ^ *                   v     |           v     |           v     |           v     | *                +-----------+     +-----------+     +-----------+     +-----------+ *                |           | --> |           | –> |           | –> |           | *                |  80 x 120 |     |  80 x 120 |     |  80 x 120 |     |  80 x 120 | *                |           | <-- |           | <-- |           | <-- |           | <-- southeast *                +-----------+     +-----------+     +-----------+     +-----------+ */void TwoDLinkedList::Build(PNG& img, unsigned int blockdimx, unsigned int blockdimy) {    Clear();      this->blockdimx = blockdimx;    this->blockdimy = blockdimy;
     unsigned int img_width = img.width();    unsigned int img_height = img.height();
     TwoDNode* prev_row = nullptr;    TwoDNode* first_in_row = nullptr;    TwoDNode* last_node = nullptr;
     for (unsigned int y = 0; y < img_height; y += blockdimy) {        TwoDNode* current_row = nullptr;        for (unsigned int x = 0; x < img_width; x += blockdimx) {            TwoDNode* new_node = new TwoDNode();            new_node->data.Build(blockdimx, blockdimy, y, x, img);
             if (first_in_row == nullptr) {                first_in_row = new_node;                if (northwest == nullptr) {                    northwest = first_in_row;                }            }
             if (current_row != nullptr) {                current_row->east = new_node;                new_node->west = current_row;            }            current_row = new_node;
             if (prev_row != nullptr) {                TwoDNode* temp_prev = prev_row;                TwoDNode* temp_curr = first_in_row;                while (temp_prev != nullptr && temp_curr != nullptr) {                    temp_prev->south = temp_curr;                    temp_curr->north = temp_prev;                    temp_prev = temp_prev->east;                    temp_curr = temp_curr->east;                }            }        }        prev_row = first_in_row;        first_in_row = nullptr;        last_node = current_row;    }
     southeast = last_node;}
 /** *  Returns the horizontal dimension of this list (in blocks) */unsigned int TwoDLinkedList::GetBlockDimensionX() const {    if (!northwest) return 0;
     unsigned int count = 0;    TwoDNode* current = northwest;    while (current) {        count++;        current = current->east;    }    return count;}
 /** *  Returns the vertical dimension of this list (in blocks) */unsigned int TwoDLinkedList::GetBlockDimensionY() const {    if (!northwest) return 0;
     unsigned int count = 0;    TwoDNode* current = northwest;    while (current) {        count++;        current = current->south;    }    return count;}
 /** *  Places this lists’s color data into a PNG of appropriate dimensions *  @param scale – integer multiplier for dimensions. 0 = scale by 0.5. */PNG TwoDLinkedList::Render(unsigned int scale) const {    if (!northwest) return PNG();
     unsigned int width = blockdimx * GetBlockDimensionX() * scale;    unsigned int height = blockdimy * GetBlockDimensionY() * scale;    PNG result(width, height);
     TwoDNode* row_start = northwest;    unsigned int row = 0;    while (row_start != nullptr) {        TwoDNode* current = row_start;        unsigned int col = 0;        while (current != nullptr) {            current->data.Render(scale, row * blockdimy * scale, col * blockdimx * scale, result);            col++;            current = current->east;        }        row++;        row_start = row_start->south;    }
     return result;}
 /** *  Reorganizes the list and node contents so that a rendered image will be flipped horizontally. *  This must be achieved by pointer manipulation. Do not deallocate any existing nodes, and *  do not allocate any new nodes. * *  Ensure that the integrity of north/south/east/west pointers of all nodes is maintained. * *  You may use up to O(n) time and space complexity, where n is the total number of nodes in the list. * *  Example: A, B, C, D etc. are physical nodes (each at a particular address) *  before: *   northwest -> A <-> B <-> C <-> D *                ^     ^     ^     ^ *                v     v     v     v *                E <-> F <-> G <-> H *                ^     ^     ^     ^ *                v     v     v     v *                I <-> J <-> K <-> L  <- southeast * *  after: *   northwest -> D <-> C <-> B <-> A *                ^     ^     ^     ^ *                v     v     v     v *                H <-> G <-> F <-> E *                ^     ^     ^     ^ *                v     v     v     v *                L <-> K <-> J <-> I  <- southeast */void TwoDLinkedList::FlipHorizontal() {    TwoDNode* row_start = northwest;    while (row_start != nullptr) {        TwoDNode* current = row_start;        TwoDNode* prev = nullptr;        TwoDNode* next;          while (current != nullptr) {            next = current->east;            current->east = prev;            current->west = next;            prev = current;            current = next;        }
         row_start->east = nullptr;        row_start = prev;    }
     // Swap northwest and southeast    std::swap(northwest, southeast);}
codes above , and system  output  : TwoDLinkedList::image constructor ——————————————————————————- testpa1.cpp:221 ……………………………………………………………………. testpa1.cpp:241: FAILED:  REQUIRE( vec_width == 16 ) with expansion:  52 == 16 ——————————————————————————- TwoDLinkedList::Build, GetBlockDimensionX, GetBlockDimensionY ——————————————————————————- testpa1.cpp:247 ……………………………………………………………………. testpa1.cpp:259: FAILED:  REQUIRE( studentblockdimx == 16 ) with expansion:  52 == 16 ——————————————————————————- TwoDLinkedList::Build, Render1 ——————————————————————————- testpa1.cpp:264 ……………………………………………………………………. testpa1.cpp:286: FAILED:  REQUIRE( vec_width == 16 ) with expansion:  52 == 16 ——————————————————————————- TwoDLinkedList::Build, Render0 ——————————————————————————- testpa1.cpp:293 ……………………………………………………………………. testpa1.cpp:293: FAILED: due to a fatal error condition:  SIGSEGV – Segmentation violation signal =============================================================================== test cases: 12 | 7 passed | 5 failed assertions: 14 | 9 passed | 5 failed stderr: ==107== Memcheck, a memory error detector ==107== Copyright (C) 2002-2017, and GNU GPL’d, by Julian Seward et al. ==107== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info ==107== Command: ./testpa1 ==107== ==107== Conditional jump or move depends on uninitialised value(s) ==107==    at 0x503720: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x503C88: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x507C18: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x44FA10: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x43A502: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4344AB: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4343E8: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x432EB1: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4321ED: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x436F76: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4364C0: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x436273: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107== ==107== Conditional jump or move depends on uninitialised value(s) ==107==    at 0x503720: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x503C88: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x507C18: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x450160: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x43A502: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4344AB: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4343E8: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x432EB1: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4321ED: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x436F76: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4364C0: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x436273: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107== ==107== Conditional jump or move depends on uninitialised value(s) ==107==    at 0x503720: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x503C88: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x507C18: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x450641: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x43A502: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4344AB: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4343E8: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x432EB1: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4321ED: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x436F76: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4364C0: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x436273: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107== ==107== Conditional jump or move depends on uninitialised value(s) ==107==    at 0x503720: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x503C88: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x507C18: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x45105A: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x43A502: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4344AB: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4343E8: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x432EB1: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4321ED: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x436F76: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4364C0: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x436273: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107== ==107== Use of uninitialised value of size 8 ==107==    at 0x503732: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x503C88: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x507C18: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x45105A: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x43A502: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4344AB: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4343E8: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x432EB1: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4321ED: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x436F76: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4364C0: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x436273: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107== ==107== Conditional jump or move depends on uninitialised value(s) ==107==    at 0x50373F: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x503C88: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x507C18: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x45105A: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x43A502: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4344AB: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4343E8: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x432EB1: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4321ED: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x436F76: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4364C0: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x436273: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107== ==107== Use of uninitialised value of size 8 ==107==    at 0x503749: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x503C88: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x507C18: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x45105A: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x43A502: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4344AB: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4343E8: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x432EB1: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4321ED: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x436F76: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4364C0: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x436273: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107== ==107== Conditional jump or move depends on uninitialised value(s) ==107==    at 0x50375D: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x503C88: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x507C18: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x45105A: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x43A502: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4344AB: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4343E8: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x432EB1: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4321ED: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x436F76: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4364C0: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x436273: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107== ==107== Use of uninitialised value of size 8 ==107==    at 0x49AD20: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x49AA14: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x5041E4: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x50376B: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x503C88: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x507C18: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x45105A: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x43A502: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4344AB: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4343E8: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x432EB1: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4321ED: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107== ==107== Invalid read of size 8 ==107==    at 0x49AF00: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x49AED4: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x49AE9E: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x49AE5C: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x49ADA0: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x49AD47: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x49AA14: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x5041E4: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x50376B: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x503C88: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x507C18: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x45105A: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==  Address 0x40ec8348e5894855 is not stack’d, malloc’d or (recently) free’d ==107== ==107== ==107== Process terminating with default action of signal 11 (SIGSEGV) ==107==    at 0x4E4E705: raise (raise.c:46) ==107==    by 0x42A717: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107==    by 0x4E4E88F: ??? (in /lib/x86_64-linux-gnu/libpthread-2.27.so) ==107==    by 0x49AEFF: ??? (in /tmp/tmp9v0vwlln/build/testpa1) ==107== ==107== HEAP SUMMARY: ==107==     in use at exit: 8,046,398 bytes in 330 blocks ==107==   total heap usage: 108,612 allocs, 108,282 frees, 132,591,587 bytes allocated ==107== ==107== LEAK SUMMARY: ==107==    definitely lost: 0 bytes in 0 blocks ==107==    indirectly lost: 0 bytes in 0 blocks ==107==      possibly lost: 0 bytes in 0 blocks ==107==    still reachable: 8,046,398 bytes in 330 blocks ==107==         suppressed: 0 bytes in 0 blocks ==107== Reachable blocks (those to which a pointer was found) are not shown. ==107== To see them, rerun with: –leak-check=full –show-leak-kinds=all ==107== ==107== For counts of detected and suppressed errors, rerun with: -v ==107== Use –track-origins=yes to see where uninitialised values come from ==107== ERROR SUMMARY: 10 errors from 10 contexts (suppressed: 0 from 0)   How to fix the code ?

OR