summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2020-09-21 21:37:33 +0200
committerGustav Sörnäs <gustav@sornas.net>2020-09-21 21:38:27 +0200
commit245a34f1fad0e4fe2fb6401dc1d5352e4914b48a (patch)
tree566511b6f032c25cc60dfb580147d3a929021bb9
parent56dc03a2791f6d2bec86285fed1a12907d7ff661 (diff)
downloadtddd86-245a34f1fad0e4fe2fb6401dc1d5352e4914b48a.tar.gz
Correct code is nice
-rw-r--r--labb3/tiles/TileList.cpp44
-rw-r--r--labb3/tiles/TileList.h6
2 files changed, 20 insertions, 30 deletions
diff --git a/labb3/tiles/TileList.cpp b/labb3/tiles/TileList.cpp
index 88bdc21..c403de3 100644
--- a/labb3/tiles/TileList.cpp
+++ b/labb3/tiles/TileList.cpp
@@ -7,19 +7,20 @@ TileList::TileList()
}
TileList::~TileList()
-{delete tiles;
+{
+ delete tiles;
}
void TileList::addTile(Tile tile)
{
- // O(1) amortized
+ // O(1) (amortized)
if (amount_tiles == cur_size) {
// reached size limit, so allocate new array and move the old tiles
- Tile *expanded_tiles = new Tile[cur_size + INCREASE_SIZE];
- for (int i = 0; i < cur_size; i++) {
+ cur_size *= 2;
+ Tile *expanded_tiles = new Tile[cur_size];
+ for (int i = 0; i < amount_tiles; i++) {
expanded_tiles[i] = tiles[i];
}
- cur_size += INCREASE_SIZE;
delete tiles;
tiles = expanded_tiles;
}
@@ -37,18 +38,7 @@ void TileList::drawAll(QGraphicsScene *scene) const
int TileList::indexOfTopTile(int x, int y) const
{
// O(n)
- for (int i = 0; i < amount_tiles; i++) {
- if ((tiles + i)->contains(x, y)) {
- return i;
- }
- }
- return -1;
-}
-
-int TileList::indexOfBottomTile(int x, int y) const
-{
- // O(n)
- for (int i = amount_tiles - 1; i >= 0; i--) {
+ for (int i = amount_tiles - 1; i >= 0; --i) {
if ((tiles + i)->contains(x, y)) {
return i;
}
@@ -58,7 +48,7 @@ int TileList::indexOfBottomTile(int x, int y) const
void TileList::shiftRight(int start, int end)
{
- // e.g. length=5, start=1, end=4:
+ // e.g. amount_tiles=6, start=1, end=4:
// 0 1 2 3 4 5
// 0 1 1 2 3 5
// O(n)
@@ -69,11 +59,11 @@ void TileList::shiftRight(int start, int end)
void TileList::shiftLeft(int start, int end)
{
- // e.g. length=5, start=4, end=1:
+ // e.g. amount_tiles=6, start=4, end=1:
// 0 1 2 3 4 5
// 0 2 3 4 4 5
// O(n)
- for (int i = start; i < end; i++) {
+ for (int i = end; i < start; i++) {
tiles[i] = tiles[i + 1];
}
}
@@ -91,7 +81,7 @@ void TileList::raise(int x, int y)
return;
}
Tile tmpTile = tiles[index];
- shiftLeft(index, amount_tiles - 1);
+ shiftLeft(amount_tiles - 1, index);
tiles[amount_tiles - 1] = tmpTile;
}
@@ -102,7 +92,7 @@ void TileList::lower(int x, int y)
// 0 0 1 2 4 5 3
// 3 0 1 2 4 5 -
// O(n)
- int index = indexOfBottomTile(x, y);
+ int index = indexOfTopTile(x, y);
if (index == -1 || index == 0) {
// no tile found, or tile is already at bottom
return;
@@ -114,10 +104,14 @@ void TileList::lower(int x, int y)
void TileList::remove(int x, int y)
{
+ // V
+ // 0 1 2 3 4 5
+ // 0 1 3 4 5 5
+ // 0 1 3 4 5 x
// O(n)
int index;
- if ((index = indexOfBottomTile(x, y)) != -1) {
- shiftLeft(index, amount_tiles - 1);
+ if ((index = indexOfTopTile(x, y)) != -1) {
+ shiftLeft(amount_tiles - 1, index);
amount_tiles--;
}
}
@@ -127,7 +121,7 @@ void TileList::removeAll(int x, int y)
// O(n^2)
int index;
while ((index = indexOfTopTile(x, y)) != -1) {
- shiftLeft(index, amount_tiles - 1);
+ shiftLeft(amount_tiles - 1, index);
amount_tiles--;
}
}
diff --git a/labb3/tiles/TileList.h b/labb3/tiles/TileList.h
index 865823f..10cfdfb 100644
--- a/labb3/tiles/TileList.h
+++ b/labb3/tiles/TileList.h
@@ -16,7 +16,7 @@ public:
~TileList(); // deallocate the tile list
void addTile(Tile tile); // add a tile to the tile list, possibly reallocating
void drawAll(QGraphicsScene *scene) const; // draw all tiles to `scene`
- int indexOfTopTile(int x, int y) const;
+ int indexOfTopTile(int x, int y) const; // return index of top tile at (x, y)
void lower(int x, int y); // move the top tile at (x, y) to the bottom
void raise(int x, int y); // move the bottom tile at (x, y) to the top
void remove(int x, int y); // remove the top tile at (x, y)
@@ -24,14 +24,10 @@ public:
private:
static const int INITIAL_SIZE = 10; // the initial size
- static const int INCREASE_SIZE = 10; // how much the size is increased when
- // it's needed
int cur_size = 0; // current size of array
int amount_tiles; // number of active tiles in array
Tile *tiles; // the array
- int indexOfBottomTile(int x, int y) const;
-
/*
* shiftRight and shiftLeft move a group of tiles either right or left
* in the internal array.