]> git.sesse.net Git - plocate/commitdiff
Format everything with clang-format.
authorSteinar H. Gunderson <steinar+git@gunderson.no>
Mon, 28 Sep 2020 22:13:18 +0000 (00:13 +0200)
committerSteinar H. Gunderson <steinar+git@gunderson.no>
Mon, 28 Sep 2020 22:13:38 +0000 (00:13 +0200)
clang-format isn't ideal, but it's better than manual formatting
in the long run.

.clang-format [new file with mode: 0644]
plocate-build.cpp
plocate.cpp

diff --git a/.clang-format b/.clang-format
new file mode 100644 (file)
index 0000000..04a3933
--- /dev/null
@@ -0,0 +1,67 @@
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignEscapedNewlines: DontAlign
+AlignOperands: false
+AlignTrailingComments: false
+AllowAllParametersOfDeclarationOnNextLine: true
+AllowShortBlocksOnASingleLine: false
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: Inline
+AllowShortIfStatementsOnASingleLine: false
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: true
+BinPackArguments: true
+BinPackParameters: true
+BreakBeforeBinaryOperators: None
+BreakBeforeBraces: WebKit
+BreakBeforeTernaryOperators: true
+BreakConstructorInitializers: BeforeColon
+#BreakInheritanceList: BeforeColon
+BreakStringLiterals: false
+CompactNamespaces: false
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+Cpp11BracedListStyle: false
+DerivePointerAlignment: false
+FixNamespaceComments: false
+IncludeBlocks: Regroup
+IndentCaseLabels: false
+IndentPPDirectives: None
+IndentWrappedFunctionNames: false
+KeepEmptyLinesAtTheStartOfBlocks: false
+Language: Cpp
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+PointerAlignment: Right
+ReflowComments: true
+SortIncludes: true
+SortUsingDeclarations: true
+SpaceAfterCStyleCast: false
+SpaceAfterTemplateKeyword: false
+SpaceBeforeAssignmentOperators: true
+#SpaceBeforeCpp11BracedList: false
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeParens: ControlStatements
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 2
+SpacesInAngles: false
+SpacesInCStyleCastParentheses: false
+SpacesInContainerLiterals: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+Standard: Cpp11
+
+# HACK: TabWidth 200 makes sure we use tabs for single-tab indentation only,
+# never for alignment. This makes the code readable at any tab width.
+# It works only because we have ColumnLimit 0.
+ColumnLimit: 0
+UseTab: ForContinuationAndIndentation
+AccessModifierOffset: -200
+ConstructorInitializerIndentWidth: 200
+ContinuationIndentWidth: 200
+IndentWidth: 200
+TabWidth: 200
index 39263b7aaaf62a36f9a013d816d9c17032991748..c95aceaefb8b69c69eef5a854f6d7685750b7eca 100644 (file)
@@ -1,23 +1,23 @@
+#include "vp4.h"
+
+#include <algorithm>
+#include <arpa/inet.h>
 #include <assert.h>
+#include <chrono>
+#include <endian.h>
+#include <fcntl.h>
 #include <stdio.h>
 #include <string.h>
-#include <algorithm>
-#include <unordered_map>
 #include <string>
-#include <vector>
-#include <chrono>
-#include <unistd.h>
-#include <fcntl.h>
 #include <sys/mman.h>
-#include <arpa/inet.h>
-#include <endian.h>
-#include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <unordered_map>
+#include <vector>
 #include <zstd.h>
 
-#include "vp4.h"
-
-#define P4NENC_BOUND(n) ((n+127)/128+(n+32)*sizeof(uint32_t))
+#define P4NENC_BOUND(n) ((n + 127) / 128 + (n + 32) * sizeof(uint32_t))
 #define dprintf(...)
 //#define dprintf(...) fprintf(stderr, __VA_ARGS__);
 
@@ -25,7 +25,7 @@ using namespace std;
 using namespace std::chrono;
 
 string zstd_compress(const string &src, string *tempbuf);
