From 09292561db537b0832e404b1b08a4954e5d4fd52 Mon Sep 17 00:00:00 2001 From: Yuri Gorshenin Date: Fri, 27 Feb 2015 13:45:01 +0300 Subject: [PATCH] Fixes. --- base/base_tests/worker_thread_test.cpp | 20 +++++++++++--------- base/mem_trie.hpp | 9 ++++----- base/worker_thread.hpp | 2 +- indexer/string_file.hpp | 20 +++++++++++--------- std/memory.hpp | 15 --------------- 5 files changed, 27 insertions(+), 39 deletions(-) delete mode 100644 std/memory.hpp diff --git a/base/base_tests/worker_thread_test.cpp b/base/base_tests/worker_thread_test.cpp index 51f800b21c..a3fc383595 100644 --- a/base/base_tests/worker_thread_test.cpp +++ b/base/base_tests/worker_thread_test.cpp @@ -2,30 +2,32 @@ #include "../worker_thread.hpp" -#include "../../std/memory.hpp" +#include "../../std/shared_ptr.hpp" #include "../../std/vector.hpp" struct Task { - Task(vector & buffer, int index) : m_buffer(buffer), m_index(index) {} + Task(vector & buffer, size_t index) : m_buffer(buffer), m_index(index) {} void operator()() const { m_buffer.push_back(m_index); } - vector & m_buffer; - int m_index; + vector & m_buffer; + size_t m_index; }; UNIT_TEST(WorkerThread_Basic) { - my::WorkerThread thread(5 /* maxTasks */); + size_t const kNumTasks = 10; + size_t const kMaxTasksInQueue = 5; - vector buffer; + my::WorkerThread thread(kMaxTasksInQueue); - for (int i = 0; i < 10; ++i) + vector buffer; + for (size_t i = 0; i < kNumTasks; ++i) thread.Push(make_shared(buffer, i)); thread.RunUntilIdleAndStop(); - TEST_EQUAL(static_cast(10), buffer.size(), ()); + TEST_EQUAL(kNumTasks, buffer.size(), ()); for (size_t i = 0; i < buffer.size(); ++i) - TEST_EQUAL(static_cast(i), buffer[i], ()); + TEST_EQUAL(i, buffer[i], ()); } diff --git a/base/mem_trie.hpp b/base/mem_trie.hpp index 27c3c4cc90..bb0b373ebb 100644 --- a/base/mem_trie.hpp +++ b/base/mem_trie.hpp @@ -51,11 +51,10 @@ private: Node * GetMove(CharT const & c) { - Node ** node = &m_moves[c]; - if (*node) - return *node; - *node = new Node(); - return *node; + Node *& node = m_moves[c]; + if (!node) + node = new Node(); + return node; } void AddValue(const ValueT & value) { m_values.push_back(value); } diff --git a/base/worker_thread.hpp b/base/worker_thread.hpp index 220d398b8a..73776242de 100644 --- a/base/worker_thread.hpp +++ b/base/worker_thread.hpp @@ -3,9 +3,9 @@ #include "macros.hpp" #include "../std/condition_variable.hpp" -#include "../std/memory.hpp" #include "../std/mutex.hpp" #include "../std/queue.hpp" +#include "../std/shared_ptr.hpp" #include "../std/thread.hpp" namespace my diff --git a/indexer/string_file.hpp b/indexer/string_file.hpp index bdcd772231..d6f4b2ad7a 100644 --- a/indexer/string_file.hpp +++ b/indexer/string_file.hpp @@ -61,6 +61,11 @@ public: } }; + using StringsListT = vector; + + // Contains start and end offsets of file portions. + using OffsetsListT = vector>; + /// This class encapsulates a task to efficiently sort a bunch of /// strings and writes them in a sorted oreder. class SortAndDumpStringsTask @@ -75,8 +80,7 @@ public: /// to the list. /// \param strings Vector of strings that should be sorted. Internal data is moved out from /// strings, so it'll become empty after ctor. - SortAndDumpStringsTask(FileWriter & writer, vector> & offsets, - std::vector & strings) + SortAndDumpStringsTask(FileWriter & writer, OffsetsListT & offsets, StringsListT & strings) : m_writer(writer), m_offsets(offsets) { strings.swap(m_strings); @@ -99,7 +103,7 @@ public: } uint64_t const spos = m_writer.Pos(); - m_writer.Write(&memBuffer[0], memBuffer.size()); + m_writer.Write(memBuffer.data(), memBuffer.size()); uint64_t const epos = m_writer.Pos(); m_offsets.push_back(make_pair(spos, epos)); m_writer.Flush(); @@ -107,8 +111,8 @@ public: private: FileWriter & m_writer; - vector> & m_offsets; - vector m_strings; + OffsetsListT & m_offsets; + StringsListT m_strings; DISALLOW_COPY_AND_MOVE(SortAndDumpStringsTask); }; @@ -153,10 +157,8 @@ private: void Flush(); bool PushNextValue(size_t i); - vector m_strings; - - // Contains start and end offsets of file portions. - vector > m_offsets; + StringsListT m_strings; + OffsetsListT m_offsets; // A worker thread that sorts and writes groups of strings. The // whole process looks like a pipeline, i.e. main thread accumulates diff --git a/std/memory.hpp b/std/memory.hpp deleted file mode 100644 index c0edfa8943..0000000000 --- a/std/memory.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once -#include "common_defines.hpp" - -#ifdef new -#undef new -#endif - -#include - -using std::make_shared; -using std::shared_ptr; - -#ifdef DEBUG_NEW -#define new DEBUG_NEW -#endif