Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
34 changes: 34 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Build directories
HW1/build/
HW2/build/
HW3/build/
homework_4/build/

# Compiled binaries
HW1/bin/
HW2/bin/
HW3/bin/
homework_4/bin/

# CMake generated files
CMakeCache.txt
CMakeFiles/
cmake_install.cmake
Makefile
CTestTestfile.cmake
Testing/

# Static / shared libraries
*.a
*.so
*.so.*

# Editor / IDE files
.vscode/
.idea/
*.swp
*.swo
*~

# macOS
.DS_Store
20 changes: 20 additions & 0 deletions HW1/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
cmake_minimum_required(VERSION 3.1)
project(homework1)

set(CMAKE_CXX_STANDARD 11)

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()

set(CMAKE_CXX_FLAGS "-Wall -Wextra")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O2")

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

include_directories(${PROJECT_SOURCE_DIR}/src)

add_subdirectory(src)
enable_testing()
add_subdirectory(tests)
3 changes: 3 additions & 0 deletions HW1/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_library(string_tools string_tools.cpp)
add_executable(hw1_main main.cpp)
target_link_libraries(hw1_main string_tools)
24 changes: 24 additions & 0 deletions HW1/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "string_tools.h"

#include <iostream>

int main() {
// Demonstrate string tokenization
const std::string csv_line = "hello,world,modern,cpp,course";
std::cout << "Tokenizing: \"" << csv_line << "\" by ','" << std::endl;
auto tokens = hw1::Tokenize(csv_line, ',');
for (const auto& token : tokens) {
std::cout << " Token: " << token << std::endl;
}

// Demonstrate word frequency counting
const std::string text =
"the quick brown fox jumps over the lazy dog the fox";
std::cout << "\nWord frequencies in: \"" << text << "\"" << std::endl;
auto frequencies = hw1::CountWordFrequency(text);
for (const auto& pair : frequencies) {
std::cout << " \"" << pair.first << "\": " << pair.second << std::endl;
}

return 0;
}
34 changes: 34 additions & 0 deletions HW1/src/string_tools.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "string_tools.h"

#include <algorithm>
#include <map>
#include <sstream>

namespace hw1 {

std::vector<std::string> Tokenize(const std::string& str, char delimiter) {
std::vector<std::string> tokens;
std::istringstream stream(str);
std::string token;
while (std::getline(stream, token, delimiter)) {
if (!token.empty()) {
tokens.push_back(token);
}
}
return tokens;
}

std::vector<std::pair<std::string, int>> CountWordFrequency(
const std::string& str) {
std::map<std::string, int> freq_map;
std::istringstream stream(str);
std::string word;
while (stream >> word) {
++freq_map[word];
}
std::vector<std::pair<std::string, int>> result(freq_map.begin(),
freq_map.end());
return result;
}

} // namespace hw1
21 changes: 21 additions & 0 deletions HW1/src/string_tools.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <string>
#include <vector>

namespace hw1 {

/// Split a string into tokens separated by a delimiter character.
/// @param str The input string to split.
/// @param delimiter The character used as delimiter.
/// @return A vector of substrings (tokens).
std::vector<std::string> Tokenize(const std::string& str, char delimiter);

/// Count the frequency of each word in a string.
/// Words are separated by whitespace.
/// @param str The input string.
/// @return A vector of (word, count) pairs, sorted alphabetically by word.
std::vector<std::pair<std::string, int>> CountWordFrequency(
const std::string& str);

} // namespace hw1
6 changes: 6 additions & 0 deletions HW1/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
include(CTest)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

add_executable(test_string_tools test_string_tools.cpp)
target_link_libraries(test_string_tools string_tools)
add_test(NAME TestStringTools COMMAND test_string_tools)
52 changes: 52 additions & 0 deletions HW1/tests/test_string_tools.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "string_tools.h"

#include <cassert>
#include <iostream>
#include <string>
#include <vector>

void TestTokenize() {
auto tokens = hw1::Tokenize("a,b,c", ',');
assert(tokens.size() == 3);
assert(tokens[0] == "a");
assert(tokens[1] == "b");
assert(tokens[2] == "c");

// Test with empty parts (consecutive delimiters skipped)
auto tokens2 = hw1::Tokenize("hello world", ' ');
assert(tokens2.size() == 2);
assert(tokens2[0] == "hello");
assert(tokens2[1] == "world");

// Test empty string
auto tokens3 = hw1::Tokenize("", ',');
assert(tokens3.empty());

std::cout << "TestTokenize: PASSED" << std::endl;
}

void TestCountWordFrequency() {
auto freq = hw1::CountWordFrequency("the cat sat on the mat the cat");
// Expect: cat=2, mat=1, on=1, sat=1, the=3
assert(freq.size() == 5);
for (const auto& wc : freq) {
if (wc.first == "the") assert(wc.second == 3);
if (wc.first == "cat") assert(wc.second == 2);
if (wc.first == "sat") assert(wc.second == 1);
if (wc.first == "on") assert(wc.second == 1);
if (wc.first == "mat") assert(wc.second == 1);
}

// Test empty string
auto freq2 = hw1::CountWordFrequency("");
assert(freq2.empty());

std::cout << "TestCountWordFrequency: PASSED" << std::endl;
}

