IndexedList.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2012 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef D_INDEXED_LIST_H
  36. #define D_INDEXED_LIST_H
  37. #include "common.h"
  38. #include <deque>
  39. #include <unordered_map>
  40. #include <vector>
  41. #include <algorithm>
  42. #include <aria2/aria2.h>
  43. namespace aria2 {
  44. template <typename SeqType, typename ValueType, typename ReferenceType,
  45. typename PointerType, typename SeqIteratorType>
  46. struct IndexedListIterator {
  47. typedef IndexedListIterator<SeqType, ValueType, ValueType&, ValueType*,
  48. typename SeqType::iterator> iterator;
  49. typedef IndexedListIterator<SeqType, ValueType, const ValueType&,
  50. const ValueType*,
  51. typename SeqType::const_iterator> const_iterator;
  52. typedef typename SeqIteratorType::iterator_category iterator_category;
  53. typedef ValueType value_type;
  54. typedef PointerType pointer;
  55. typedef ReferenceType reference;
  56. typedef typename SeqType::size_type size_type;
  57. typedef typename SeqType::difference_type difference_type;
  58. typedef IndexedListIterator SelfType;
  59. IndexedListIterator() {}
  60. IndexedListIterator(const iterator& other) : p(other.p) {}
  61. IndexedListIterator(const SeqIteratorType& p) : p(p) {}
  62. reference operator*() const { return (*p).second; }
  63. pointer operator->() const { return &(*p).second; }
  64. SelfType& operator++()
  65. {
  66. ++p;
  67. return *this;
  68. }
  69. SelfType operator++(int)
  70. {
  71. SelfType copy = *this;
  72. ++*this;
  73. return copy;
  74. }
  75. SelfType& operator--()
  76. {
  77. --p;
  78. return *this;
  79. }
  80. SelfType operator--(int)
  81. {
  82. SelfType copy = *this;
  83. --*this;
  84. return copy;
  85. }
  86. SelfType& operator+=(difference_type n)
  87. {
  88. std::advance(p, n);
  89. return *this;
  90. }
  91. SelfType operator+(difference_type n) const
  92. {
  93. SelfType copy = *this;
  94. return copy += n;
  95. }
  96. SelfType& operator-=(difference_type n)
  97. {
  98. std::advance(p, -n);
  99. return *this;
  100. }
  101. SelfType operator-(difference_type n) const
  102. {
  103. SelfType copy = *this;
  104. return copy -= n;
  105. }
  106. reference operator[](size_type n) const { return p[n].second; }
  107. SeqIteratorType p;
  108. };
  109. template <typename SeqType, typename ValueType, typename ReferenceType,
  110. typename PointerType, typename SeqIteratorType>
  111. bool operator==(const IndexedListIterator<SeqType, ValueType, ReferenceType,
  112. PointerType, SeqIteratorType>& lhs,
  113. const IndexedListIterator<SeqType, ValueType, ReferenceType,
  114. PointerType, SeqIteratorType>& rhs)
  115. {
  116. return lhs.p == rhs.p;
  117. }
  118. template <typename SeqType, typename ValueType, typename ReferenceTypeL,
  119. typename PointerTypeL, typename SeqIteratorTypeL,
  120. typename ReferenceTypeR, typename PointerTypeR,
  121. typename SeqIteratorTypeR>
  122. bool operator==(const IndexedListIterator<SeqType, ValueType, ReferenceTypeL,
  123. PointerTypeL, SeqIteratorTypeL>& lhs,
  124. const IndexedListIterator<SeqType, ValueType, ReferenceTypeR,
  125. PointerTypeR, SeqIteratorTypeR>& rhs)
  126. {
  127. return lhs.p == rhs.p;
  128. }
  129. template <typename SeqType, typename ValueType, typename ReferenceType,
  130. typename PointerType, typename SeqIteratorType>
  131. bool operator!=(const IndexedListIterator<SeqType, ValueType, ReferenceType,
  132. PointerType, SeqIteratorType>& lhs,
  133. const IndexedListIterator<SeqType, ValueType, ReferenceType,
  134. PointerType, SeqIteratorType>& rhs)
  135. {
  136. return lhs.p != rhs.p;
  137. }
  138. template <typename SeqType, typename ValueType, typename ReferenceTypeL,
  139. typename PointerTypeL, typename SeqIteratorTypeL,
  140. typename ReferenceTypeR, typename PointerTypeR,
  141. typename SeqIteratorTypeR>
  142. bool operator!=(const IndexedListIterator<SeqType, ValueType, ReferenceTypeL,
  143. PointerTypeL, SeqIteratorTypeL>& lhs,
  144. const IndexedListIterator<SeqType, ValueType, ReferenceTypeR,
  145. PointerTypeR, SeqIteratorTypeR>& rhs)
  146. {
  147. return lhs.p != rhs.p;
  148. }
  149. template <typename SeqType, typename ValueType, typename ReferenceType,
  150. typename PointerType, typename SeqIteratorType>
  151. bool operator<(const IndexedListIterator<SeqType, ValueType, ReferenceType,
  152. PointerType, SeqIteratorType>& lhs,
  153. const IndexedListIterator<SeqType, ValueType, ReferenceType,
  154. PointerType, SeqIteratorType>& rhs)
  155. {
  156. return lhs.p < rhs.p;
  157. }
  158. template <typename SeqType, typename ValueType, typename ReferenceTypeL,
  159. typename PointerTypeL, typename SeqIteratorTypeL,
  160. typename ReferenceTypeR, typename PointerTypeR,
  161. typename SeqIteratorTypeR>
  162. bool operator<(const IndexedListIterator<SeqType, ValueType, ReferenceTypeL,
  163. PointerTypeL, SeqIteratorTypeL>& lhs,
  164. const IndexedListIterator<SeqType, ValueType, ReferenceTypeR,
  165. PointerTypeR, SeqIteratorTypeR>& rhs)
  166. {
  167. return lhs.p < rhs.p;
  168. }
  169. template <typename SeqType, typename ValueType, typename ReferenceType,
  170. typename PointerType, typename SeqIteratorType>
  171. bool operator>(const IndexedListIterator<SeqType, ValueType, ReferenceType,
  172. PointerType, SeqIteratorType>& lhs,
  173. const IndexedListIterator<SeqType, ValueType, ReferenceType,
  174. PointerType, SeqIteratorType>& rhs)
  175. {
  176. return lhs.p > rhs.p;
  177. }
  178. template <typename SeqType, typename ValueType, typename ReferenceTypeL,
  179. typename PointerTypeL, typename SeqIteratorTypeL,
  180. typename ReferenceTypeR, typename PointerTypeR,
  181. typename SeqIteratorTypeR>
  182. bool operator>(const IndexedListIterator<SeqType, ValueType, ReferenceTypeL,
  183. PointerTypeL, SeqIteratorTypeL>& lhs,
  184. const IndexedListIterator<SeqType, ValueType, ReferenceTypeR,
  185. PointerTypeR, SeqIteratorTypeR>& rhs)
  186. {
  187. return lhs.p > rhs.p;
  188. }
  189. template <typename SeqType, typename ValueType, typename ReferenceType,
  190. typename PointerType, typename SeqIteratorType>
  191. bool operator<=(const IndexedListIterator<SeqType, ValueType, ReferenceType,
  192. PointerType, SeqIteratorType>& lhs,
  193. const IndexedListIterator<SeqType, ValueType, ReferenceType,
  194. PointerType, SeqIteratorType>& rhs)
  195. {
  196. return lhs.p <= rhs.p;
  197. }
  198. template <typename SeqType, typename ValueType, typename ReferenceTypeL,
  199. typename PointerTypeL, typename SeqIteratorTypeL,
  200. typename ReferenceTypeR, typename PointerTypeR,
  201. typename SeqIteratorTypeR>
  202. bool operator<=(const IndexedListIterator<SeqType, ValueType, ReferenceTypeL,
  203. PointerTypeL, SeqIteratorTypeL>& lhs,
  204. const IndexedListIterator<SeqType, ValueType, ReferenceTypeR,
  205. PointerTypeR, SeqIteratorTypeR>& rhs)
  206. {
  207. return lhs.p <= rhs.p;
  208. }
  209. template <typename SeqType, typename ValueType, typename ReferenceType,
  210. typename PointerType, typename SeqIteratorType>
  211. bool operator>=(const IndexedListIterator<SeqType, ValueType, ReferenceType,
  212. PointerType, SeqIteratorType>& lhs,
  213. const IndexedListIterator<SeqType, ValueType, ReferenceType,
  214. PointerType, SeqIteratorType>& rhs)
  215. {
  216. return lhs.p >= rhs.p;
  217. }
  218. template <typename SeqType, typename ValueType, typename ReferenceTypeL,
  219. typename PointerTypeL, typename SeqIteratorTypeL,
  220. typename ReferenceTypeR, typename PointerTypeR,
  221. typename SeqIteratorTypeR>
  222. bool operator>=(const IndexedListIterator<SeqType, ValueType, ReferenceTypeL,
  223. PointerTypeL, SeqIteratorTypeL>& lhs,
  224. const IndexedListIterator<SeqType, ValueType, ReferenceTypeR,
  225. PointerTypeR, SeqIteratorTypeR>& rhs)
  226. {
  227. return lhs.p >= rhs.p;
  228. }
  229. template <typename SeqType, typename ValueType, typename ReferenceType,
  230. typename PointerType, typename SeqIteratorType>
  231. IndexedListIterator<SeqType, ValueType, ReferenceType, PointerType,
  232. SeqIteratorType>
  233. operator+(
  234. typename IndexedListIterator<SeqType, ValueType, ReferenceType, PointerType,
  235. SeqIteratorType>::difference_type n,
  236. const IndexedListIterator<SeqType, ValueType, ReferenceType, PointerType,
  237. SeqIteratorType>& lhs)
  238. {
  239. return lhs + n;
  240. }
  241. template <typename SeqType, typename ValueType, typename ReferenceType,
  242. typename PointerType, typename SeqIteratorType>
  243. typename IndexedListIterator<SeqType, ValueType, ReferenceType, PointerType,
  244. SeqIteratorType>::difference_type
  245. operator-(const IndexedListIterator<SeqType, ValueType, ReferenceType,
  246. PointerType, SeqIteratorType>& lhs,
  247. const IndexedListIterator<SeqType, ValueType, ReferenceType,
  248. PointerType, SeqIteratorType>& rhs)
  249. {
  250. return typename IndexedListIterator<SeqType, ValueType, ReferenceType,
  251. PointerType,
  252. SeqIteratorType>::difference_type(lhs.p -
  253. rhs.p);
  254. }
  255. template <typename SeqType, typename ValueType, typename ReferenceTypeL,
  256. typename PointerTypeL, typename SeqIteratorTypeL,
  257. typename ReferenceTypeR, typename PointerTypeR,
  258. typename SeqIteratorTypeR>
  259. typename IndexedListIterator<SeqType, ValueType, ReferenceTypeL, PointerTypeL,
  260. SeqIteratorTypeL>::difference_type
  261. operator-(const IndexedListIterator<SeqType, ValueType, ReferenceTypeL,
  262. PointerTypeL, SeqIteratorTypeL>& lhs,
  263. const IndexedListIterator<SeqType, ValueType, ReferenceTypeR,
  264. PointerTypeR, SeqIteratorTypeR>& rhs)
  265. {
  266. return typename IndexedListIterator<SeqType, ValueType, ReferenceTypeL,
  267. PointerTypeL,
  268. SeqIteratorTypeL>::difference_type(lhs.p -
  269. rhs.p);
  270. }
  271. template <typename KeyType, typename ValuePtrType> class IndexedList {
  272. public:
  273. IndexedList() {}
  274. ~IndexedList() {}
  275. typedef KeyType key_type;
  276. typedef ValuePtrType value_type;
  277. typedef std::unordered_map<KeyType, ValuePtrType> IndexType;
  278. typedef std::deque<std::pair<KeyType, ValuePtrType>> SeqType;
  279. typedef IndexedListIterator<SeqType, ValuePtrType, ValuePtrType&,
  280. ValuePtrType*,
  281. typename SeqType::iterator> iterator;
  282. typedef IndexedListIterator<SeqType, ValuePtrType, const ValuePtrType&,
  283. const ValuePtrType*,
  284. typename SeqType::const_iterator> const_iterator;
  285. ValuePtrType& operator[](size_t n) { return seq_[n].second; }
  286. const ValuePtrType& operator[](size_t n) const { return seq_[n].second; }
  287. // Inserts (|key|, |value|) to the end of the list. If the same key
  288. // has been already added, this function fails. This function
  289. // returns true if it succeeds. Complexity: O(1)
  290. bool push_back(KeyType key, ValuePtrType value)
  291. {
  292. auto i = index_.find(key);
  293. if (i == std::end(index_)) {
  294. index_.insert({key, value});
  295. seq_.emplace_back(key, value);
  296. return true;
  297. }
  298. else {
  299. return false;
  300. }
  301. }
  302. // Inserts (|key|, |value|) to the front of the list. If the same
  303. // key has been already added, this function fails. This function
  304. // returns true if it succeeds. Complexity: O(1)
  305. bool push_front(KeyType key, ValuePtrType value)
  306. {
  307. auto i = index_.find(key);
  308. if (i == std::end(index_)) {
  309. index_.insert({key, value});
  310. seq_.emplace_front(key, value);
  311. return true;
  312. }
  313. else {
  314. return false;
  315. }
  316. }
  317. // Inserts (|key|, |value|) to the position |dest|. If the same key
  318. // has been already added, this function fails. This function
  319. // returns the iterator to the newly added element if it is
  320. // succeeds, or end(). Complexity: O(N)
  321. iterator insert(size_t dest, KeyType key, ValuePtrType value)
  322. {
  323. if (dest > size()) {
  324. return std::end(seq_);
  325. }
  326. auto i = index_.find(key);
  327. if (i == std::end(index_)) {
  328. auto j = std::begin(seq_);
  329. std::advance(j, dest);
  330. index_.insert({key, value});
  331. return iterator(seq_.insert(j, {key, value}));
  332. }
  333. else {
  334. return iterator(std::end(seq_));
  335. }
  336. }
  337. // Inserts (|key|, |value|) to the position |dest|. If the same key
  338. // has been already added, this function fails. This function
  339. // returns the iterator to the newly added element if it is
  340. // succeeds, or end(). Complexity: O(1) if inserted to the first or
  341. // last, otherwise O(N)
  342. iterator insert(iterator dest, KeyType key, ValuePtrType value)
  343. {
  344. auto i = index_.find(key);
  345. if (i == std::end(index_)) {
  346. index_.insert({key, value});
  347. return iterator(seq_.insert(dest.p, {key, value}));
  348. }
  349. else {
  350. return iterator(std::end(seq_));
  351. }
  352. }
  353. // Inserts values in iterator range [first, last). The key for each
  354. // value is retrieved by functor |keyFunc|. The insertion position
  355. // is given by |dest|.
  356. template <typename KeyFunc, typename InputIterator>
  357. void insert(iterator dest, KeyFunc keyFunc, InputIterator first,
  358. InputIterator last)
  359. {
  360. std::vector<typename SeqType::value_type> v;
  361. v.reserve(std::distance(first, last));
  362. for (; first != last; ++first) {
  363. auto key = keyFunc(*first);
  364. auto i = index_.find(key);
  365. if (i == std::end(index_)) {
  366. index_.insert({key, *first});
  367. v.emplace_back(key, *first);
  368. }
  369. }
  370. seq_.insert(dest.p, std::begin(v), std::end(v));
  371. }
  372. template <typename KeyFunc, typename InputIterator>
  373. void insert(size_t pos, KeyFunc keyFunc, InputIterator first,
  374. InputIterator last)
  375. {
  376. if (pos > size()) {
  377. return;
  378. }
  379. std::vector<typename SeqType::value_type> v;
  380. v.reserve(std::distance(first, last));
  381. for (; first != last; ++first) {
  382. auto key = keyFunc(*first);
  383. auto i = index_.find(key);
  384. if (i == std::end(index_)) {
  385. index_.insert({key, *first});
  386. v.emplace_back(key, *first);
  387. }
  388. }
  389. seq_.insert(std::begin(seq_) + pos, std::begin(v), std::end(v));
  390. }
  391. // Removes |key| from the list. If the element is not found, this
  392. // function fails. This function returns true if it
  393. // succeeds. Complexity: O(N)
  394. bool remove(KeyType key)
  395. {
  396. auto i = index_.find(key);
  397. if (i == std::end(index_)) {
  398. return false;
  399. }
  400. for (auto j = std::begin(seq_), eoj = std::end(seq_); j != eoj; ++j) {
  401. if ((*j).first == key) {
  402. seq_.erase(j);
  403. break;
  404. }
  405. }
  406. index_.erase(i);
  407. return true;
  408. }
  409. // Removes element pointed by iterator |k| from the list. If the
  410. // iterator must be valid. This function returns the iterator
  411. // pointing to the element following the erased element. Complexity:
  412. // O(N)
  413. iterator erase(iterator k)
  414. {
  415. index_.erase((*k.p).first);
  416. return iterator(seq_.erase(k.p));
  417. }
  418. // Removes elements for which Pred returns true. The pred is called
  419. // against each each element once per each.
  420. template <typename Pred> void remove_if(Pred pred)
  421. {
  422. auto first = std::begin(seq_), last = std::end(seq_);
  423. for (; first != last && !pred((*first).second); ++first)
  424. ;
  425. if (first == last) {
  426. return;
  427. }
  428. index_.erase((*first).first);
  429. auto store = first;
  430. ++first;
  431. for (; first != last; ++first) {
  432. if (pred((*first).second)) {
  433. index_.erase((*first).first);
  434. }
  435. else {
  436. *store++ = *first;
  437. }
  438. }
  439. seq_.erase(store, last);
  440. }
  441. // Removes element at the front of the list. If the list is empty,
  442. // this function fails. This function returns true if it
  443. // succeeds. Complexity: O(1)
  444. bool pop_front()
  445. {
  446. if (seq_.empty()) {
  447. return false;
  448. }
  449. index_.erase(seq_.front().first);
  450. seq_.pop_front();
  451. return true;
  452. }
  453. // Moves element with |key| to the specified position. If |how| is
  454. // OFFSET_MODE_CUR, the element is moved to the position |offset|
  455. // relative to the current position. If |how| is OFFSET_MODE_SET,
  456. // the element is moved to the position |offset|. If |how| is
  457. // OFFSET_MODE_END, the element is moved to the position |offset|
  458. // relative to the end of the list. This function returns the
  459. // position the element is moved to if it succeeds, or -1 if no
  460. // element with |key| is found or |how| is invalid. Complexity:
  461. // O(N)
  462. ssize_t move(KeyType key, ssize_t offset, OffsetMode how)
  463. {
  464. auto idxent = index_.find(key);
  465. if (idxent == std::end(index_)) {
  466. return -1;
  467. }
  468. auto x = std::begin(seq_), eseq = std::end(seq_);
  469. for (; x != eseq; ++x) {
  470. if ((*x).first == (*idxent).first) {
  471. break;
  472. }
  473. }
  474. ssize_t xp = std::distance(std::begin(seq_), x);
  475. ssize_t size = index_.size();
  476. ssize_t dest;
  477. if (how == OFFSET_MODE_CUR) {
  478. if (offset > 0) {
  479. dest = std::min(xp + offset, static_cast<ssize_t>(size - 1));
  480. }
  481. else {
  482. dest = std::max(xp + offset, static_cast<ssize_t>(0));
  483. }
  484. }
  485. else {
  486. if (how == OFFSET_MODE_END) {
  487. dest = std::min(size - 1 + offset, size - 1);
  488. }
  489. else if (how == OFFSET_MODE_SET) {
  490. dest = std::min(offset, size - 1);
  491. }
  492. else {
  493. return -1;
  494. }
  495. dest = std::max(dest, static_cast<ssize_t>(0));
  496. }
  497. auto d = std::begin(seq_);
  498. std::advance(d, dest);
  499. if (xp < dest) {
  500. std::rotate(x, x + 1, d + 1);
  501. }
  502. else {
  503. std::rotate(d, x, x + 1);
  504. }
  505. return dest;
  506. }
  507. // Returns the value associated by |key|. If it is not found,
  508. // returns ValuePtrType(). Complexity: O(1)
  509. ValuePtrType get(KeyType key) const
  510. {
  511. auto idxent = index_.find(key);
  512. if (idxent == std::end(index_)) {
  513. return ValuePtrType();
  514. }
  515. else {
  516. return (*idxent).second;
  517. }
  518. }
  519. size_t size() const { return index_.size(); }
  520. size_t empty() const { return index_.empty(); }
  521. iterator begin() { return iterator(std::begin(seq_)); }
  522. iterator end() { return iterator(std::end(seq_)); }
  523. const_iterator begin() const { return const_iterator(std::begin(seq_)); }
  524. const_iterator end() const { return const_iterator(std::end(seq_)); }
  525. // Removes all elements from the list.
  526. void clear()
  527. {
  528. index_.clear();
  529. seq_.clear();
  530. }
  531. private:
  532. SeqType seq_;
  533. IndexType index_;
  534. };
  535. } // namespace aria2
  536. #endif // D_INDEXED_LIST_H