-       
+
 static inline uint32_t read_unigram(const string_view s, size_t idx)
 {
        if (idx < s.size()) {
@@ -42,16 +42,14 @@ static inline uint32_t read_trigram(const string_view s, size_t start)
                (read_unigram(s, start + 2) << 16);
 }
 
-enum
-{
-       DBE_NORMAL          = 0,    /* A non-directory file */
-       DBE_DIRECTORY       = 1,    /* A directory */
-       DBE_END             = 2   /* End of directory contents; contains no name */
+enum {
+       DBE_NORMAL = 0, /* A non-directory file */
+       DBE_DIRECTORY = 1, /* A directory */
+       DBE_END = 2 /* End of directory contents; contains no name */
 };
 
 // From mlocate.
-struct db_header
-{
+struct db_header {
        uint8_t magic[8];
        uint32_t conf_size;
        uint8_t version;
@@ -60,8 +58,7 @@ struct db_header
 };
 
 // From mlocate.
-struct db_directory
-{
+struct db_directory {
        uint64_t time_sec;
        uint32_t time_nsec;
        uint8_t pad[4];
@@ -143,7 +140,8 @@ void PostingListBuilder::write_header(uint32_t docid)
 
 class Corpus {
 public:
-       Corpus(size_t block_size) : block_size(block_size) {}
+       Corpus(size_t block_size)
+               : block_size(block_size) {}
        void add_file(string filename);
        void flush_block();
 
@@ -207,8 +205,8 @@ const char *handle_directory(const char *ptr, Corpus *corpus)
        if (dir_path == "/") {
                dir_path = "";
        }
-               
-       for ( ;; ) {
+
+       for (;;) {
                uint8_t type = *ptr++;
                if (type == DBE_NORMAL) {
                        string filename = ptr;
@@ -282,7 +280,8 @@ void do_build(const char *infile, const char *outfile, int block_size)
                                break;
                        }
                        string s(buf);
-                       if (s.back() == '\n') s.pop_back();
+                       if (s.back() == '\n')
+                               s.pop_back();
                        corpus.add_file(move(s));
                }
                fclose(fp);
@@ -299,7 +298,7 @@ void do_build(const char *infile, const char *outfile, int block_size)
                bytes_used += pl_builder.encoded.size();
        }
        dprintf("%zu files, %zu different trigrams, %zu entries, avg len %.2f, longest %zu\n",
-               corpus.num_files, corpus.invindex.size(), trigrams, double(trigrams) / corpus.invindex.size(), longest_posting_list);
+               corpus.num_files, corpus.invindex.size(), trigrams, double(trigrams) / corpus.invindex.size(), longest_posting_list);
        dprintf("%zu bytes used for posting lists (%.2f bits/entry)\n", bytes_used, 8 * bytes_used / double(trigrams));
 
        //steady_clock::time_point end = steady_clock::now();
@@ -356,7 +355,7 @@ void do_build(const char *infile, const char *outfile, int block_size)
                bytes_for_filename_index += sizeof(offset);
                bytes_for_filenames += filename.size();
        }
-       
+
        // Write the actual filenames.
        for (const string &filename : corpus.filename_blocks) {
                fwrite(filename.data(), filename.size(), 1, outfp);
index 6af03d39679cbb8fa98ede663f9d25a302f82fb3..138eb36705473458d8d04cd64313292aa36db55f 100644 (file)
@@ -1,27 +1,27 @@
-#include <stdio.h>
-#include <string.h>
+#include "vp4.h"
+
 #include <algorithm>
-#include <unordered_map>
-#include <string>
-#include <vector>
+#include <arpa/inet.h>
 #include <chrono>
-#include <unistd.h>
+#include <endian.h>
 #include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <string>
 #include <sys/mman.h>
-#include <arpa/inet.h>
-#include <endian.h>
+#include <unistd.h>
+#include <unordered_map>
+#include <vector>
 #include <zstd.h>
 
-#include "vp4.h"
-
-#define P4NENC_BOUND(n) ((n+127)/128+(n+32)*sizeof(uint32_t))
+#define P4NENC_BOUND(n) ((n + 127) / 128 + (n + 32) * sizeof(uint32_t))
 
 using namespace std;
 using namespace std::chrono;
 
 #define dprintf(...)
 //#define dprintf(...) fprintf(stderr, __VA_ARGS__);
-       
+
 static inline uint32_t read_unigram(const string &s, size_t idx)
 {
        if (idx < s.size()) {
@@ -33,12 +33,12 @@ static inline uint32_t read_unigram(const string &s, size_t idx)
 
 static inline uint32_t read_trigram(const string &s, size_t start)
 {
-       return read_unigram(s, start) |
-               (read_unigram(s, start + 1) << 8) |
+       return read_unigram(s, start) | (read_unigram(s, start + 1) << 8) |
                (read_unigram(s, start + 2) << 16);
 }
 
-bool has_access(const char *filename, unordered_map<string, bool> *access_rx_cache)
+bool has_access(const char *filename,
+                unordered_map<string, bool> *access_rx_cache)
 {
        const char *end = strchr(filename + 1, '/');
        while (end != nullptr) {
@@ -71,7 +71,8 @@ public:
        Corpus(int fd);
        ~Corpus();
        const Trigram *find_trigram(uint32_t trgm) const;
-       const unsigned char *get_compressed_posting_list(const Trigram *trigram) const;
+       const unsigned char *
+       get_compressed_posting_list(const Trigram *trigram) const;
        string_view get_compressed_filename_block(uint32_t docid) const;
 
 private:
@@ -112,16 +113,17 @@ Corpus::~Corpus()
 
 const Trigram *Corpus::find_trigram(uint32_t trgm) const
 {
-       const Trigram *trgmptr = lower_bound(trgm_begin, trgm_end, trgm, [](const Trigram &trgm, uint32_t t) {
-               return trgm.trgm < t;
-       });
+       const Trigram *trgmptr = lower_bound(
+               trgm_begin, trgm_end, trgm,
+               [](const Trigram &trgm, uint32_t t) { return trgm.trgm < t; });
        if (trgmptr == trgm_end || trgmptr->trgm != trgm) {
                return nullptr;
        }
        return trgmptr;
 }
 
-const unsigned char *Corpus::get_compressed_posting_list(const Trigram *trgmptr) const
+const unsigned char *
+Corpus::get_compressed_posting_list(const Trigram *trgmptr) const
 {
        return reinterpret_cast<const unsigned char *>(data + trgmptr->offset);
 }
@@ -129,24 +131,29 @@ const unsigned char *Corpus::get_compressed_posting_list(const Trigram *trgmptr)
 string_view Corpus::get_compressed_filename_block(uint32_t docid) const
 {
        const char *compressed = (const char *)(data + filename_offsets[docid]);
-       size_t compressed_size = filename_offsets[docid + 1] - filename_offsets[docid];  // Allowed we have a sentinel block at the end.
-       return {compressed, compressed_size};
+       size_t compressed_size =
+               filename_offsets[docid + 1] -
+               filename_offsets[docid];  // Allowed we have a sentinel block at the end.
+       return { compressed, compressed_size };
 }
 
-size_t scan_docid(const string &needle, uint32_t docid, const Corpus &corpus, unordered_map<string, bool> *access_rx_cache)
+size_t scan_docid(const string &needle, uint32_t docid, const Corpus &corpus,
+                  unordered_map<string, bool> *access_rx_cache)
 {
        string_view compressed = corpus.get_compressed_filename_block(docid);
        size_t matched = 0;
 
        string block;
-       block.resize(ZSTD_getFrameContentSize(compressed.data(), compressed.size()) + 1);
+       block.resize(ZSTD_getFrameContentSize(compressed.data(), compressed.size()) +
+                    1);
 
-       ZSTD_decompress(&block[0], block.size(), compressed.data(), compressed.size());
+       ZSTD_decompress(&block[0], block.size(), compressed.data(),
+                       compressed.size());
        block[block.size() - 1] = '\0';
 
        for (const char *filename = block.data();
-                       filename != block.data() + block.size();
-                       filename += strlen(filename) + 1) {
+            filename != block.data() + block.size();
+            filename += strlen(filename) + 1) {
                if (strstr(filename, needle.c_str()) == nullptr) {
                        continue;
                }
@@ -172,7 +179,7 @@ void do_search_file(const string &needle, const char *filename)
                exit(EXIT_FAILURE);
        }
 
-       //steady_clock::time_point start = steady_clock::now();
+       // steady_clock::time_point start = steady_clock::now();
        if (access("/", R_OK | X_OK)) {
                // We can't find anything, no need to bother...
                return;
@@ -195,24 +202,27 @@ void do_search_file(const string &needle, const char *filename)
                auto last = unique(trigrams.begin(), trigrams.end());
                trigrams.erase(last, trigrams.end());
        }
-       sort(trigrams.begin(), trigrams.end(), [&](const Trigram *a, const Trigram *b) {
-               return a->num_docids < b->num_docids;
-       });
+       sort(trigrams.begin(), trigrams.end(),
+            [&](const Trigram *a, const Trigram *b) {
+                    return a->num_docids < b->num_docids;
+            });
 
        vector<uint32_t> in1, in2, out;
        for (const Trigram *trgmptr : trigrams) {
-               //uint32_t trgm = trgmptr->trgm;
+               // uint32_t trgm = trgmptr->trgm;
                size_t num = trgmptr->num_docids;
                const unsigned char *pldata = corpus.get_compressed_posting_list(trgmptr);
                if (in1.empty()) {
                        in1.resize(num + 128);
                        p4nd1dec128v32(const_cast<unsigned char *>(pldata), num, &in1[0]);
                        in1.resize(num);
-                       dprintf("trigram '%c%c%c' decoded to %zu entries\n", trgm & 0xff, (trgm >> 8) & 0xff, (trgm >> 16) & 0xff, num);
+                       dprintf("trigram '%c%c%c' decoded to %zu entries\n", trgm & 0xff,
+                               (trgm >> 8) & 0xff, (trgm >> 16) & 0xff, num);
                } else {
                        if (num > in1.size() * 100) {
-                               dprintf("trigram '%c%c%c' has %zu entries, ignoring the rest (will weed out false positives later)\n",
-                                       trgm & 0xff, (trgm >> 8) & 0xff, (trgm >> 16) & 0xff, num);
+                               dprintf("trigram '%c%c%c' has %zu entries, ignoring the rest (will "
+                                       "weed out false positives later)\n",
+                                       trgm & 0xff, (trgm >> 8) & 0xff, (trgm >> 16) & 0xff, num);
                                break;
                        }
 
@@ -222,9 +232,12 @@ void do_search_file(const string &needle, const char *filename)
                        p4nd1dec128v32(const_cast<unsigned char *>(pldata), num, &in2[0]);
 
                        out.clear();
-                       set_intersection(in1.begin(), in1.end(), in2.begin(), in2.begin() + num, back_inserter(out));
+                       set_intersection(in1.begin(), in1.end(), in2.begin(), in2.begin() + num,
+                                        back_inserter(out));
                        swap(in1, out);
-                       dprintf("trigram '%c%c%c' decoded to %zu entries, %zu left\n", trgm & 0xff, (trgm >> 8) & 0xff, (trgm >> 16) & 0xff, num, in1.size());
+                       dprintf("trigram '%c%c%c' decoded to %zu entries, %zu left\n",
+                               trgm & 0xff, (trgm >> 8) & 0xff, (trgm >> 16) & 0xff, num,
+                               in1.size());
                        if (in1.empty()) {
                                dprintf("no matches (intersection list is empty)\n");
                                break;
@@ -234,7 +247,7 @@ void do_search_file(const string &needle, const char *filename)
        steady_clock::time_point end = steady_clock::now();
 
        dprintf("Intersection took %.1f ms. Doing final verification and printing:\n",
-               1e3 * duration<float>(end - start).count());
+               1e3 * duration<float>(end - start).count());
 
        unordered_map<string, bool> access_rx_cache;
 
@@ -244,7 +257,7 @@ void do_search_file(const string &needle, const char *filename)
        }
        end = steady_clock::now();
        dprintf("Done in %.1f ms, found %d matches.\n",
-               1e3 * duration<float>(end - start).count(), matched);
+               1e3 * duration<float>(end - start).count(), matched);
 }
 
 int main(int argc, char **argv)