Skip to content

Commit d7e7bb1

Browse files
committed
Use new all-post target to factor makefiles.
1 parent 903fff6 commit d7e7bb1

36 files changed

Lines changed: 494 additions & 405 deletions

‎.vimrc‎

Lines changed: 0 additions & 30 deletions
This file was deleted.

‎.vimrc‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vimrc_many

‎.vimrc_many‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
" Used on directories that compile files individually (e.g. c.c -> c and cpp.cpp -> cpp)
2+
" <F6>: compile and run the current file
3+
4+
function! RedirStdoutNewTabSingle(cmd)
5+
tabnext
6+
if expand('%:p') != ""
7+
tabprevious
8+
execute "tabnew"
9+
setlocal buftype=nofile
10+
setlocal bufhidden=wipe
11+
setlocal noswapfile
12+
endif
13+
%delete
14+
execute "silent read !" . a:cmd
15+
set nomodified
16+
endfunction
17+
18+
function! MapAllBuff(keys, rhs)
19+
exe 'noremap <buffer>' a:keys a:rhs
20+
exe 'noremap! <buffer>' a:keys '<ESC>'.a:rhs
21+
endfunction
22+
23+
call MapAllBuff( '<F5>' , ':w<CR>:make<CR>' )
24+
if expand('%:e') =~ '\(c\|cpp\|f\)'
25+
call MapAllBuff('<F6>', ':call RedirStdoutNewTabSingle("make run=''\"' . expand('%:r') . '\"''")<CR>')
26+
call MapAllBuff('<F9>', ':call RedirStdoutNewTabSingle("make profile=''\"' . expand('%:r') . '\"''")<CR>')
27+
else
28+
call MapAllBuff('<F6>', ':call RedirStdoutNewTabSingle("make run")<CR>')
29+
call MapAllBuff('<F9>', ':w<CR>:call RedirStdoutNewTabSingle("make profile")<CR>')
30+
endif

‎.vimrc_one‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
boilerplate/.vimrc

‎Makefile_many‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
-include Makefile_params
44

5-
# c/cpp
5+
# C/CPP
66
# Don't use the standard names like `CC` and `CXX` to avoid `?=` getting overridden.
77
# by the Makefile default values.
88
MYCC ?= gcc
@@ -21,7 +21,8 @@ FFLAGS ?= -std=f2003 -Wall -pedantic-errors -march=native $(FFLAGS_EXTRA)
2121
# Paths
2222
IN_DIR ?= ./
2323
IN_EXTS ?= .c .cpp .f
24-
OUT_DIR ?= _out/
24+
# Unlike in Makefile_one, there is no TMP_DIR because there are no .o files generated.
25+
OUT_DIR ?= _build/
2526
OUT_EXT ?= #.elf
2627

2728
# Basename without extension of file to run on run target.
@@ -50,6 +51,8 @@ OUTS := $(addprefix $(OUT_DIR), $(OUTS_NODIR))
5051
.PHONY: all asm set_asm_flags clean debug set_debug_flags help mkdir objdump set_objdump_flags profile set_profile_flags test $(PHONY)
5152

5253
all: mkdir $(OUTS)
54+
@#TODO ignore errors if not present
55+
@-$(MAKE) all-post
5356
ifneq ($(strip $(run)),)
5457
cd $(OUT_DIR) && ./"$(run)"
5558
endif

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ C and C++ information, cheatsheets and mini-projects.
22

