Skip to content

Commit 566c7a2

Browse files
author
Ian Hunter
committed
Selecting Google Code Style for C/C++
1 parent f37b48b commit 566c7a2

22 files changed

+2238
-1941
lines changed

.clang-format

+226
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
---
2+
Language: Cpp
3+
# BasedOnStyle: Google
4+
AccessModifierOffset: -1
5+
AlignAfterOpenBracket: Align
6+
AlignArrayOfStructures: None
7+
AlignConsecutiveMacros: None
8+
AlignConsecutiveAssignments: None
9+
AlignConsecutiveBitFields: None
10+
AlignConsecutiveDeclarations: None
11+
AlignEscapedNewlines: Left
12+
AlignOperands: Align
13+
AlignTrailingComments: true
14+
AllowAllArgumentsOnNextLine: true
15+
AllowAllParametersOfDeclarationOnNextLine: true
16+
AllowShortEnumsOnASingleLine: true
17+
AllowShortBlocksOnASingleLine: Never
18+
AllowShortCaseLabelsOnASingleLine: false
19+
AllowShortFunctionsOnASingleLine: All
20+
AllowShortLambdasOnASingleLine: All
21+
AllowShortIfStatementsOnASingleLine: WithoutElse
22+
AllowShortLoopsOnASingleLine: true
23+
AlwaysBreakAfterDefinitionReturnType: None
24+
AlwaysBreakAfterReturnType: None
25+
AlwaysBreakBeforeMultilineStrings: true
26+
AlwaysBreakTemplateDeclarations: Yes
27+
AttributeMacros:
28+
- __capability
29+
BinPackArguments: true
30+
BinPackParameters: true
31+
BraceWrapping:
32+
AfterCaseLabel: false
33+
AfterClass: false
34+
AfterControlStatement: Never
35+
AfterEnum: false
36+
AfterFunction: false
37+
AfterNamespace: false
38+
AfterObjCDeclaration: false
39+
AfterStruct: false
40+
AfterUnion: false
41+
AfterExternBlock: false
42+
BeforeCatch: false
43+
BeforeElse: false
44+
BeforeLambdaBody: false
45+
BeforeWhile: false
46+
IndentBraces: false
47+
SplitEmptyFunction: true
48+
SplitEmptyRecord: true
49+
SplitEmptyNamespace: true
50+
BreakBeforeBinaryOperators: None
51+
BreakBeforeConceptDeclarations: true
52+
BreakBeforeBraces: Attach
53+
BreakBeforeInheritanceComma: false
54+
BreakInheritanceList: BeforeColon
55+
BreakBeforeTernaryOperators: true
56+
BreakConstructorInitializersBeforeComma: false
57+
BreakConstructorInitializers: BeforeColon
58+
BreakAfterJavaFieldAnnotations: false
59+
BreakStringLiterals: true
60+
ColumnLimit: 80
61+
CommentPragmas: '^ IWYU pragma:'
62+
QualifierAlignment: Leave
63+
CompactNamespaces: false
64+
ConstructorInitializerIndentWidth: 4
65+
ContinuationIndentWidth: 4
66+
Cpp11BracedListStyle: true
67+
DeriveLineEnding: true
68+
DerivePointerAlignment: true
69+
DisableFormat: false
70+
EmptyLineAfterAccessModifier: Never
71+
EmptyLineBeforeAccessModifier: LogicalBlock
72+
ExperimentalAutoDetectBinPacking: false
73+
PackConstructorInitializers: NextLine
74+
BasedOnStyle: ''
75+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
76+
AllowAllConstructorInitializersOnNextLine: true
77+
FixNamespaceComments: true
78+
ForEachMacros:
79+
- foreach
80+
- Q_FOREACH
81+
- BOOST_FOREACH
82+
IfMacros:
83+
- KJ_IF_MAYBE
84+
IncludeBlocks: Regroup
85+
IncludeCategories:
86+
- Regex: '^<ext/.*\.h>'
87+
Priority: 2
88+
SortPriority: 0
89+
CaseSensitive: false
90+
- Regex: '^<.*\.h>'
91+
Priority: 1
92+
SortPriority: 0
93+
CaseSensitive: false
94+
- Regex: '^<.*'
95+
Priority: 2
96+
SortPriority: 0
97+
CaseSensitive: false
98+
- Regex: '.*'
99+
Priority: 3
100+
SortPriority: 0
101+
CaseSensitive: false
102+
IncludeIsMainRegex: '([-_](test|unittest))?$'
103+
IncludeIsMainSourceRegex: ''
104+
IndentAccessModifiers: false
105+
IndentCaseLabels: true
106+
IndentCaseBlocks: false
107+
IndentGotoLabels: true
108+
IndentPPDirectives: None
109+
IndentExternBlock: AfterExternBlock
110+
IndentRequires: false
111+
IndentWidth: 2
112+
IndentWrappedFunctionNames: false
113+
InsertTrailingCommas: None
114+
JavaScriptQuotes: Leave
115+
JavaScriptWrapImports: true
116+
KeepEmptyLinesAtTheStartOfBlocks: false
117+
LambdaBodyIndentation: Signature
118+
MacroBlockBegin: ''
119+
MacroBlockEnd: ''
120+
MaxEmptyLinesToKeep: 1
121+
NamespaceIndentation: None
122+
ObjCBinPackProtocolList: Never
123+
ObjCBlockIndentWidth: 2
124+
ObjCBreakBeforeNestedBlockParam: true
125+
ObjCSpaceAfterProperty: false
126+
ObjCSpaceBeforeProtocolList: true
127+
PenaltyBreakAssignment: 2
128+
PenaltyBreakBeforeFirstCallParameter: 1
129+
PenaltyBreakComment: 300
130+
PenaltyBreakFirstLessLess: 120
131+
PenaltyBreakOpenParenthesis: 0
132+
PenaltyBreakString: 1000
133+
PenaltyBreakTemplateDeclaration: 10
134+
PenaltyExcessCharacter: 1000000
135+
PenaltyReturnTypeOnItsOwnLine: 200
136+
PenaltyIndentedWhitespace: 0
137+
PointerAlignment: Left
138+
PPIndentWidth: -1
139+
RawStringFormats:
140+
- Language: Cpp
141+
Delimiters:
142+
- cc
143+
- CC
144+
- cpp
145+
- Cpp
146+
- CPP
147+
- 'c++'
148+
- 'C++'
149+
CanonicalDelimiter: ''
150+
BasedOnStyle: google
151+
- Language: TextProto
152+
Delimiters:
153+
- pb
154+
- PB
155+
- proto
156+
- PROTO
157+
EnclosingFunctions:
158+
- EqualsProto
159+
- EquivToProto
160+
- PARSE_PARTIAL_TEXT_PROTO
161+
- PARSE_TEST_PROTO
162+
- PARSE_TEXT_PROTO
163+
- ParseTextOrDie
164+
- ParseTextProtoOrDie
165+
- ParseTestProto
166+
- ParsePartialTestProto
167+
CanonicalDelimiter: pb
168+
BasedOnStyle: google
169+
ReferenceAlignment: Pointer
170+
ReflowComments: true
171+
RemoveBracesLLVM: false
172+
SeparateDefinitionBlocks: Leave
173+
ShortNamespaceLines: 1
174+
SortIncludes: CaseSensitive
175+
SortJavaStaticImport: Before
176+
SortUsingDeclarations: true
177+
SpaceAfterCStyleCast: false
178+
SpaceAfterLogicalNot: false
179+
SpaceAfterTemplateKeyword: true
180+
SpaceBeforeAssignmentOperators: true
181+
SpaceBeforeCaseColon: false
182+
SpaceBeforeCpp11BracedList: false
183+
SpaceBeforeCtorInitializerColon: true
184+
SpaceBeforeInheritanceColon: true
185+
SpaceBeforeParens: ControlStatements
186+
SpaceBeforeParensOptions:
187+
AfterControlStatements: true
188+
AfterForeachMacros: true
189+
AfterFunctionDefinitionName: false
190+
AfterFunctionDeclarationName: false
191+
AfterIfMacros: true
192+
AfterOverloadedOperator: false
193+
BeforeNonEmptyParentheses: false
194+
SpaceAroundPointerQualifiers: Default
195+
SpaceBeforeRangeBasedForLoopColon: true
196+
SpaceInEmptyBlock: false
197+
SpaceInEmptyParentheses: false
198+
SpacesBeforeTrailingComments: 2
199+
SpacesInAngles: Never
200+
SpacesInConditionalStatement: false
201+
SpacesInContainerLiterals: true
202+
SpacesInCStyleCastParentheses: false
203+
SpacesInLineCommentPrefix:
204+
Minimum: 1
205+
Maximum: -1
206+
SpacesInParentheses: false
207+
SpacesInSquareBrackets: false
208+
SpaceBeforeSquareBrackets: false
209+
BitFieldColonSpacing: Both
210+
Standard: Auto
211+
StatementAttributeLikeMacros:
212+
- Q_EMIT
213+
StatementMacros:
214+
- Q_UNUSED
215+
- QT_REQUIRE_VERSION
216+
TabWidth: 8
217+
UseCRLF: false
218+
UseTab: Never
219+
WhitespaceSensitiveMacros:
220+
- STRINGIZE
221+
- PP_STRINGIZE
222+
- BOOST_PP_STRINGIZE
223+
- NS_SWIFT_NAME
224+
- CF_SWIFT_NAME
225+
...
226+

