Mantid
Loading...
Searching...
No Matches
MRUList.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2008 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
8
9//----------------------------------------------------------------------
10// Includes
11//----------------------------------------------------------------------
12#include "MantidKernel/DllConfig.h"
13#include <mutex>
14
15#ifndef Q_MOC_RUN
16#include <boost/multi_index/hashed_index.hpp>
17#include <boost/multi_index/mem_fun.hpp>
18#include <boost/multi_index/sequenced_index.hpp>
19#include <boost/multi_index_container.hpp>
20#endif
21
22namespace Mantid {
23namespace Kernel {
32template <class T> class DLLExport MRUList {
33private:
35 using item_list = typename boost::multi_index::multi_index_container<
36 std::shared_ptr<T>,
37 boost::multi_index::indexed_by<boost::multi_index::sequenced<>,
38 boost::multi_index::hashed_unique<::boost::multi_index::const_mem_fun<
39 T, std::uintptr_t, &T::hashIndexFunction>>>>;
40
42 using ordered_item_list = typename boost::multi_index::nth_index<item_list, 1>::type;
43
45 mutable item_list il;
47 const std::size_t max_num_items;
48
49public:
50 //---------------------------------------------------------------------------------------------
54 MRUList(const std::size_t &max_num_items_) : max_num_items(max_num_items_) {}
55
56 //---------------------------------------------------------------------------------------------
59 MRUList() : max_num_items(100) {}
60
61 //---------------------------------------------------------------------------------------------
64 ~MRUList() { this->clear(); }
65
66 //---------------------------------------------------------------------------------------------
78 std::shared_ptr<T> insert(std::shared_ptr<T> item) {
79 std::lock_guard<std::mutex> _lock(m_mutex);
80 auto p = this->il.push_front(std::move(item));
81
82 if (!p.second) {
83 /* duplicate item */
84 this->il.relocate(this->il.begin(), p.first); /* put in front */
85 return nullptr;
86 }
87
88 bool exceeding_size;
89 exceeding_size = this->il.size() > max_num_items;
90
91 if (exceeding_size) {
92 std::shared_ptr<T> toWrite;
93 /* keep the length <= max_num_items */
94 // This is dropping an item - you may need to write it to disk (if it's
95 // changed) and delete
96 // but this is left up to the calling class to do,
97 // by returning the to-be-dropped item pointer.
98 toWrite = std::move(this->il.back());
99 this->il.pop_back();
100 return toWrite;
101 }
102 return nullptr;
103 }
104
105 //---------------------------------------------------------------------------------------------
107 void clear() {
108 std::lock_guard<std::mutex> _lock(m_mutex);
109 this->il.clear();
110 }
111
112 //---------------------------------------------------------------------------------------------
117 void deleteIndex(const uintptr_t index) {
118 std::lock_guard<std::mutex> _lock(m_mutex);
119
120 auto it = il.template get<1>().find(index);
121 if (it != il.template get<1>().end()) {
122 il.template get<1>().erase(it);
123 }
124 }
125
126 //---------------------------------------------------------------------------------------------
128 size_t size() const { return il.size(); }
129
130 //---------------------------------------------------------------------------------------------
135 T *find(const uintptr_t index) const {
136 std::lock_guard<std::mutex> _lock(m_mutex);
137
138 auto it = il.template get<1>().find(index);
139 if (it == il.template get<1>().end()) {
140 return nullptr;
141 } else {
142 return it->get();
143 }
144 }
145
146private:
151
153 mutable std::mutex m_mutex;
154};
155
156} // namespace Kernel
157} // namespace Mantid
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
#define DLLExport
Definitions of the DLLImport compiler directives for MSVC.
Definition: System.h:53
An MRU (most recently used) list keeps record of the last n inserted items, listing first the newer o...
Definition: MRUList.h:32
MRUList & operator=(MRUList &)
Private, unimplemented copy assignment operator.
MRUList()
Constructor.
Definition: MRUList.h:59
size_t size() const
Size of the list.
Definition: MRUList.h:128
std::mutex m_mutex
Mutex for modifying the MRU list.
Definition: MRUList.h:153
void clear()
Delete all the T's pointed to by the list, and empty the list itself.
Definition: MRUList.h:107
const std::size_t max_num_items
The length of the list.
Definition: MRUList.h:47
void deleteIndex(const uintptr_t index)
Delete the T at the given index.
Definition: MRUList.h:117
T * find(const uintptr_t index) const
Find an element of the list from the key of the index.
Definition: MRUList.h:135
MRUList(const std::size_t &max_num_items_)
Constructor.
Definition: MRUList.h:54
item_list il
The most recently used list.
Definition: MRUList.h:45
MRUList(MRUList &)
Private, unimplemented copy constructor.
std::shared_ptr< T > insert(std::shared_ptr< T > item)
Insert an item into the list.
Definition: MRUList.h:78
typename boost::multi_index::nth_index< item_list, 1 >::type ordered_item_list
This typedef makes an ordered item list (you access it by the 1st index)
Definition: MRUList.h:42
~MRUList()
Destructor.
Definition: MRUList.h:64
typename boost::multi_index::multi_index_container< std::shared_ptr< T >, boost::multi_index::indexed_by< boost::multi_index::sequenced<>, boost::multi_index::hashed_unique<::boost::multi_index::const_mem_fun< T, std::uintptr_t, &T::hashIndexFunction > > > > item_list
hideous typedef for the container holding the list
Definition: MRUList.h:39
Helper class which provides the Collimation Length for SANS instruments.