33
[![Build Status](https://travis-ci.org/cirosantilli/cpp.svg?branch=master)](https://travis-ci.org/cirosantilli/cpp)
44

5-
Relies on <https://github.com/cirosantilli/cpp-boilerplate> to factor code out. See [its documentation](https://github.com/cirosantilli/cpp-boilerplate/blob/master/README.md) for useful information on how to use this project.
5+
Relies on <https://github.com/cirosantilli/cpp-boilerplate> to factor code out. See [its documentation](https://github.com/cirosantilli/cpp-boilerplate/blob/master/README.md) for information on how to use this project.
66

77
# Most useful files
88

‎boilerplate‎

‎boost/graph.cpp‎

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//=======================================================================
2-
// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
3-
//
4-
// Distributed under the Boost Software License, Version 1.0. (See
5-
// accompanying file LICENSE_1_0.txt or copy at
6-
// http://www.boost.org/LICENSE_1_0.txt)
7-
//=======================================================================
81
#include <iostream>
92
#include <fstream>
103

@@ -16,62 +9,67 @@
169

1710
using namespace boost;
1811

19-
int
20-
main(int, char *[]) {
21-
typedef adjacency_list < listS, vecS, directedS,
22-
no_property, property < edge_weight_t, int > > graph_t;
23-
typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor;
24-
typedef graph_traits < graph_t >::edge_descriptor edge_descriptor;
25-
typedef std::pair<int, int> Edge;
12+
int main(int, char *[]) {
13+
// #dijikstra
14+
{
15+
typedef adjacency_list <listS, vecS, directedS,
16+
no_property, property <edge_weight_t, int>> graph_t;
17+
typedef graph_traits <graph_t>::vertex_descriptor vertex_descriptor;
18+
typedef graph_traits <graph_t>::edge_descriptor edge_descriptor;
19+
typedef std::pair<int, int> Edge;
2620

27-
const int num_nodes = 5;
28-
enum nodes { A, B, C, D, E };
29-
char name[] = "ABCDE";
30-
Edge edge_array[] = { Edge(A, C), Edge(B, B), Edge(B, D), Edge(B, E),
31-
Edge(C, B), Edge(C, D), Edge(D, E), Edge(E, A), Edge(E, B)
32-
};
33-
int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 };
34-
int num_arcs = sizeof(edge_array) / sizeof(Edge);
35-
graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes);
36-
property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g);
37-
std::vector<vertex_descriptor> p(num_vertices(g));
38-
std::vector<int> d(num_vertices(g));
39-
vertex_descriptor s = vertex(A, g);
21+
// Model
22+
const int num_nodes = 5;
23+
enum nodes { A, B, C, D, E };
24+
char name[] = "ABCDE";
25+
Edge edge_array[] = {
26+
Edge(A, C), Edge(B, B), Edge(B, D), Edge(B, E),
27+
Edge(C, B), Edge(C, D), Edge(D, E), Edge(E, A), Edge(E, B)
28+
};
29+
int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 };
4030

41-
dijkstra_shortest_paths(g, s,
42-
predecessor_map(boost::make_iterator_property_map(p.begin(), get(boost::vertex_index, g))).
43-
distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, g))));
31+
// Solution
32+
int num_arcs = sizeof(edge_array) / sizeof(Edge);
33+
graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes);
34+
std::vector<vertex_descriptor> p(num_vertices(g));
35+
std::vector<int> d(num_vertices(g));
36+
vertex_descriptor s = vertex(A, g);
37+
dijkstra_shortest_paths(g, s,
38+
predecessor_map(boost::make_iterator_property_map(p.begin(), get(boost::vertex_index, g))).
39+
distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, g))));
4440

45-
std::cout << "distances and parents:" << std::endl;
46-
graph_traits < graph_t >::vertex_iterator vi, vend;
47-
for (boost::tie(vi, vend) = vertices(g); vi != vend; ++vi) {
48-
std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", ";
49-
std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]] << std::
50-
endl;
51-
}
52-
std::cout << std::endl;
41+
// Output Solution
42+
std::cout <<"distances and parents:" <<std::endl;
43+
graph_traits <graph_t>::vertex_iterator vi, vend;
44+
for (boost::tie(vi, vend) = vertices(g); vi != vend; ++vi) {
45+
std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", ";
46+
std::cout << "wparent(" << name[*vi] << ") = " << name[p[*vi]] << std::endl;
47+
}
48+
std::cout <<std::endl;
5349

