Skip to content

Commit

Permalink
multiple targets
Browse files Browse the repository at this point in the history
  • Loading branch information
landerrosette committed Nov 9, 2024
1 parent 3a221a4 commit 61bb36f
Show file tree
Hide file tree
Showing 54 changed files with 692 additions and 890 deletions.
193 changes: 100 additions & 93 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,96 +3,103 @@ project(algs4)

set(CMAKE_CXX_STANDARD 17)

include_directories(.)

add_executable(algs4
Insertion.h
main.cpp
Merge.h
MergeBU.h
Quick.h
Selection.h
Shell.h
Sorting.h
Quick3way.h
Heap.h
MaxPQ.h
SequentialSearchST.h
ST.h
BinarySearchST.h
BST.h
OrderedST.h
tests/testSort.h
tests/testPQ.h
tests/testPQ.cpp
tests/testST.h
tests/testST.cpp
RedBlackBST.h
SeparateChainingHashST.h
LinearProbingHashST.h
Graph.cpp
Graph.h
Paths.h
Paths.cpp
DepthFirstPaths.cpp
DepthFirstPaths.h
tests/testGraph.h
BreadthFirstPaths.cpp
BreadthFirstPaths.h
Digraph.cpp
Digraph.h
CC.cpp
CC.h
DirectedDFS.cpp
DirectedDFS.h
DirectedCycle.h
DepthFirstOrder.h
SymbolDigraph.cpp
SymbolDigraph.h
Topological.h
KosarajuSCC.cpp
KosarajuSCC.h
Edge.cpp
Edge.h
EdgeWeightedGraph.cpp
EdgeWeightedGraph.h
GraphBase.h
PQ.h
MinPQ.h
IndexMinPQ.h
CCBase.cpp
CCBase.h
MST.h
PrimMST.cpp
PrimMST.h
tests/testGraph.cpp
UF.cpp
UF.h
tests/testUF.h
tests/testUF.cpp
KruskalMST.cpp
KruskalMST.h
MST.cpp
DirectedEdge.h
DirectedEdge.cpp
EdgeWeightedDigraph.cpp
EdgeWeightedDigraph.h
SP.cpp
SP.h
DijkstraSP.cpp
DijkstraSP.h
AcyclicSP.cpp
AcyclicSP.h
BellmanFordSP.cpp
BellmanFordSP.h
LSD.cpp
LSD.h
MSD.cpp
MSD.h
Quick3string.cpp
Quick3string.h
StringSorting.h
StringST.h
TrieST.h
TST.h
)
# 1.5
add_executable(UF main_UF.cpp UF.cpp)

# 2.1, 2.2, 2.3, 2.4, 2.5, 2.7, 5.1, 5.2, 5.3
foreach (SORTER "Selection" "Insertion" "Shell" "Merge" "MergeBU" "Quick" "Quick3way" "Heap" "LSD" "MSD" "Quick3string")
configure_file(main_Sorting.cpp.in main_${SORTER}.cpp @ONLY)

add_executable(${SORTER} main_${SORTER}.cpp)

