Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
0348f5f
Homework 10 - task 1 (WIP)
ilya-krivtsov Dec 1, 2024
c0844f0
Fixed function arguments (hw10 task 1)
ilya-krivtsov Dec 1, 2024
f4afe02
Moved queue to hashtable (hw10 task 1)
ilya-krivtsov Dec 1, 2024
7f7c021
Renamed NodeHashtableData -> NodeData (hw10 task 1)
ilya-krivtsov Dec 1, 2024
440e23b
Made queue parameters more explicit (hw10 task 1)
ilya-krivtsov Dec 1, 2024
636c8db
Moved iterator to the top of the implementation (hw10 task 1)
ilya-krivtsov Dec 2, 2024
4e61283
Use iterator when expanding hashtable (hw10 task 1)
ilya-krivtsov Dec 2, 2024
dba48cf
Use list instead of hashtable for storing neighbors (hw10 task 1)
ilya-krivtsov Dec 2, 2024
7de2ad6
Fixed couple logical issues (hw10 task 1)
ilya-krivtsov Dec 2, 2024
f44bb55
Updated hash functions (hw10 task 1)
ilya-krivtsov Dec 2, 2024
f5120c6
Added disposeNode and disposeCountry (hw10 task 1)
ilya-krivtsov Dec 2, 2024
52d4bd1
Fixed tryExtendArrayByOne() (hw10 task 1)
ilya-krivtsov Dec 2, 2024
7d78d6b
Fixed hashtable iterator (hw10 task 1)
ilya-krivtsov Dec 2, 2024
d919bc1
Removed replacedValue parameter from addDistanceToHashtable() (hw10 t…
ilya-krivtsov Dec 2, 2024
896ec30
Create countries before capturing instead of after (hw10 task 1)
ilya-krivtsov Dec 2, 2024
2a4178e
Moved GraphNode declaration to the top (hw10 task 1)
ilya-krivtsov Dec 2, 2024
48c1bfe
Update count in buckets (hw10 task 1)
ilya-krivtsov Dec 2, 2024
8951ab9
Fixed memory disposing (hw10 task 1)
ilya-krivtsov Dec 2, 2024
0a85902
Removed newDistance variable (hw10 task 1)
ilya-krivtsov Dec 2, 2024
97cf9d4
Enqueue neighbor after distance update (hw10 task 1)
ilya-krivtsov Dec 2, 2024
530ac22
Add node to captured cities after checking neighbors (hw10 task 1)
ilya-krivtsov Dec 2, 2024
318b03d
Fixed getAllCities() parameters (hw10 task 1)
ilya-krivtsov Dec 2, 2024
7b6d039
Removed unnecessary disposing (hw10 task 1)
ilya-krivtsov Dec 2, 2024
cdd3bcb
Added main functionality (hw10 task 1)
ilya-krivtsov Dec 2, 2024
10528b0
Moved reading graph from file and country creation to separate file (…
ilya-krivtsov Dec 4, 2024
56e0515
Added tests (hw10 task 1)
ilya-krivtsov Dec 4, 2024
f8028e1
Removed unused getAllNeighbors() (hw10 task 1)
ilya-krivtsov Dec 4, 2024
57cd7d7
Fixed typo (hw10 task 1)
ilya-krivtsov Dec 4, 2024
3d83300
Added comments (hw10 task 1)
ilya-krivtsov Dec 4, 2024
281edfa
Added stdbool.h includes (hw10 task 1)
ilya-krivtsov Dec 10, 2024
6fd9446
Added stdlib.h include (hw10 task 1)
ilya-krivtsov Dec 10, 2024
2e0fa41
Check hashtable resize result (hw10 task 1)
ilya-krivtsov Dec 30, 2024
8ece014
Simplified return in createQueue() (hw10 task 1)
ilya-krivtsov Dec 30, 2024
1f8324b
Merge branch 'main' into hw-10-task-1
ilya-krivtsov Dec 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ add_subdirectory(homework_6)
add_subdirectory(homework_7)
add_subdirectory(homework_8)
add_subdirectory(homework_9)
add_subdirectory(homework_10)
5 changes: 5 additions & 0 deletions homework_10/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
project(homework_10)

set(homeworkName "${PROJECT_NAME}")

add_subdirectory(task_1)
1 change: 1 addition & 0 deletions homework_10/task_1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
graph.txt
9 changes: 9 additions & 0 deletions homework_10/task_1/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
project("${homeworkName}_task_1")

add_library(graph graph.c fileGraphReader.c)

add_executable(${PROJECT_NAME} main.c)
target_link_libraries(${PROJECT_NAME} graph)

add_executable(${PROJECT_NAME}_test test.c)
target_link_libraries(${PROJECT_NAME}_test graph)
165 changes: 165 additions & 0 deletions homework_10/task_1/fileGraphReader.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#include "fileGraphReader.h"

#include <stdio.h>
#include <stdlib.h>

#include "graph.h"

ReadResult readGraphFromFileAndCreateCountries(FILE *file, GraphNode ***nodes, int *nodeCount, Country ***countries, int *countryCount, FILE *warningAndErrorOutput) {
int storedNodeCount = 0;
if (fscanf(file, "%d", &storedNodeCount) != 1) {
if (warningAndErrorOutput != NULL) {
fprintf(warningAndErrorOutput, "couldn't read node count\n");
}
return READ_FILE_ERROR;
}

int connectionsCount = 0;
if (fscanf(file, "%d", &connectionsCount) != 1) {
if (warningAndErrorOutput != NULL) {
fprintf(warningAndErrorOutput, "couldn't read connections count\n");
}
return READ_FILE_ERROR;
}

GraphNode **storedNodes = calloc(storedNodeCount, sizeof(GraphNode *));
if (storedNodes == NULL) {
return READ_ALLOCATION_ERROR;
}

ReadResult result = READ_OK;
for (int i = 0; i < storedNodeCount; ++i) {
if (!createNode(&storedNodes[i])) {
result = READ_ALLOCATION_ERROR;
break;
}
}

if (result != READ_OK) {
for (int i = 0; i < storedNodeCount; ++i) {
disposeNode(storedNodes[i]);
}
free(storedNodes);

return result;
}

for (int i = 0; i < connectionsCount; ++i) {
int nodeAIndex = -1;
int nodeBIndex = -1;
int distance = -1;
if (fscanf(file, "%d %d %d", &nodeAIndex, &nodeBIndex, &distance) != 3) {
if (warningAndErrorOutput != NULL) {
fprintf(warningAndErrorOutput, "couldn't read connection\n");
}
result = READ_FILE_ERROR;
break;
}

if (nodeAIndex < 0 || nodeAIndex >= storedNodeCount) {
if (warningAndErrorOutput != NULL) {
fprintf(warningAndErrorOutput, "warning: node #%d does not exist, connection skipped\n", nodeAIndex);
}
continue;
}

if (nodeBIndex < 0 || nodeBIndex >= storedNodeCount) {
if (warningAndErrorOutput != NULL) {
fprintf(warningAndErrorOutput, "warning: node #%d does not exist, connection skipped\n", nodeBIndex);
}
continue;
}

switch (connect(storedNodes[nodeAIndex], storedNodes[nodeBIndex], distance))
{
case CONNECTION_ALLOCATION_ERROR:
result = READ_ALLOCATION_ERROR;
break;

case CONNECTION_ALREADY_EXISTS:
if (warningAndErrorOutput != NULL) {
fprintf(warningAndErrorOutput, "warning: connection between node #%d and #%d already exists, skipped\n", nodeAIndex, nodeBIndex);
}
break;

case CONNECTION_OK:
break;
}

if (result != READ_OK) {
break;
}
}

if (result != READ_OK) {
for (int i = 0; i < storedNodeCount; ++i) {
disposeNode(storedNodes[i]);
}
free(storedNodes);

return result;
}

int capitalsCount = 0;
if (fscanf(file, "%d", &capitalsCount) != 1) {
if (warningAndErrorOutput != NULL) {
fprintf(warningAndErrorOutput, "couldn't read capitals count\n");
}

for (int i = 0; i < storedNodeCount; ++i) {
disposeNode(storedNodes[i]);
}
free(storedNodes);

return READ_FILE_ERROR;
}

GraphNode **capitals = calloc(capitalsCount, sizeof(GraphNode *));
if (capitals == NULL) {
for (int i = 0; i < storedNodeCount; ++i) {
disposeNode(storedNodes[i]);
}
free((storedNodes));

return READ_ALLOCATION_ERROR;
}

for (int i = 0; i < capitalsCount; ++i) {
int capitalIndex = -1;
if (fscanf(file, "%d", &capitalIndex) != 1) {
if (warningAndErrorOutput != NULL) {
fprintf(warningAndErrorOutput, "couldn't read capital index\n");
}
result = READ_FILE_ERROR;
break;
}
capitals[i] = storedNodes[capitalIndex];
}

if (result != READ_OK) {
for (int i = 0; i < storedNodeCount; ++i) {
disposeNode(storedNodes[i]);
}
free(storedNodes);
free(capitals);

return result;
}

if (!createCountries(capitals, countries, capitalsCount)) {
for (int i = 0; i < storedNodeCount; ++i) {
disposeNode(storedNodes[i]);
}
free(storedNodes);
free(capitals);

return READ_ALLOCATION_ERROR;
}

*nodes = storedNodes;
*nodeCount = storedNodeCount;
*countryCount = capitalsCount;

free(capitals);
return READ_OK;
}
25 changes: 25 additions & 0 deletions homework_10/task_1/fileGraphReader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <stdio.h>

#include "graph.h"

typedef enum {
READ_OK,
READ_ALLOCATION_ERROR,
READ_FILE_ERROR,
} ReadResult;

/// @brief Reads graph from file and creates countries
/// @param file File to read graph from
/// @param nodes Pointer to store array of nodes to
/// @param nodeCount Pointer to store nodes count
/// @param countries Pointer to store array of countries to
/// @param countryCount Pointer to store countries count
/// @param warningAndErrorOutput File to write warning or error messages, can be `NULL`
/// @return `READ_OK` if read file and created countries successfully
///
/// `READ_FILE_ERROR` if file has incorrect format
///
/// `READ_ALLOCATION_ERROR` if allocation failed
ReadResult readGraphFromFileAndCreateCountries(FILE *file, GraphNode ***nodes, int *nodeCount, Country ***countries, int *countryCount, FILE *warningAndErrorOutput);
Loading