54-
std::ofstream dot_file("figs/dijkstra-eg.dot");
55-
56-
dot_file << "digraph D {\n"
57-
<< " rankdir=LR\n"
58-
<< " size=\"4,3\"\n"
59-
<< " ratio=\"fill\"\n"
60-
<< " edge[style=\"bold\"]\n" << " node[shape=\"circle\"]\n";
61-
62-
graph_traits < graph_t >::edge_iterator ei, ei_end;
63-
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
64-
edge_descriptor e = *ei;
65-
graph_traits < graph_t >::vertex_descriptor
66-
u = source(e, g), v = target(e, g);
67-
dot_file << name[u] << " -> " << name[v]
68-
<< "[label=\"" << get(weightmap, e) << "\"";
69-
if (p[v] == u)
70-
dot_file << ", color=\"black\"";
71-
else
72-
dot_file << ", color=\"grey\"";
73-
dot_file << "]";
74-
}
75-
dot_file << "}";
76-
return EXIT_SUCCESS;
50+
// Generate a .dot file. To png wigh:
51+
// dot -Tpng -o outfile.png input.dot
52+
property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g);
53+
std::ofstream dot_file("dijkstra.dot");
54+
dot_file << "digraph D {\n"
55+
<< " rankdir=LR\n"
56+
<< " size=\"4,3\"\n"
57+
<< " ratio=\"fill\"\n"
58+
<< " edge[style=\"bold\"]\n" << " node[shape=\"circle\"]\n";
59+
graph_traits <graph_t>::edge_iterator ei, ei_end;
60+
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
61+
edge_descriptor e = *ei;
62+
graph_traits < graph_t >::vertex_descriptor
63+
u = source(e, g), v = target(e, g);
64+
dot_file << name[u] << " -> " << name[v]
65+
<< "[label=\"" << get(weightmap, e) << "\"";
66+
if (p[v] == u)
67+
dot_file << ", color=\"black\"";
68+
else
69+
dot_file << ", color=\"grey\"";
70+
dot_file << "]";
71+
}
72+
dot_file << "}";
73+
return EXIT_SUCCESS;
74+
}
7775
}

‎boost/main.cpp‎

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,53 @@ Full list of libs: <http://www.boost.org/doc/libs/>
88
99
Some of boost libraries may be in separate packages / shared objects than others.
1010
11-
The convention is: `-lboost_XXX` to include libs in gcc, for exapmle `-lboost_graph`
11+
The library naming convention is: `-lboost_XXX` to include libs in gcc, e.g. `-lboost_graph`
1212
*/
1313
#include <cassert>
1414
#include <cstdlib>
1515
#include <iostream>
1616
#include <vector>
1717

18+
#include <boost/algorithm/string.hpp> // split
1819
#include <boost/iterator/counting_iterator.hpp>
20+
#include <boost/range/algorithm/remove_if.hpp>
1921

20-
using namespace boost;
22+
int main() {
2123

22-
int main(int, char *[])
23-
{
24-
//#counting_iterator
24+
// #counting_iterator
2525
{
2626
std::vector<int> v(boost::counting_iterator<int>(0), boost::counting_iterator<int>(3));
2727
assert((v == std::vector<int>{0, 1, 2}));
2828
}
2929

30-
return EXIT_SUCCESS;
30+
//#string
31+
{
32+
// #split string into array of strings at a character.
33+
{
34+
std::vector<std::string> strs;
35+
boost::split(strs, "a b\tcd", boost::is_any_of("\t "));
36+
assert((strs == std::vector<std::string>{"a", "", "b", "cd"}));
37+
}
38+
39+
// #strip ##filter
40+
{
41+
// Single character, no single function in C++11.
42+
{
43+
std::string str = "a0bc00d";
44+
boost::erase_all(str, "0");
45+
assert((str == "abcd"));
46+
}
47+
48+
// Any character from a string.
49+
{
50+
std::string str = "a_bc0_d";
51+
str.erase(boost::remove_if(str, boost::is_any_of("_0")), str.end());
52+
assert((str == "abcd"));
53+
54+
std::vector<int> is{0, 1, 2, 0, 3};
55+
str.erase(boost::remove_if(str, boost::is_any_of(std::vector<int>{0, 2})), str.end());
56+
assert((is == std::vector<int>{1, 3}));
57+
}
58+
}
59+
}
3160
}

0 commit comments

Comments
 (0)