2016-02-09 20:35:13 +00:00
|
|
|
/* Copyright 2015, 2016, Michele Santullo
|
|
|
|
* This file is part of "dindexer".
|
|
|
|
*
|
|
|
|
* "dindexer" is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* "dindexer" is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-02-12 20:02:26 +00:00
|
|
|
#ifndef id040FEEC20F7B4F65A3EF67BA6460E737
|
|
|
|
#define id040FEEC20F7B4F65A3EF67BA6460E737
|
|
|
|
|
2016-02-09 20:35:13 +00:00
|
|
|
#include "dindexer-machinery/recorddata.hpp"
|
2016-02-18 20:00:17 +00:00
|
|
|
#include "kakoune/safe_ptr.hh"
|
2016-02-09 20:35:13 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <boost/iterator/iterator_facade.hpp>
|
|
|
|
#include <memory>
|
2016-02-12 20:02:26 +00:00
|
|
|
#include <type_traits>
|
2016-02-16 17:51:17 +00:00
|
|
|
#include <ciso646>
|
2016-02-23 17:56:07 +00:00
|
|
|
#include <cstdint>
|
2016-02-09 20:35:13 +00:00
|
|
|
|
|
|
|
namespace mchlib {
|
2016-02-16 17:51:17 +00:00
|
|
|
namespace implem {
|
|
|
|
template <bool Const> class DirIterator;
|
|
|
|
} //namespace implem
|
|
|
|
|
2016-02-09 20:35:13 +00:00
|
|
|
class PathName;
|
2016-02-16 17:51:17 +00:00
|
|
|
template <bool Const> class SetListingView;
|
2016-02-09 20:35:13 +00:00
|
|
|
|
2016-02-16 19:29:50 +00:00
|
|
|
template <bool Const>
|
|
|
|
implem::DirIterator<Const> first_file ( const SetListingView<Const>& parList );
|
|
|
|
template <bool Const>
|
|
|
|
implem::DirIterator<Const> first_file ( SetListingView<Const>& parList );
|
|
|
|
|
2016-03-08 07:48:12 +00:00
|
|
|
typedef FileRecordData SetListingItemType;
|
|
|
|
|
2016-02-09 20:35:13 +00:00
|
|
|
namespace implem {
|
2016-02-12 20:02:26 +00:00
|
|
|
template <bool Const>
|
2016-03-08 07:48:12 +00:00
|
|
|
class DirIterator : public boost::iterator_facade<DirIterator<Const>, SetListingItemType, boost::forward_traversal_tag> {
|
2016-02-16 17:51:17 +00:00
|
|
|
friend class mchlib::SetListingView<Const>;
|
2016-02-09 20:35:13 +00:00
|
|
|
friend class boost::iterator_core_access;
|
2016-02-16 17:12:26 +00:00
|
|
|
template <bool> friend class DirIterator;
|
2016-03-08 07:48:12 +00:00
|
|
|
typedef boost::iterator_facade<DirIterator<Const>, SetListingItemType, boost::forward_traversal_tag> base_class;
|
2016-02-16 17:12:26 +00:00
|
|
|
struct enabler {};
|
2016-02-09 20:35:13 +00:00
|
|
|
public:
|
2016-02-12 20:02:26 +00:00
|
|
|
typedef typename std::conditional<
|
|
|
|
Const,
|
2016-03-08 07:48:12 +00:00
|
|
|
std::vector<SetListingItemType>::const_iterator,
|
|
|
|
std::vector<SetListingItemType>::iterator
|
2016-02-12 20:02:26 +00:00
|
|
|
>::type VecIterator;
|
2016-02-19 08:46:55 +00:00
|
|
|
typedef typename base_class::difference_type difference_type;
|
|
|
|
typedef typename base_class::value_type value_type;
|
|
|
|
typedef typename base_class::pointer pointer;
|
|
|
|
typedef typename base_class::reference reference;
|
|
|
|
typedef typename base_class::iterator_category iterator_category;
|
2016-02-09 20:35:13 +00:00
|
|
|
|
2016-02-19 08:46:55 +00:00
|
|
|
DirIterator ( DirIterator&& parOther );
|
|
|
|
DirIterator ( const DirIterator& parOther );
|
2016-02-16 17:12:26 +00:00
|
|
|
template <bool OtherConst>
|
|
|
|
DirIterator ( DirIterator<OtherConst>&& parOther, typename std::enable_if<std::is_convertible<typename DirIterator<OtherConst>::VecIterator, VecIterator>::value, enabler>::type = enabler() );
|
2016-02-18 20:00:17 +00:00
|
|
|
DirIterator ( VecIterator parBegin, VecIterator parEnd, const PathName* parBasePath, std::size_t parLevelOffset );
|
2016-02-09 20:35:13 +00:00
|
|
|
~DirIterator ( void ) noexcept;
|
|
|
|
|
2016-02-19 20:56:10 +00:00
|
|
|
DirIterator& operator= ( DirIterator&& parOther );
|
|
|
|
DirIterator& operator= ( const DirIterator& parOther );
|
|
|
|
template <bool OtherConst>
|
|
|
|
DirIterator& operator= ( typename std::enable_if<std::is_convertible<typename DirIterator<OtherConst>::VecIterator, VecIterator>::value, DirIterator<OtherConst>>::type&& parOther );
|
|
|
|
template <bool OtherConst>
|
|
|
|
DirIterator& operator= ( const typename std::enable_if<std::is_convertible<typename DirIterator<OtherConst>::VecIterator, VecIterator>::value, DirIterator<OtherConst>>::type& parOther );
|
|
|
|
|
2016-02-09 20:35:13 +00:00
|
|
|
private:
|
|
|
|
void increment ( void );
|
2016-02-12 20:02:26 +00:00
|
|
|
difference_type distance_to ( const DirIterator<Const>& parOther ) const;
|
2016-02-16 17:12:26 +00:00
|
|
|
template <bool OtherConst>
|
|
|
|
bool equal ( const DirIterator<OtherConst>& parOther ) const;
|
|
|
|
|
2016-02-09 20:35:13 +00:00
|
|
|
reference dereference ( void ) const;
|
|
|
|
bool is_end ( void ) const;
|
|
|
|
|
|
|
|
VecIterator m_current;
|
|
|
|
VecIterator m_end;
|
2016-02-18 20:00:17 +00:00
|
|
|
Kakoune::SafePtr<const PathName> m_base_path;
|
2016-02-16 20:18:10 +00:00
|
|
|
std::size_t m_level_offset;
|
2016-02-09 20:35:13 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-02-16 17:51:17 +00:00
|
|
|
template <bool Const>
|
2016-02-12 19:14:12 +00:00
|
|
|
class SetListingView {
|
2016-02-16 17:51:17 +00:00
|
|
|
struct enabler {};
|
2016-02-09 20:35:13 +00:00
|
|
|
public:
|
2016-02-12 20:02:26 +00:00
|
|
|
typedef implem::DirIterator<true> const_iterator;
|
2016-02-16 17:51:17 +00:00
|
|
|
typedef implem::DirIterator<false> iterator;
|
|
|
|
typedef typename implem::DirIterator<Const>::VecIterator list_iterator;
|
2016-02-09 20:35:13 +00:00
|
|
|
|
2016-02-16 17:51:17 +00:00
|
|
|
explicit SetListingView ( const implem::DirIterator<Const>& parIter );
|
2016-02-16 20:18:10 +00:00
|
|
|
SetListingView ( list_iterator parBeg, list_iterator parEnd, std::size_t parLevelOffset );
|
2016-02-18 20:00:17 +00:00
|
|
|
SetListingView ( list_iterator parBeg, list_iterator parEnd, std::size_t parLevelOffset, const std::shared_ptr<PathName>& parBasePath );
|
|
|
|
template <bool B=not Const, typename Other=typename std::enable_if<B, SetListingView<not B>>::type>
|
|
|
|
SetListingView ( const Other& parOther );
|
2016-02-12 19:14:12 +00:00
|
|
|
SetListingView ( SetListingView&& ) = default;
|
|
|
|
~SetListingView ( void ) noexcept = default;
|
2016-02-09 20:35:13 +00:00
|
|
|
|
|
|
|
const_iterator begin ( void ) const;
|
|
|
|
const_iterator cbegin ( void ) const;
|
|
|
|
const_iterator end ( void ) const;
|
|
|
|
const_iterator cend ( void ) const;
|
2016-02-16 17:51:17 +00:00
|
|
|
template <bool B=not Const, typename R=typename std::enable_if<B, iterator>::type>
|
|
|
|
R begin ( void );
|
|
|
|
template <bool B=not Const, typename R=typename std::enable_if<B, iterator>::type>
|
|
|
|
R end ( void );
|
2016-02-09 20:35:13 +00:00
|
|
|
|
|
|
|
private:
|
2016-02-12 19:14:12 +00:00
|
|
|
list_iterator m_begin;
|
|
|
|
list_iterator m_end;
|
2016-02-18 20:00:17 +00:00
|
|
|
std::shared_ptr<PathName> m_base_path;
|
2016-02-16 20:18:10 +00:00
|
|
|
std::size_t m_level_offset;
|
2016-02-09 20:35:13 +00:00
|
|
|
};
|
2016-02-09 22:23:50 +00:00
|
|
|
|
2016-02-17 07:54:38 +00:00
|
|
|
using MutableSetListingView = SetListingView<false>;
|
|
|
|
using ConstSetListingView = SetListingView<true>;
|
|
|
|
|
2016-02-12 19:14:12 +00:00
|
|
|
class SetListing {
|
2016-02-09 22:23:50 +00:00
|
|
|
public:
|
2016-03-08 07:48:12 +00:00
|
|
|
typedef std::vector<SetListingItemType> ListType;
|
2016-02-16 17:51:17 +00:00
|
|
|
typedef implem::DirIterator<true> const_iterator;
|
2016-02-09 22:23:50 +00:00
|
|
|
|
2016-02-12 19:14:12 +00:00
|
|
|
explicit SetListing ( ListType&& parList, bool parSort=true );
|
|
|
|
~SetListing ( void ) noexcept;
|
2016-02-09 22:23:50 +00:00
|
|
|
|
|
|
|
const_iterator begin ( void ) const;
|
|
|
|
const_iterator cbegin ( void ) const;
|
|
|
|
const_iterator end ( void ) const;
|
|
|
|
const_iterator cend ( void ) const;
|
|
|
|
|
2016-02-12 19:14:12 +00:00
|
|
|
//ListType descend_copy ( const const_iterator& parItem ) const;
|
2016-02-16 17:51:17 +00:00
|
|
|
SetListingView<false> make_view ( void );
|
|
|
|
SetListingView<true> make_view ( void ) const;
|
|
|
|
SetListingView<true> make_cview ( void ) const;
|
2016-02-12 19:14:12 +00:00
|
|
|
|
2016-02-19 08:46:55 +00:00
|
|
|
bool empty ( void ) const;
|
|
|
|
std::size_t size ( void ) const;
|
|
|
|
std::size_t files_count ( void ) const;
|
|
|
|
std::size_t dir_count ( void ) const;
|
2016-02-23 17:56:07 +00:00
|
|
|
const ListType& sorted_list ( void ) const;
|
|
|
|
|
|
|
|
static void sort_list ( ListType& parList );
|
|
|
|
static ListType::iterator lower_bound ( ListType& parList, const char* parPath, uint16_t parLevel, bool parIsDir );
|
2016-02-19 08:46:55 +00:00
|
|
|
|
2016-02-09 22:23:50 +00:00
|
|
|
private:
|
2016-02-12 19:14:12 +00:00
|
|
|
ListType m_list;
|
2016-02-18 20:00:17 +00:00
|
|
|
std::shared_ptr<PathName> m_base_path;
|
2016-02-09 22:23:50 +00:00
|
|
|
};
|
2016-02-16 17:51:17 +00:00
|
|
|
|
2016-02-16 19:29:50 +00:00
|
|
|
template <bool Const>
|
|
|
|
inline implem::DirIterator<Const> first_file (const SetListingView<Const>& parList) {
|
|
|
|
auto end = parList.end();
|
|
|
|
for (auto it = parList.begin(); it != end; ++it) {
|
|
|
|
if (not it->is_directory)
|
2016-03-18 21:28:58 +00:00
|
|
|
return it;
|
2016-02-16 19:29:50 +00:00
|
|
|
}
|
|
|
|
return parList.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool Const>
|
|
|
|
inline implem::DirIterator<Const> first_file (SetListingView<Const>& parList) {
|
|
|
|
auto end = parList.end();
|
|
|
|
for (auto it = parList.begin(); it != end; ++it) {
|
|
|
|
if (not it->is_directory)
|
2016-03-18 21:28:58 +00:00
|
|
|
return it;
|
2016-02-16 19:29:50 +00:00
|
|
|
}
|
|
|
|
return parList.end();
|
|
|
|
}
|
2016-02-09 20:35:13 +00:00
|
|
|
} //namespace mchlib
|
|
|
|
|
|
|
|
#endif
|