.github/CONTRIBUTING.md

+1
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,4 @@ Currently there are not enough people using the project to warrant a chat system
103103
This will be appended as we add language support
104104

105105
* **Python** - [PEP8](https://www.python.org/dev/peps/pep-0008/)
106+
* **C/C++** - [Google](https://google.github.io/styleguide/cppguide.html)

src/grammar/external/pcg_basic.c

+46-56
Original file line numberDiff line numberDiff line change
@@ -39,77 +39,67 @@ static pcg32_random_t pcg32_global = PCG32_INITIALIZER;
3939
// Seed the rng. Specified in two parts, state initializer and a
4040
// sequence selection constant (a.k.a. stream id)
4141

42-
void pcg32_srandom_r(pcg32_random_t* rng, uint64_t initstate, uint64_t initseq)
43-
{
44-
rng->state = 0U;
45-
rng->inc = (initseq << 1u) | 1u;
46-
pcg32_random_r(rng);
47-
rng->state += initstate;
48-
pcg32_random_r(rng);
42+
void pcg32_srandom_r(pcg32_random_t* rng, uint64_t initstate,
43+
uint64_t initseq) {
44+
rng->state = 0U;
45+
rng->inc = (initseq << 1u) | 1u;
46+
pcg32_random_r(rng);
47+
rng->state += initstate;
48+
pcg32_random_r(rng);
4949
}
5050

51-
void pcg32_srandom(uint64_t seed, uint64_t seq)
52-
{
53-
pcg32_srandom_r(&pcg32_global, seed, seq);
51+
void pcg32_srandom(uint64_t seed, uint64_t seq) {
52+
pcg32_srandom_r(&pcg32_global, seed, seq);
5453
}
5554

5655
// pcg32_random()
5756
// pcg32_random_r(rng)
5857
// Generate a uniformly distributed 32-bit random number
5958

60-
uint32_t pcg32_random_r(pcg32_random_t* rng)
61-
{
62-
uint64_t oldstate = rng->state;
63-
rng->state = oldstate * 6364136223846793005ULL + rng->inc;
64-
uint32_t xorshifted = ((oldstate >> 18u) ^ oldstate) >> 27u;
65-
uint32_t rot = oldstate >> 59u;
66-
return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
67-
}
68-
69-
uint32_t pcg32_random()
70-
{
71-
return pcg32_random_r(&pcg32_global);
59+
uint32_t pcg32_random_r(pcg32_random_t* rng) {
60+
uint64_t oldstate = rng->state;
61+
rng->state = oldstate * 6364136223846793005ULL + rng->inc;
62+
uint32_t xorshifted = ((oldstate >> 18u) ^ oldstate) >> 27u;
63+
uint32_t rot = oldstate >> 59u;
64+
return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
7265
}
7366

67+
uint32_t pcg32_random() { return pcg32_random_r(&pcg32_global); }
7468

7569
// pcg32_boundedrand(bound):
7670
// pcg32_boundedrand_r(rng, bound):
7771
// Generate a uniformly distributed number, r, where 0 <= r < bound
7872

79-
uint32_t pcg32_boundedrand_r(pcg32_random_t* rng, uint32_t bound)
80-
{
81-
// To avoid bias, we need to make the range of the RNG a multiple of
82-
// bound, which we do by dropping output less than a threshold.
83-
// A naive scheme to calculate the threshold would be to do
84-
//
85-
// uint32_t threshold = 0x100000000ull % bound;
86-
//
87-
// but 64-bit div/mod is slower than 32-bit div/mod (especially on
88-
// 32-bit platforms). In essence, we do
89-
//
90-
// uint32_t threshold = (0x100000000ull-bound) % bound;
91-
//
92-
// because this version will calculate the same modulus, but the LHS
93-
// value is less than 2^32.
94-
95-
uint32_t threshold = -bound % bound;
96-
97-
// Uniformity guarantees that this loop will terminate. In practice, it
98-
// should usually terminate quickly; on average (assuming all bounds are
99-
// equally likely), 82.25% of the time, we can expect it to require just
100-
// one iteration. In the worst case, someone passes a bound of 2^31 + 1
101-
// (i.e., 2147483649), which invalidates almost 50% of the range. In
102-
// practice, bounds are typically small and only a tiny amount of the range
103-
// is eliminated.
104-
for (;;) {
105-
uint32_t r = pcg32_random_r(rng);
106-
if (r >= threshold)
107-
return r % bound;
108-
}
73+
uint32_t pcg32_boundedrand_r(pcg32_random_t* rng, uint32_t bound) {
74+
// To avoid bias, we need to make the range of the RNG a multiple of
75+
// bound, which we do by dropping output less than a threshold.
76+
// A naive scheme to calculate the threshold would be to do
77+
//
78+
// uint32_t threshold = 0x100000000ull % bound;
79+
//
80+
// but 64-bit div/mod is slower than 32-bit div/mod (especially on
81+
// 32-bit platforms). In essence, we do
82+
//
83+
// uint32_t threshold = (0x100000000ull-bound) % bound;
84+
//
85+
// because this version will calculate the same modulus, but the LHS
86+
// value is less than 2^32.
87+
88+
uint32_t threshold = -bound % bound;
89+
90+
// Uniformity guarantees that this loop will terminate. In practice, it
91+
// should usually terminate quickly; on average (assuming all bounds are
92+
// equally likely), 82.25% of the time, we can expect it to require just
93+
// one iteration. In the worst case, someone passes a bound of 2^31 + 1
94+
// (i.e., 2147483649), which invalidates almost 50% of the range. In
95+
// practice, bounds are typically small and only a tiny amount of the range
96+
// is eliminated.
97+
for (;;) {
98+
uint32_t r = pcg32_random_r(rng);
99+
if (r >= threshold) return r % bound;
100+
}
109101
}
110102

111-
112-
uint32_t pcg32_boundedrand(uint32_t bound)
113-
{
114-
return pcg32_boundedrand_r(&pcg32_global, bound);
103+
uint32_t pcg32_boundedrand(uint32_t bound) {
104+
return pcg32_boundedrand_r(&pcg32_global, bound);
115105
}

src/grammar/external/pcg_basic.h

+8-9
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,29 @@
3333

3434
#include <inttypes.h>
3535

36-
3736
#ifdef __cplusplus
3837
extern "C" {
3938
#endif
4039

41-
struct pcg_state_setseq_64 { // Internals are *Private*.
42-
uint64_t state; // RNG state. All values are possible.
43-
uint64_t inc; // Controls which RNG sequence (stream) is
44-
// selected. Must *always* be odd.
40+
struct pcg_state_setseq_64 { // Internals are *Private*.
41+
uint64_t state; // RNG state. All values are possible.
42+
uint64_t inc; // Controls which RNG sequence (stream) is
43+
// selected. Must *always* be odd.
4544
};
4645
typedef struct pcg_state_setseq_64 pcg32_random_t;
4746

4847
// If you *must* statically initialize it, here's one.
4948

50-
#define PCG32_INITIALIZER { 0x853c49e6748fea9bULL, 0xda3e39cb94b95bdbULL }
49+
#define PCG32_INITIALIZER \
50+
{ 0x853c49e6748fea9bULL, 0xda3e39cb94b95bdbULL }
5151

5252
// pcg32_srandom(initstate, initseq)
5353
// pcg32_srandom_r(rng, initstate, initseq):
5454
// Seed the rng. Specified in two parts, state initializer and a
5555
// sequence selection constant (a.k.a. stream id)
5656

5757
void pcg32_srandom(uint64_t initstate, uint64_t initseq);
58-
void pcg32_srandom_r(pcg32_random_t* rng, uint64_t initstate,
59-
uint64_t initseq);
58+
void pcg32_srandom_r(pcg32_random_t* rng, uint64_t initstate, uint64_t initseq);
6059

6160
// pcg32_random()
6261
// pcg32_random_r(rng)
@@ -76,4 +75,4 @@ uint32_t pcg32_boundedrand_r(pcg32_random_t* rng, uint32_t bound);
7675
}
7776
#endif
7877

79-
#endif // PCG_BASIC_H_INCLUDED
78+
#endif // PCG_BASIC_H_INCLUDED

0 commit comments

Comments
 (0)