target_include_directories(${SORTER} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

if (SORTER STREQUAL "LSD" OR SORTER STREQUAL "MSD" OR SORTER STREQUAL "Quick3string")
target_sources(${SORTER} PRIVATE ${SORTER}.cpp)
endif ()
endforeach ()

# 2.6
add_executable(MaxPQ main_MaxPQ.cpp)

# 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 5.4, 5.5
foreach (ST "SequentialSearchST" "BinarySearchST" "BST" "RedBlackBST" "SeparateChainingHashST" "LinearProbingHashST"
"TrieST" "TST")
if (ST STREQUAL "SeparateChainingHashST")
set(ST_INIT_ARGS "4")
elseif (ST STREQUAL "BinarySearchST")
set(ST_INIT_ARGS "20")
endif ()

configure_file(main_ST.cpp.in main_${ST}.cpp @ONLY)

add_executable(${ST} main_${ST}.cpp STTest.cpp)

target_include_directories(${ST} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

if (ST STREQUAL "BinarySearchST" OR ST STREQUAL "BST" OR ST STREQUAL "RedBlackBST")
target_sources(${ST} PRIVATE OrderedSTTest.cpp)

target_compile_definitions(${ST} PRIVATE ORDERED)
elseif (ST STREQUAL "TrieST" OR ST STREQUAL "TST")
target_sources(${ST} PRIVATE StringSTTest.cpp)

target_compile_definitions(${ST} PRIVATE STRING)
endif ()

unset(ST_INIT_ARGS)
endforeach ()

# 4.1, 4.2
foreach (PATHS_SEARCHER "DepthFirstPaths" "BreadthFirstPaths")
configure_file(main_Paths.cpp.in main_${PATHS_SEARCHER}.cpp @ONLY)

add_executable(${PATHS_SEARCHER} main_${PATHS_SEARCHER}.cpp ${PATHS_SEARCHER}.cpp Paths.cpp Graph.cpp)

target_include_directories(${PATHS_SEARCHER} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
endforeach ()

# 4.3, 4.6
foreach (CC "CC" "KosarajuSCC")
configure_file(main_CC.cpp.in main_${CC}.cpp @ONLY)

add_executable(${CC} main_${CC}.cpp ${CC}.cpp CCBase.cpp)

target_include_directories(${CC} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

if (CC STREQUAL "CC")
target_sources(${CC} PRIVATE Graph.cpp)
elseif (CC STREQUAL "KosarajuSCC")
target_sources(${CC} PRIVATE Digraph.cpp)

target_compile_definitions(${CC} PRIVATE DIRECTED)
endif ()
endforeach ()

# 4.4
add_executable(DirectedDFS main_DirectedDFS.cpp DirectedDFS.cpp Digraph.cpp)

# 4.5
add_executable(Topological main_Topological.cpp SymbolDigraph.cpp Digraph.cpp)

# 4.7, 4.8
foreach (MST "PrimMST" "KruskalMST")
configure_file(main_MST.cpp.in main_${MST}.cpp @ONLY)

add_executable(${MST} main_${MST}.cpp ${MST}.cpp MST.cpp EdgeWeightedGraph.cpp Edge.cpp)

target_include_directories(${MST} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

if (MST STREQUAL "KruskalMST")
target_sources(${MST} PRIVATE UF.cpp)
endif ()
endforeach ()

# 4.9, 4.10, 4.11
foreach (SP "DijkstraSP" "AcyclicSP" "BellmanFordSP")
configure_file(main_SP.cpp.in main_${SP}.cpp @ONLY)

add_executable(${SP} main_${SP}.cpp ${SP}.cpp SP.cpp EdgeWeightedDigraph.cpp DirectedEdge.cpp)

target_include_directories(${SP} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
endforeach ()
2 changes: 1 addition & 1 deletion Digraph.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "Digraph.h"

Digraph::Digraph(std::istream &&in) : GraphBase(in) {
Digraph::Digraph(std::istream &in) : GraphBase(in) {
int E;
in >> E;
for (int i = 0; i < E; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion Digraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Digraph : public GraphBase<int> {
public:
Digraph(int V) : GraphBase(V) {}

Digraph(std::istream &&in);
Digraph(std::istream &in);

void addEdge(int v, int w);

Expand Down
2 changes: 1 addition & 1 deletion EdgeWeightedDigraph.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "EdgeWeightedDigraph.h"

EdgeWeightedDigraph::EdgeWeightedDigraph(std::istream &&in) : GraphBase(in) {
EdgeWeightedDigraph::EdgeWeightedDigraph(std::istream &in) : GraphBase(in) {
int E;
in >> E;
for (int i = 0; i < E; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion EdgeWeightedDigraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class EdgeWeightedDigraph : public GraphBase<DirectedEdge> {
public:
EdgeWeightedDigraph(int V) : GraphBase(V) {}

EdgeWeightedDigraph(std::istream &&in);
EdgeWeightedDigraph(std::istream &in);

void addEdge(const DirectedEdge &e);

Expand Down
2 changes: 1 addition & 1 deletion EdgeWeightedGraph.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "EdgeWeightedGraph.h"

EdgeWeightedGraph::EdgeWeightedGraph(std::istream &&in) : GraphBase(in) {
EdgeWeightedGraph::EdgeWeightedGraph(std::istream &in) : GraphBase(in) {
int E;
in >> E;
for (int i = 0; i < E; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion EdgeWeightedGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class EdgeWeightedGraph : public GraphBase<Edge> {
public:
EdgeWeightedGraph(int V) : GraphBase(V) {}

EdgeWeightedGraph(std::istream &&in);
EdgeWeightedGraph(std::istream &in);

void addEdge(const Edge &e);

Expand Down
2 changes: 1 addition & 1 deletion Graph.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "Graph.h"

Graph::Graph(std::istream &&in) : GraphBase(in) {
Graph::Graph(std::istream &in) : GraphBase(in) {
int E;
in >> E;
for (int i = 0; i < E; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion Graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Graph : public GraphBase<int> {
public:
Graph(int V) : GraphBase(V) {}

Graph(std::istream &&in);
Graph(std::istream &in);

void addEdge(int v, int w);
};
Expand Down
66 changes: 66 additions & 0 deletions OrderedSTTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "OrderedSTTest.h"
#include "STTest.h"
#include <iomanip>

namespace STTest {
void init(OrderedST<std::string, int> &st, std::istream &is, std::ostream &os) {
init(static_cast<ST<std::string, int> &>(st), is, os);
os << "min = " << st.min().value_or(INVALID_KEY) << std::endl;
os << "max = " << st.max().value_or(INVALID_KEY) << std::endl;
}

void removeSome(OrderedST<std::string, int> &st, std::ostream &os) {
// remove the smallest keys
for (int i = 0; i < st.size() / 2; ++i) {
st.removeMin();
}
os << "After removing the smallest " << st.size() / 2 << " keys, size = " << st.size() << ":" << std::endl;
os << "--------------------------------" << std::endl;
listAll(st, os);
}

void removeAll(OrderedST<std::string, int> &st, std::ostream &os) {
// remove all the remaining keys
while (!st.isEmpty()) {
st.remove(st.select(st.size() / 2).value_or(INVALID_KEY));
}
std::cout << "After removing the remaining keys, size = " << st.size() << ":" << std::endl;
os << "--------------------------------" << std::endl;
listAll(st, os);
}

void testOrderedST(OrderedST<std::string, int> &st, std::ostream &os) {
// print keys in order using select
os << "Testing select:" << std::endl;
os << "--------------------------------" << std::endl;
for (int i = 0; i < st.size(); ++i) {
os << i << " " << st.select(i).value_or(INVALID_KEY) << std::endl;
}
os << std::endl;

// test rank, floor, ceiling
os << "key rank floor ceil" << std::endl;
os << "-------------------" << std::endl;
for (char i = 'A'; i <= 'Z'; ++i) {
std::string s(1, i);
os << std::setw(2) << s << " "
<< std::setw(4) << st.rank(s) << " "
<< std::setw(4) << st.floor(s).value_or(INVALID_KEY) << " "
<< std::setw(4) << st.ceiling(s).value_or(INVALID_KEY) << std::endl;
}
os << std::endl;

// test range search and range count
std::vector<std::string> from = {"A", "Z", "X", "0", "B", "C"};
std::vector<std::string> to = {"Z", "A", "X", "Z", "G", "L"};
os << "range search" << std::endl;
os << "-------------------" << std::endl;
for (int i = 0; i < from.size(); ++i) {
os << from[i] << "-" << to[i] << " (" << std::setw(2) << st.size(from[i], to[i]) << ") : ";
for (const auto &s: st.keys(from[i], to[i])) {
os << s << " ";
}
os << std::endl;
}
}
}
19 changes: 19 additions & 0 deletions OrderedSTTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef ORDEREDSTTEST_H
#define ORDEREDSTTEST_H


#include <iostream>
#include "OrderedST.h"

namespace STTest {
void init(OrderedST<std::string, int> &st, std::istream &is, std::ostream &os);

void removeSome(OrderedST<std::string, int> &st, std::ostream &os);

void removeAll(OrderedST<std::string, int> &st, std::ostream &os);

void testOrderedST(OrderedST<std::string, int> &st, std::ostream &os);
}


#endif //ORDEREDSTTEST_H
1 change: 0 additions & 1 deletion Quick3string.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@


#include "StringSorting.h"
#include "Sorting.h"
#include <vector>

class Quick3string : public StringSorting {
Expand Down
Loading

0 comments on commit 61bb36f

Please sign in to comment.