int main() {
TestTokenize();
TestCountWordFrequency();
std::cout << "All HW1 tests passed!" << std::endl;
return 0;
}
20 changes: 20 additions & 0 deletions HW2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
cmake_minimum_required(VERSION 3.1)
project(homework2)

set(CMAKE_CXX_STANDARD 11)

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()

set(CMAKE_CXX_FLAGS "-Wall -Wextra -fPIC")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O2")

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

include_directories(${PROJECT_SOURCE_DIR}/src)

add_subdirectory(src)
enable_testing()
add_subdirectory(tests)
11 changes: 11 additions & 0 deletions HW2/data/sample.pgm
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
P2
8 8
255
0 18 36 54 72 91 109 127
18 36 54 72 91 109 127 145
36 54 72 91 109 127 145 163
54 72 91 109 127 145 163 182
72 91 109 127 145 163 182 200
91 109 127 145 163 182 200 218
109 127 145 163 182 200 218 236
127 145 163 182 200 218 236 255
5 changes: 5 additions & 0 deletions HW2/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_subdirectory(igg_image)
add_subdirectory(application1)
add_subdirectory(application2)
add_subdirectory(application3)
add_subdirectory(application4)
2 changes: 2 additions & 0 deletions HW2/src/application1/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_executable(application1 main.cpp)
target_link_libraries(application1 igg_image)
21 changes: 21 additions & 0 deletions HW2/src/application1/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "igg_image/image.h"

#include <iostream>

/// Application 1: demonstrates the getter and setter via at().
int main() {
std::cout << "=== Application 1: getter/setter test ===" << std::endl;
igg::Image img(50, 50);

// Reading through a const reference exercises the const (getter) overload.
const igg::Image& const_img = img;
std::cout << "Before set: img.at(49,49) = " << const_img.at(49, 49)
<< std::endl;

// Assignment through the non-const overload exercises the setter.
img.at(49, 49) = 50;

std::cout << "After set: img.at(49,49) = " << const_img.at(49, 49)
<< std::endl;
return 0;
}
2 changes: 2 additions & 0 deletions HW2/src/application2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_executable(application2 main.cpp)
target_link_libraries(application2 igg_image)
23 changes: 23 additions & 0 deletions HW2/src/application2/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "igg_image/image.h"

#include <iostream>

/// Application 2: reads a PGM file, draws a diagonal, and writes it back.
int main() {
std::cout << "=== Application 2: read/write PGM ===" << std::endl;
igg::Image img;
if (img.FillFromPgm("../data/lena.ascii.pgm")) {
std::cout << "Image loaded: " << img.rows() << " rows x " << img.cols()
<< " cols" << std::endl;
// Draw a white diagonal
int diag = std::min(img.rows(), img.cols());
for (int i = 0; i < diag; ++i) {
img.at(i, i) = 255;
}
img.WriteToPgm("../data/lena_diagonal.ascii.pgm");
std::cout << "Written to ../data/lena_diagonal.ascii.pgm" << std::endl;
} else {
std::cerr << "Could not open ../data/lena.ascii.pgm" << std::endl;
}
return 0;
}
2 changes: 2 additions & 0 deletions HW2/src/application3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_executable(application3 main.cpp)
target_link_libraries(application3 igg_image)
23 changes: 23 additions & 0 deletions HW2/src/application3/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "igg_image/image.h"

#include <iostream>
#include <vector>

/// Application 3: loads a PGM image and prints its histogram.
int main() {
std::cout << "=== Application 3: histogram ===" << std::endl;
igg::Image img;
if (img.FillFromPgm("../data/lena.ascii.pgm")) {
std::cout << "Image loaded: " << img.rows() << " rows x " << img.cols()
<< " cols" << std::endl;
const int kBins = 10;
std::vector<float> hist = img.ComputeHistogram(kBins);
std::cout << "Histogram (" << kBins << " bins):" << std::endl;
for (int i = 0; i < kBins; ++i) {
std::cout << " bin[" << i << "] = " << hist[i] << std::endl;
}
} else {
std::cerr << "Could not open ../data/lena.ascii.pgm" << std::endl;
}
return 0;
}
2 changes: 2 additions & 0 deletions HW2/src/application4/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_executable(application4 main.cpp)
target_link_libraries(application4 igg_image)
25 changes: 25 additions & 0 deletions HW2/src/application4/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "igg_image/image.h"

#include <iostream>

/// Application 4: demonstrates downscaling and upscaling.
int main() {
std::cout << "=== Application 4: scaling ===" << std::endl;
igg::Image img;
if (img.FillFromPgm("../data/lena.ascii.pgm")) {
std::cout << "Original size: " << img.rows() << " x " << img.cols()
<< std::endl;
img.DownScale(2);
std::cout << "After DownScale(2): " << img.rows() << " x " << img.cols()
<< std::endl;
img.WriteToPgm("../data/lena_downscaled.ascii.pgm");

img.UpScale(2);
std::cout << "After UpScale(2): " << img.rows() << " x " << img.cols()
<< std::endl;
img.WriteToPgm("../data/lena_upscaled.ascii.pgm");
} else {
std::cerr << "Could not open ../data/lena.ascii.pgm" << std::endl;
}
return 0;
}
1 change: 1 addition & 0 deletions HW2/src/igg_image/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_library(igg_image image.cpp)
Loading