Skip to content

Commit

Permalink
Minor fixes to README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Dec 11, 2022
1 parent f99e75d commit 5b25f98
Show file tree
Hide file tree
Showing 293 changed files with 24,679 additions and 65 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ CTestTestfile.cmake
_deps
.idea
cmake-build-debug
/doxygen/
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "lab4/src/lib/simpleini"]
path = lab4/src/lib/simpleini
url = https://github.com/brofield/simpleini
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ include_directories(utils)
add_subdirectory(lab1)
add_subdirectory(lab2)
add_subdirectory(lab3)
add_subdirectory(lab4)

add_subdirectory(Google_tests)
18 changes: 18 additions & 0 deletions Google_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,21 @@ target_link_libraries(Google_Tests_run_lab3_overloaded gtest gtest_main libtable
add_executable(Google_Tests_run_lab3_dynamic ../lab3/test/test_overloaded.cpp)
target_link_libraries(Google_Tests_run_lab3_dynamic gtest gtest_main libtable)
target_compile_definitions(Google_Tests_run_lab3_dynamic PRIVATE DYNAMIC)

include_directories(../lab4/src/game)
add_executable(Google_Tests_run_lab4_world ../lab4/src/test/TestWorld.cpp)
target_link_libraries(Google_Tests_run_lab4_world gtest gtest_main dungeon)
add_executable(Google_Tests_run_lab4_calc ../lab4/src/test/TestRangeCalc.cpp)
target_link_libraries(Google_Tests_run_lab4_calc gtest gtest_main dungeon)
add_executable(Google_Tests_run_lab4_talent ../lab4/src/test/TestTalentConfig.cpp)
target_link_libraries(Google_Tests_run_lab4_talent gtest gtest_main dungeon)
add_executable(Google_Tests_run_lab4_player ../lab4/src/test/TestPlayer.cpp)
target_link_libraries(Google_Tests_run_lab4_player gtest gtest_main dungeon)
add_executable(Google_Tests_run_lab4_mobs ../lab4/src/test/TestMobs.cpp)
target_link_libraries(Google_Tests_run_lab4_mobs gtest gtest_main dungeon)
add_executable(Google_Tests_run_lab4_level ../lab4/src/test/TestLevel.cpp)
target_link_libraries(Google_Tests_run_lab4_level gtest gtest_main dungeon)
add_executable(Google_Tests_run_lab4 ../lab4/src/test/TestWorld.cpp ../lab4/src/test/TestRangeCalc.cpp
../lab4/src/test/TestTalentConfig.cpp ../lab4/src/test/TestPlayer.cpp
../lab4/src/test/TestMobs.cpp ../lab4/src/test/TestLevel.cpp)
target_link_libraries(Google_Tests_run_lab4 gtest gtest_main dungeon)
47 changes: 32 additions & 15 deletions lab3/lib/dynamic/OrderedTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,23 @@ namespace Lab3 {
}

void OrderedTable::_extend() {
_vector = (TableElem**) realloc(_vector, (max_size + quota)*sizeof(void*));
_vector = (TableElem**) realloc(_vector, (max_size + quota) * 3/2 * sizeof(void*));
max_size += quota;
}

void OrderedTable::_shrink() {
_vector = (TableElem**) realloc(_vector, (length + 2*quota)*sizeof(void*));
max_size = length + 2*quota;
_vector = (TableElem**) realloc(_vector, (length + quota)*sizeof(void*));
max_size = length + quota;
}

// -------------------------------- Read / Write methods -------------------------------- //

void OrderedTable::add(TableElem* elem) {
if (length >= max_size) _extend();
size_t i = _findIndex(elem->key);
if (i < length && _vector[i]->key == elem->key) {
throw std::invalid_argument("Cannot add: duplicate key");
}
if (length >= max_size) _extend();
memmove(&_vector[i+1], &_vector[i], (length - i)*sizeof(void*));
_vector[i] = elem;
length++;
Expand Down Expand Up @@ -153,11 +156,13 @@ namespace Lab3 {

OrderedTable& OrderedTable::operator=(const OrderedTable &t) {
if (this != &t) {
for (int i = 0; i < length; i++) {
free(_vector[i]->info);
free(_vector[i]);
if (_vector) {
for (int i = 0; i < length; i++) {
free(_vector[i]->info);
free(_vector[i]);
}
free(_vector);
}
free(_vector);
length = t.length;
max_size = length + quota;
_vector = (TableElem**) calloc(max_size, sizeof(void*));
Expand All @@ -171,18 +176,30 @@ namespace Lab3 {
return *this;
}

OrderedTable& OrderedTable::operator=(OrderedTable &&t) noexcept {
if (this != &t) {
if (_vector) {
for (int i = 0; i < length; i++) {
free(_vector[i]->info);
free(_vector[i]);
}
free(_vector);
}
length = t.length;
max_size = t.max_size;
_vector = t._vector;
t.length = 0;
t._vector = nullptr;
}
return *this;
}

OrderedTable::operator bool() const {
return length != 0;
}

const char*& OrderedTable::operator[](int key) const {
if (!length)
throw std::invalid_argument("Trying to access elements from empty table");
size_t i = _findIndex(key);
if (i < length && _vector[i]->key == key) {
return (const char*&) _vector[i]->info;
}
throw std::invalid_argument("Trying to access non-existent element");
return (const char*&) (*(OrderedTable*) this)[key];
}

char*& OrderedTable::operator[](int key) {
Expand Down
1 change: 1 addition & 0 deletions lab3/lib/dynamic/OrderedTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace Lab3 {
void update(int key, const char* info);

OrderedTable& operator = (const OrderedTable& t);
OrderedTable& operator = (OrderedTable&& t) noexcept;
OrderedTable operator + (const OrderedTable& t);
OrderedTable& operator += (const OrderedTable& t);

Expand Down
2 changes: 1 addition & 1 deletion lab3/lib/overloaded/OrderedTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ namespace Lab3 {
return *this;
}

OrderedTable OrderedTable::operator+(const OrderedTable &t) {
OrderedTable OrderedTable::operator+(const OrderedTable &t) const {
OrderedTable tmp(*this);
tmp += t;
return tmp;
Expand Down
2 changes: 1 addition & 1 deletion lab3/lib/overloaded/OrderedTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Lab3 {
void rm(int key);
void update(int key, const char* info);

OrderedTable operator + (const OrderedTable& t);
OrderedTable operator + (const OrderedTable& t) const;
OrderedTable& operator += (const OrderedTable& t);

friend std::ostream& operator << (std::ostream& stream, const OrderedTable& t);
Expand Down
5 changes: 4 additions & 1 deletion lab3/lib/static/OrderedTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
#include "TableElemStatic.h"

namespace Lab3 {
static const size_t max_size = 1024;

class OrderedTable {
public:
static const size_t max_size = 1024;

private:
TableElem _vector[max_size];
void _cleanup();
[[nodiscard]] size_t _findIndex(int key) const;
public:

size_t length;
OrderedTable();
OrderedTable(size_t n, TableElem* vector);
Expand Down
83 changes: 37 additions & 46 deletions lab3/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,67 +27,58 @@ void interactive() {
std::cout << "For command list, enter '?'" << std::endl;

while (true) {
if (Input::input(command) == EOF) break;
if (command[0] == '?') print_commands();
try {
if (Input::input(command) == EOF) break;
if (command[0] == '?') print_commands();

else if (!strcmp("add", command)) {
std::cout << "Key: " << std::ends;
if (Input::input(k) == EOF) break;
std::cout << "Info: " << std::ends;
if (Input::input(info) == EOF) break;
t.add(k, info);
std::cout << "Element added." << std::endl;
}
else if (!strcmp("update", command)) {
std::cout << "Key: " << std::ends;
if (Input::input(k) == EOF) break;
std::cout << "Info: " << std::ends;
if (Input::input(info) == EOF) break;
try {
else if (!strcmp("add", command)) {
std::cout << "Key: " << std::ends;
if (Input::input(k) == EOF) break;
std::cout << "Info: " << std::ends;
if (Input::input(info) == EOF) break;
t.add(k, info);
std::cout << "Element added." << std::endl;
}
else if (!strcmp("update", command)) {
std::cout << "Key: " << std::ends;
if (Input::input(k) == EOF) break;
std::cout << "Info: " << std::ends;
if (Input::input(info) == EOF) break;
t.update(k, info);
std::cout << "Info updated." << std::endl;
}
catch (std::invalid_argument& e) {
std::cout << "Error: " << e.what() << std::endl;
}
}
else if (!strcmp("input", command)) {
std::cout << "Key: " << std::ends;
if (Input::input(k) == EOF) break;
std::cout << "Info: " << std::ends;
try {
else if (!strcmp("input", command)) {
std::cout << "Key: " << std::ends;
if (Input::input(k) == EOF) break;
std::cout << "Info: " << std::ends;
std::cin.ignore();
t.inputElem(std::cin, k);
std::cout << "Element added." << std::endl;
}
catch (std::exception& e) {
std::cout << "Error: " << e.what() << std::endl;
else if (!strcmp("find", command)) {
std::cout << "Key: " << std::ends;
if (Input::input(k) == EOF) break;
s = t.find(k);
if (!s) std::cout << "Element not found." << std::endl;
else std::cout << "[" << k << "]: '" << s << "'" << std::endl;
}
}
else if (!strcmp("find", command)) {
std::cout << "Key: " << std::ends;
if (Input::input(k) == EOF) break;
s = t.find(k);
if (!s) std::cout << "Element not found." << std::endl;
else std::cout << "[" << k << "]: '" << s << "'" << std::endl;
}
else if (!strcmp("rm", command)) {
std::cout << "Key: " << std::ends;
if (Input::input(k) == EOF) break;
try {
else if (!strcmp("rm", command)) {
std::cout << "Key: " << std::ends;
if (Input::input(k) == EOF) break;
t.rm(k);
std::cout << "Element removed." << std::endl;
}
catch (std::invalid_argument& e) {
std::cout << "Error: " << e.what() << std::endl;
else if (!strcmp("print", command)) {
t.print(std::cout);
}

else if (!strcmp("exit", command)) break;
else std::cout << "Unknown command. Try again" << std::endl;

}
else if (!strcmp("print", command)) {
t.print(std::cout);
catch (std::exception& e) {
std::cout << e.what() << std::endl;
}

else if (!strcmp("exit", command)) break;
else std::cout << "Unknown command. Try again" << std::endl;
}
}

Expand Down
25 changes: 25 additions & 0 deletions lab3/test/test_overloaded.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,31 @@ TEST (OrderedTableConstructor, CopyConstructor) {
ASSERT_STREQ("456", t2.find(123));
}

#ifdef DYNAMIC
TEST (OrderedTableConstructor, MoveConstructor) {
Lab3::OrderedTable t1;
t1.add(123, "456");
Lab3::OrderedTable t2(std::move(t1));
ASSERT_EQ(1, t2.length);
ASSERT_STREQ("456", t2.find(123));
}

TEST (OrderedTableOperator, AssignmentCopy) {
Lab3::OrderedTable te;
te.add(123, "456");
Lab3::OrderedTable tf = te;
ASSERT_NE(nullptr, tf.find(123));
ASSERT_NE(nullptr, te.find(123));
}

TEST (OrderedTableOperator, AssignmentMove) {
Lab3::OrderedTable t00;
t00.add(123, "456");
Lab3::OrderedTable t01 = std::move(t00);
ASSERT_NE(nullptr, t01.find(123));
}
#endif

TEST (OrderedTableOperator, Index) {
Lab3::OrderedTable t3;
t3.add(21, "Info string");
Expand Down
2 changes: 1 addition & 1 deletion lab3/test/test_static.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "../../Google_tests/lib/googletest/include/gtest/gtest.h"

#include <TableElemStatic.h>
#include "TableElemStatic.h"
#include "static/OrderedTable.h"


Expand Down
25 changes: 25 additions & 0 deletions lab4/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.17)
set(CMAKE_CXX_STANDARD 17)

project(lab4)

set(G_DIR src/game)
set(SIMPLE_INI_DIR src/lib/simpleini)

include_directories(src/graphics)
include_directories(${G_DIR})
include_directories(${G_DIR}/mobs)
include_directories(${SIMPLE_INI_DIR})

add_library(dungeon ${G_DIR}/Level.cpp ${G_DIR}/RangeCalc.cpp ${G_DIR}/World.cpp ${G_DIR}/TalentConfig.cpp
${G_DIR}/Entity.cpp ${G_DIR}/Player.cpp ${G_DIR}/Mob.cpp
${G_DIR}/mobs/Golem.cpp ${G_DIR}/mobs/Mortal.cpp ${G_DIR}/mobs/Undead.cpp)

add_library(simple-ini ${SIMPLE_INI_DIR}/ConvertUTF.c ${SIMPLE_INI_DIR}/ConvertUTF.h ${SIMPLE_INI_DIR}/SimpleIni.h)

add_executable(lab4 src/main.cpp src/graphics/TileMap.cpp)
include_directories(src/test)

target_link_libraries(lab4 dungeon)
target_link_libraries(lab4 simple-ini)
target_link_libraries(lab4 sfml-graphics sfml-window sfml-system)
Loading

0 comments on commit 5b25f98

Please sign in to comment.