From ff99d9bbb9e4713701cbf9e4786f612722d0d633 Mon Sep 17 00:00:00 2001 From: Ivan Morozov Date: Tue, 5 Jul 2022 22:51:06 +0300 Subject: [PATCH] correct SVector --- Common/cppcf/cfcpp.pro | 4 +- Common/cppcf/compoundfile.cpp | 16 +++---- Common/cppcf/slist.h | 30 ++++++++++++ Common/cppcf/streamview.cpp | 6 +-- Common/cppcf/streamview.h | 8 ++-- Common/cppcf/svector.cpp | 86 ----------------------------------- Common/cppcf/svector.h | 44 +++++------------- 7 files changed, 59 insertions(+), 135 deletions(-) create mode 100644 Common/cppcf/slist.h delete mode 100644 Common/cppcf/svector.cpp diff --git a/Common/cppcf/cfcpp.pro b/Common/cppcf/cfcpp.pro index 48c981800e..e1559dc18e 100644 --- a/Common/cppcf/cfcpp.pro +++ b/Common/cppcf/cfcpp.pro @@ -22,8 +22,7 @@ SOURCES += \ sector.cpp \ sectorcollection.cpp \ streamrw.cpp \ - streamview.cpp \ - svector.cpp + streamview.cpp HEADERS += \ RBTree/irbnode.h \ @@ -40,6 +39,7 @@ HEADERS += \ idirectoryentry.h \ sector.h \ sectorcollection.h \ + slist.h \ streamrw.h \ streamview.h \ svector.h diff --git a/Common/cppcf/compoundfile.cpp b/Common/cppcf/compoundfile.cpp index e5763b8ba5..2536c56d60 100644 --- a/Common/cppcf/compoundfile.cpp +++ b/Common/cppcf/compoundfile.cpp @@ -426,7 +426,7 @@ SVector CompoundFile::GetFatSectorChain() std::unordered_set processedSectors; std::streamsize stLength = header.fatSectorsNumber > N_HEADER_FAT_ENTRY ? (header.fatSectorsNumber - N_HEADER_FAT_ENTRY) * 4 : 0; - SVector zeroQueue; + SList zeroQueue; std::shared_ptr difatStream( new StreamView @@ -558,8 +558,8 @@ SVector CompoundFile::GetNormalSectorChain(int secID) SVector fatSectors = GetFatSectorChain(); std::unordered_set processedSectors; - SVector availableSectors; - StreamView fatStream(fatSectors, GetSectorSize(), fatSectors.size() * GetSectorSize(), availableSectors, sourceStream); + SList zeroQueue; + StreamView fatStream(fatSectors, GetSectorSize(), fatSectors.size() * GetSectorSize(), zeroQueue, sourceStream); while (true) { @@ -606,12 +606,12 @@ SVector CompoundFile::GetMiniSectorChain(int secID) SVector miniFAT = GetNormalSectorChain(header.firstMiniFATSectorID); SVector miniStream = GetNormalSectorChain(RootEntry()->getStartSetc()); - SVector zeroVector; + SList zeroQueue; - StreamView miniFATView(miniFAT, GetSectorSize(), header.miniFATSectorsNumber * Sector::MINISECTOR_SIZE, zeroVector, sourceStream); + StreamView miniFATView(miniFAT, GetSectorSize(), header.miniFATSectorsNumber * Sector::MINISECTOR_SIZE, zeroQueue, sourceStream); // TODO here - StreamView miniStreamView(miniStream, GetSectorSize(), rootStorage.Size, zeroVector, sourceStream); + StreamView miniStreamView(miniStream, GetSectorSize(), rootStorage.Size, zeroQueue, sourceStream); BinaryReader miniFATReader = new BinaryReader(miniFATView); @@ -682,7 +682,7 @@ void CompoundFile::CommitDirectory() auto directorySectors = GetSectorChain(header.firstDirectorySectorID, SectorType::Normal); - SVector zeroQueue; + SList zeroQueue; std::shared_ptr sv( new StreamView( directorySectors, @@ -887,7 +887,7 @@ void CompoundFile::LoadDirectories() if (header.firstDirectorySectorID == Sector::ENDOFCHAIN) header.firstDirectorySectorID = directoryChain[0]->id; - SVector zeroQueue; + SList zeroQueue; StreamView dirReader(directoryChain, GetSectorSize(), directoryChain.size() * GetSectorSize(), zeroQueue, sourceStream); diff --git a/Common/cppcf/slist.h b/Common/cppcf/slist.h new file mode 100644 index 0000000000..21e3660b37 --- /dev/null +++ b/Common/cppcf/slist.h @@ -0,0 +1,30 @@ +#pragma once + +#include +#include + +template +class SList : public std::list> +{ +public: + template + SList

cast() const + { + SList

res; + for (const auto& spEl : *this) + res.push_back(std::dynamic_pointer_cast

(spEl)); + + return res; + } + + std::shared_ptr dequeue() + { + if (this->empty()) + return {}; + + auto spEl = this->front(); + this->pop_front(); + + return spEl; + } +}; diff --git a/Common/cppcf/streamview.cpp b/Common/cppcf/streamview.cpp index f93a48c27e..85fdac45ad 100644 --- a/Common/cppcf/streamview.cpp +++ b/Common/cppcf/streamview.cpp @@ -15,7 +15,7 @@ StreamView::StreamView(const SVector §orChain, int sectorSize, Strea } StreamView::StreamView(const SVector §orChain, int sectorSize, std::streamsize length, - SVector &availableSectors, Stream stream, bool isFatStream) : + SList &availableSectors, Stream stream, bool isFatStream) : StreamView(sectorChain, sectorSize, stream) { this->isFatStream = isFatStream; @@ -186,11 +186,11 @@ void StreamView::WriteInt32(int val) void StreamView::adjustLength(std::streamsize value) { - SVector q; + SList q; adjustLength(value, q); } -void StreamView::adjustLength(std::streamsize value, SVector &availableSectors) +void StreamView::adjustLength(std::streamsize value, SList &availableSectors) { this->length = value; diff --git a/Common/cppcf/streamview.h b/Common/cppcf/streamview.h index 4bcee37343..0441851fc1 100644 --- a/Common/cppcf/streamview.h +++ b/Common/cppcf/streamview.h @@ -3,7 +3,7 @@ #include #include #include "sector.h" -#include +#include "slist.h" #include "svector.h" namespace CFCPP @@ -13,7 +13,7 @@ class StreamView : public std::iostream public: StreamView(const SVector §orChain, int sectorSize, Stream stream); StreamView(const SVector §orChain, int sectorSize, std::streamsize length, - SVector &availableSectors, Stream stream, bool isFatStream = false); + SList &availableSectors, Stream stream, bool isFatStream = false); void Write(const char *buffer, std::streamsize offset, std::streamsize count); std::streamsize Read(char *buffer, std::streamsize offset, std::streamsize count); @@ -25,7 +25,7 @@ public: private: void adjustLength(std::streamsize value); - void adjustLength(std::streamsize value, SVector &availableSectors); + void adjustLength(std::streamsize value, SList &availableSectors); private: int sectorSize; std::streamsize length; @@ -36,7 +36,7 @@ private: int buf = 0; public: - std::list freeSectors; + SList freeSectors; std::streamsize position; }; } diff --git a/Common/cppcf/svector.cpp b/Common/cppcf/svector.cpp deleted file mode 100644 index ea149b2fec..0000000000 --- a/Common/cppcf/svector.cpp +++ /dev/null @@ -1,86 +0,0 @@ -#include "svector.h" - -template -SVector::SVector(size_t reserve, T initValue) : array(reserve, initValue) -{ - init(); -} - -template -SVector::SVector(bool needInit) -{ - isInit = needInit; -} - -template -SVector::SVector(size_t reserve) : array(reserve) -{ - init(); -} - -template -std::shared_ptr SVector::operator[](size_t pos) const -{ - if (!isInit || array.size() <= pos) - return {}; - - return array[pos]; -} - -template -void SVector::push_back(T &&value) -{ - isInit = true; - array.push_back(std::shared_ptr(value)); -} - -template -void SVector::push_back(std::shared_ptr pValue) -{ - isInit = true; - array.emplace_back(pValue); -} - -template -void SVector::push_back(const T &value) -{ - isInit = true; - array.push_back(std::shared_ptr(value)); -} - -template -std::shared_ptr SVector::back() const -{ - if (!isInit || array.empty()) - return {}; - - return array.back(); -} - -template -std::shared_ptr SVector::front() const -{ - if (!isInit || array.empty()) - return {}; - - return array.front(); -} - -template -std::shared_ptr SVector::dequeue() -{ - auto pElement = front(); - array.erase(array.begin()); - return pElement; -} - -template -template -SVector

SVector::cast() const -{ - SVector

res(array.size()); - for (auto& spT : array) - res.push_back(std::dynamic_pointer_cast

(spT)); - - return res; -} diff --git a/Common/cppcf/svector.h b/Common/cppcf/svector.h index fcc0fa6165..5ed0b32a93 100644 --- a/Common/cppcf/svector.h +++ b/Common/cppcf/svector.h @@ -2,43 +2,23 @@ #include #include -#include template -class SVector +class SVector : public std::vector> { public: - SVector(bool needInit = true); - SVector(size_t reserve); - SVector(size_t reserve, T initValue); - inline void init() {isInit = true;} + SVector(size_t res = 0) : std::vector>(res) + { - inline size_t size()const {return array.size();} - inline void clear() {array.empty();} - inline bool empty() {array.empty();} - std::shared_ptr operator[](size_t pos)const; - void push_back(T&& value); - void push_back(std::shared_ptr pValue); - void push_back(const T& value); - inline void pop_back() {array.pop_back();} - std::shared_ptr back()const; - std::shared_ptr front()const; - std::shared_ptr dequeue(); + } template - SVector

cast()const; - - inline void erase(typename std::vector>::iterator &beg, - typename std::vector>::iterator &end) {array.erase(beg, end);} - inline void erase(typename std::vector>::iterator &iter) {array.erase(iter);} - - inline typename std::vector>::iterator begin() const {return array.begin();} - inline typename std::vector>::iterator end() const {return array.end();} - - inline bool IsInit() const {return isInit;}; - -private: - bool isInit = false; - std::vector> array; + SVector

cast() const + { + auto sz = this->size(); + SVector

res(sz); + for (size_t i = 0; i < sz; i++) + res[i] = std::dynamic_pointer_cast

((*this)[i]); + return res; + } }; -