From 109ce9b82adf12a96c24d8ffb0ba2a7ba51629be Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 5 Nov 2015 17:22:34 +0100 Subject: [PATCH] Use string_ref instead of string and implement join(). --- CMakeLists.txt | 6 +++++ src/main.cpp | 3 +++ src/pathname.cpp | 68 ++++++++++++++++++++++++++++++++++++++---------- src/pathname.hpp | 11 +++++++- 4 files changed, 73 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a3dfe70..966bb19 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,8 +4,14 @@ project(dindexer VERSION 0.1 LANGUAGES CXX) set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -std=c++11") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -std=c++11") +find_library (Boost REQUIRED 1.53.0) + add_executable(${PROJECT_NAME} src/main.cpp src/filesearcher.cpp src/pathname.cpp ) + +target_include_directories(${PROJECT_NAME} SYSTEM + INTERFACE ${Boost_INCLUDE_DIR} +) diff --git a/src/main.cpp b/src/main.cpp index 428f6c3..52aee7f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -50,5 +50,8 @@ int main (int parArgc, char* parArgv[]) { std::cout << pn2.path() << '\n'; std::cout << pn3.path() << '\n'; std::cout << pn4.path() << '\n'; + + pn1.join(pn2); + std::cout << pn1.path() << '\n'; return 0; } diff --git a/src/pathname.cpp b/src/pathname.cpp index 6155a24..d0abacd 100644 --- a/src/pathname.cpp +++ b/src/pathname.cpp @@ -17,34 +17,45 @@ #include "pathname.hpp" #include -#include +#include +#include + +#include namespace din { namespace { - void split_path (std::vector* parOut, const char* parPath, std::size_t parLen) { + bool ptr_between (const char* parPtr, const char* parBeg, const char* parEnd) { + std::less less; + std::less_equal lesseq; + + return lesseq(parBeg, parPtr) and less(parPtr, parEnd); + } + + void split_path (std::vector* parOut, boost::string_ref parPath) { parOut->clear(); - const char* from = parPath; - const char* next; - while (parPath + parLen != (next = std::find(from, parPath + parLen, '/'))) { + auto from = parPath.begin(); + boost::string_ref::const_iterator next; + const auto end = parPath.end(); + const auto beg = parPath.begin(); + while (end != (next = std::find(from, end, '/'))) { if (next != from) { - parOut->push_back(std::string(from, next)); + parOut->push_back(parPath.substr(from - beg, next - from)); from = next; } ++from; } if (next != from) { - parOut->push_back(std::string(from, next)); + parOut->push_back(parPath.substr(from - beg, next - from)); } } } //unnamed namespace - PathName::PathName (const char* parPath) { - if (not parPath) { - m_absolute = false; - } - else { + PathName::PathName (const char* parPath) : + m_original_path(parPath) + { + if (not m_original_path.empty()) { m_absolute = ('/' == *parPath); - split_path(&m_atoms, parPath, std::strlen(parPath)); + split_path(&m_atoms, m_original_path); } } @@ -69,9 +80,38 @@ namespace din { const char* slash = (m_absolute ? "/" : ""); for (const auto& itm : m_atoms) { out += slash; - out += itm; + out.insert(out.end(), itm.begin(), itm.end()); slash = "/"; } return std::move(out); } + + void PathName::join (const PathName& parOther) { + typedef std::pair PairType; + using boost::string_ref; + + for (const auto& itm : parOther.m_pool) { + m_pool[itm.first] += itm.second; + } + const auto& other_path = parOther.original_path(); + const auto it_other_path = m_pool.insert(PairType(other_path, 0)).first; + + for (auto str : parOther.m_atoms) { + if (ptr_between(str.data(), other_path.data(), other_path.data() + other_path.size())) { + it_other_path->second++; + auto offset = str.data() - other_path.data(); + m_atoms.push_back(string_ref(it_other_path->first).substr(offset, str.size())); + } + else { + } + } + if (not it_other_path->second) { + m_pool.erase(it_other_path); + } + + std::cout << " --------------- content -----------------\n"; + for (const auto& itm : m_pool) { + std::cout << itm.first << " - " << itm.second << '\n'; + } + } } //namespace din diff --git a/src/pathname.hpp b/src/pathname.hpp index 18e0902..0b71610 100644 --- a/src/pathname.hpp +++ b/src/pathname.hpp @@ -20,6 +20,8 @@ #include #include +#include +#include namespace din { class PathName { @@ -29,9 +31,16 @@ namespace din { bool is_absolute ( void ) const; std::string path ( void ) const; + const std::string& original_path ( void ) const { return m_original_path; } + std::size_t atom_count ( void ) const { return m_atoms.size(); } + const boost::string_ref operator[] ( std::size_t parIndex ) const { return m_atoms[parIndex]; } + void join ( const PathName& parOther ); private: - std::vector m_atoms; + typedef std::vector AtomList; + std::map m_pool; + AtomList m_atoms; + std::string m_original_path; bool m_absolute; }; } //namespace din