Skip to content
Merged
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
24 changes: 24 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,27 @@ clean:
@for dir in $(SUBDIRS); do \
$(MAKE) -C $$dir clean; \
done

.PHONY: check-format-all check-format

check-format-all:
@for dir in $(SUBDIRS); do \
if [ -d "$$dir" ]; then \
echo "=== clang-format check in $$dir ==="; \
$(MAKE) -f $(MAKEFILE_LIST) check-format DIR=$$dir; \
fi; \
done

check-format:
@cd $(DIR); \
echo "Checking clang-format in $(DIR)"; \
if command -v bash >/dev/null 2>&1; then \
bash -c 'shopt -s nullglob; files=( *.cpp *.hpp *.c *.h ); \
if [ $${#files[@]} -ne 0 ]; then \
clang-format --Werror --dry-run "$${files[@]}"; \
else \
echo "No source files found, skipping"; \
fi'; \
else \
echo "Bash not found; skipping clang-format check"; \
fi
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Examples include (and will expand to):
* Views
* [views-zip-enumerate](./views-zip-enumerate/)
* Atomics and memory ordering
* Folding
* [fold-left-fold-right](./fold-left-fold-right/)
* RAII and ownership patterns
* Performance‑oriented C++ idioms

Expand Down Expand Up @@ -140,6 +142,18 @@ The `clang-format` is used to ensure the code format.
./clang-check.sh *.cpp *.hpp
```

At top level, you can do:

```make
make check-format-all
```

At each example directory, you can do:

```make
make check-format
```

---

## Continuous Integration
Expand Down
29 changes: 29 additions & 0 deletions fold-left-fold-right/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# pull in shared compiler settings
include ../common.mk

# per-example flags
CXXFLAGS += -pthread

TARGET := fold_left_fold_right
SRCS := main.cpp
OBJS := $(SRCS:.cpp=.o)

all: $(TARGET)

$(TARGET): $(OBJS)
$(CXX) $(CXXFLAGS) -o $@ $^

%.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@

run: $(TARGET)
./$(TARGET) $(ARGS)

clean:
rm -f $(OBJS) $(TARGET)

# Delegates to top-level Makefile
check-format:
$(MAKE) -f ../Makefile check-format DIR=$(CURDIR)

.PHONY: all clean run check-format
85 changes: 85 additions & 0 deletions fold-left-fold-right/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <iostream>
#include <vector>
#include <numeric> // accumulate, fold_*
#include <functional> // std::plus
#include <optional>

template <typename Range, typename T, typename Op>
T
fold_left(const Range& r, T init, Op op)
{
for (auto&& v : r)
init = op(init, v);
return init;
}

template <typename Range, typename T, typename Op>
T
fold_right(const Range& r, T init, Op op)
{
for (auto it = r.rbegin(); it != r.rend(); ++it)
init = op(*it, init);
return init;
}

// fold_left_first: uses the first element of the range as the initial value
template <typename Range, typename Op>
std::optional<typename Range::value_type>
fold_left_first(const Range& r, Op op)
{
auto it = r.begin();
if (it == r.end())
return std::nullopt;
auto init = *it++;
for (; it != r.end(); ++it)
init = op(init, *it);
return init;
}

// fold_right_first: uses the first element of the range as the initial value
template <typename Range, typename Op>
std::optional<typename Range::value_type>
fold_right_first(const Range& r, Op op)
{
auto it = r.rbegin();
if (it == r.rend())
return std::nullopt;
auto init = *it++;
for (; it != r.rend(); ++it)
init = op(*it, init);
return init;
}

int
main()
{
std::vector<int> numbers = {1, 2, 3, 4, 5};

// Fold left (classic)
int sum_left = std::accumulate(numbers.begin(), numbers.end(), 0, std::plus<int>{});
std::cout << "Fold left (sum): " << sum_left << '\n';

// Fold right (classic using reverse iterators)
int sum_right = std::accumulate(numbers.rbegin(), numbers.rend(), 0, std::plus<int>{});
std::cout << "Fold right (sum): " << sum_right << '\n';

// Fold left with ranges (C++23)
int sum_ranges = fold_left(numbers, 0, std::plus{});
std::cout << "Fold left with ranges (sum): " << sum_ranges << '\n';

// Fold right with ranges (C++23)
int sum_ranges_right = fold_right(numbers, 0, std::plus{});
std::cout << "Fold right with ranges (sum): " << sum_ranges_right << '\n';

// Fold left using first element as initial value
if (auto sum_first = fold_left_first(numbers, std::plus{})) {
std::cout << "Fold left first element (sum): " << *sum_first << '\n';
}

// Fold right using first element as initial value
if (auto sum_right_first = fold_right_first(numbers, std::plus{})) {
std::cout << "Fold right first element (sum): " << *sum_right_first << '\n';
}

return 0;
}
6 changes: 5 additions & 1 deletion thread-safe-queue/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ run: $(TARGET)
clean:
rm -f $(OBJS) $(TARGET)

.PHONY: all clean run
# Delegates to top-level Makefile
check-format:
$(MAKE) -f ../Makefile check-format DIR=$(CURDIR)

.PHONY: all clean run check-format
6 changes: 5 additions & 1 deletion unique-ptr-basics/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ run: $(TARGET)
clean:
rm -f $(OBJS) $(TARGET)

.PHONY: all clean run
# Delegates to top-level Makefile
check-format:
$(MAKE) -f ../Makefile check-format DIR=$(CURDIR)

.PHONY: all clean run check-format
6 changes: 5 additions & 1 deletion views-zip-enumerate/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ run: $(TARGET)
clean:
rm -f $(OBJS) $(TARGET)

.PHONY: all clean run
# Delegates to top-level Makefile
check-format:
$(MAKE) -f ../Makefile check-format DIR=$(CURDIR)

.PHONY: all clean run check-format