IndexedList.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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,
  48. ValueType,
  49. ValueType&,
  50. ValueType*,
  51. typename SeqType::iterator> iterator;
  52. typedef IndexedListIterator<SeqType,
  53. ValueType,
  54. const ValueType&,
  55. const ValueType*,
  56. typename SeqType::const_iterator> const_iterator;
  57. typedef typename SeqIteratorType::iterator_category iterator_category;
  58. typedef ValueType value_type;
  59. typedef PointerType pointer;
  60. typedef ReferenceType reference;
  61. typedef typename SeqType::size_type size_type;
  62. typedef typename SeqType::difference_type difference_type;
  63. typedef IndexedListIterator SelfType;
  64. IndexedListIterator() {}
  65. IndexedListIterator(const iterator& other)
  66. : p(other.p) {}
  67. IndexedListIterator(const SeqIteratorType& p)
  68. : p(p) {}
  69. reference operator*() const
  70. {
  71. return (*p).second;
  72. }
  73. pointer operator->() const
  74. {
  75. return &(*p).second;
  76. }
  77. SelfType& operator++()
  78. {
  79. ++p;
  80. return *this;
  81. }
  82. SelfType operator++(int)
  83. {
  84. SelfType copy = *this;
  85. ++*this;
  86. return copy;
  87. }
  88. SelfType& operator--()
  89. {
  90. --p;
  91. return *this;
  92. }
  93. SelfType operator--(int)
  94. {
  95. SelfType copy = *this;
  96. --*this;
  97. return copy;
  98. }
  99. SelfType& operator+=(difference_type n)
  100. {
  101. std::advance(p, n);
  102. return *this;
  103. }
  104. SelfType operator+(difference_type n) const
  105. {
  106. SelfType copy = *this;
  107. return copy += n;
  108. }
  109. SelfType& operator-=(difference_type n)
  110. {
  111. std::advance(p, -n);
  112. return *this;
  113. }
  114. SelfType operator-(difference_type n) const
  115. {
  116. SelfType copy = *this;
  117. return copy -= n;
  118. }
  119. reference operator[](size_type n) const
  120. {
  121. return p[n].second;
  122. }
  123. SeqIteratorType p;
  124. };
  125. template<typename SeqType, typename ValueType, typename ReferenceType,
  126. typename PointerType, typename SeqIteratorType>
  127. bool operator==(const IndexedListIterator<SeqType, ValueType, ReferenceType,
  128. PointerType, SeqIteratorType>& lhs,
  129. const IndexedListIterator<SeqType, ValueType, ReferenceType,
  130. PointerType, SeqIteratorType>& rhs)
  131. {
  132. return lhs.p == rhs.p;
  133. }
  134. template<typename SeqType, typename ValueType,
  135. typename ReferenceTypeL, typename PointerTypeL,
  136. typename SeqIteratorTypeL,
  137. typename ReferenceTypeR, typename PointerTypeR,
  138. typename SeqIteratorTypeR>
  139. bool operator==(const IndexedListIterator<SeqType, ValueType, ReferenceTypeL,
  140. PointerTypeL, SeqIteratorTypeL>& lhs,
  141. const IndexedListIterator<SeqType, ValueType, ReferenceTypeR,
  142. PointerTypeR, SeqIteratorTypeR>& rhs)
  143. {
  144. return lhs.p == rhs.p;
  145. }
  146. template<typename SeqType, typename ValueType, typename ReferenceType,
  147. typename PointerType, typename SeqIteratorType>
  148. bool operator!=(const IndexedListIterator<SeqType, ValueType, ReferenceType,
  149. PointerType, SeqIteratorType>& lhs,
  150. const IndexedListIterator<SeqType, ValueType, ReferenceType,
  151. PointerType, SeqIteratorType>& rhs)
  152. {
  153. return lhs.p != rhs.p;
  154. }
  155. template<typename SeqType, typename ValueType,
  156. typename ReferenceTypeL, typename PointerTypeL,
  157. typename SeqIteratorTypeL,
  158. typename ReferenceTypeR, typename PointerTypeR,
  159. typename SeqIteratorTypeR>
  160. bool operator!=(const IndexedListIterator<SeqType, ValueType, ReferenceTypeL,
  161. PointerTypeL, SeqIteratorTypeL>& lhs,
  162. const IndexedListIterator<SeqType, ValueType, ReferenceTypeR,
  163. PointerTypeR, SeqIteratorTypeR>& rhs)
  164. {
  165. return lhs.p != rhs.p;
  166. }
  167. template<typename SeqType, typename ValueType, typename ReferenceType,
  168. typename PointerType, typename SeqIteratorType>
  169. bool operator<(const IndexedListIterator<SeqType, ValueType, ReferenceType,
  170. PointerType, SeqIteratorType>& lhs,
  171. const IndexedListIterator<SeqType, ValueType, ReferenceType,
  172. PointerType, SeqIteratorType>& rhs)
  173. {
  174. return lhs.p < rhs.p;
  175. }
  176. template<typename SeqType, typename ValueType,
  177. typename ReferenceTypeL, typename PointerTypeL,
  178. typename SeqIteratorTypeL,
  179. typename ReferenceTypeR, typename PointerTypeR,
  180. typename SeqIteratorTypeR>
  181. bool operator<(const IndexedListIterator<SeqType, ValueType, ReferenceTypeL,
  182. PointerTypeL, SeqIteratorTypeL>& lhs,
  183. const IndexedListIterator<SeqType, ValueType, ReferenceTypeR,
  184. PointerTypeR, SeqIteratorTypeR>& rhs)
  185. {
  186. return lhs.p < rhs.p;
  187. }
  188. template<typename SeqType, typename ValueType, typename ReferenceType,
  189. typename PointerType, typename SeqIteratorType>
  190. bool operator>(const IndexedListIterator<SeqType, ValueType, ReferenceType,
  191. PointerType, SeqIteratorType>& lhs,
  192. const IndexedListIterator<SeqType, ValueType, ReferenceType,
  193. PointerType, SeqIteratorType>& rhs)
  194. {
  195. return lhs.p > rhs.p;
  196. }
  197. template<typename SeqType, typename ValueType,
  198. typename ReferenceTypeL, typename PointerTypeL,
  199. 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,
  219. typename ReferenceTypeL, typename PointerTypeL,
  220. typename SeqIteratorTypeL,
  221. typename ReferenceTypeR, typename PointerTypeR,
  222. typename SeqIteratorTypeR>
  223. bool operator<=(const IndexedListIterator<SeqType, ValueType, ReferenceTypeL,
  224. PointerTypeL, SeqIteratorTypeL>& lhs,
  225. const IndexedListIterator<SeqType, ValueType, ReferenceTypeR,
  226. PointerTypeR, SeqIteratorTypeR>& rhs)
  227. {
  228. return lhs.p <= rhs.p;
  229. }
  230. template<typename SeqType, typename ValueType, typename ReferenceType,
  231. typename PointerType, typename SeqIteratorType>
  232. bool operator>=(const IndexedListIterator<SeqType, ValueType, ReferenceType,
  233. PointerType, SeqIteratorType>& lhs,
  234. const IndexedListIterator<SeqType, ValueType, ReferenceType,
  235. PointerType, SeqIteratorType>& rhs)
  236. {
  237. return lhs.p >= rhs.p;
  238. }
  239. template<typename SeqType, typename ValueType,
  240. typename ReferenceTypeL, typename PointerTypeL,
  241. typename SeqIteratorTypeL,
  242. typename ReferenceTypeR, typename PointerTypeR,
  243. typename SeqIteratorTypeR>
  244. bool operator>=(const IndexedListIterator<SeqType, ValueType, ReferenceTypeL,
  245. PointerTypeL, SeqIteratorTypeL>& lhs,
  246. const IndexedListIterator<SeqType, ValueType, ReferenceTypeR,
  247. PointerTypeR, SeqIteratorTypeR>& rhs)
  248. {
  249. return lhs.p >= rhs.p;
  250. }
  251. template<typename SeqType, typename ValueType, typename ReferenceType,
  252. typename PointerType, typename SeqIteratorType>
  253. IndexedListIterator<SeqType, ValueType, ReferenceType, PointerType,
  254. SeqIteratorType>
  255. operator+(typename IndexedListIterator<SeqType, ValueType, ReferenceType,
  256. PointerType, SeqIteratorType>::difference_type n,
  257. const IndexedListIterator<SeqType, ValueType, ReferenceType,
  258. PointerType, SeqIteratorType>& lhs)
  259. {
  260. return lhs + n;
  261. }
  262. template<typename SeqType, typename ValueType, typename ReferenceType,
  263. typename PointerType, typename SeqIteratorType>
  264. typename IndexedListIterator<SeqType, ValueType, ReferenceType, PointerType,
  265. SeqIteratorType>::difference_type
  266. operator-(const IndexedListIterator<SeqType, ValueType, ReferenceType,
  267. PointerType, SeqIteratorType>& lhs,
  268. const IndexedListIterator<SeqType, ValueType, ReferenceType,
  269. PointerType, SeqIteratorType>& rhs)
  270. {
  271. return typename IndexedListIterator<SeqType, ValueType, ReferenceType,
  272. PointerType,
  273. SeqIteratorType>::difference_type
  274. (lhs.p - rhs.p);
  275. }
  276. template<typename SeqType, typename ValueType,
  277. typename ReferenceTypeL, typename PointerTypeL,
  278. typename SeqIteratorTypeL,
  279. typename ReferenceTypeR, typename PointerTypeR,
  280. typename SeqIteratorTypeR>
  281. typename IndexedListIterator<SeqType, ValueType, ReferenceTypeL, PointerTypeL,
  282. SeqIteratorTypeL>::difference_type
  283. operator-(const IndexedListIterator<SeqType, ValueType, ReferenceTypeL,
  284. PointerTypeL, SeqIteratorTypeL>& lhs,
  285. const IndexedListIterator<SeqType, ValueType, ReferenceTypeR,
  286. PointerTypeR, SeqIteratorTypeR>& rhs)
  287. {
  288. return typename IndexedListIterator<SeqType, ValueType, ReferenceTypeL,
  289. PointerTypeL,
  290. SeqIteratorTypeL>::difference_type
  291. (lhs.p - rhs.p);
  292. }
  293. template<typename KeyType, typename ValuePtrType>
  294. class IndexedList {
  295. public:
  296. IndexedList() {}
  297. ~IndexedList() {}
  298. typedef KeyType key_type;
  299. typedef ValuePtrType value_type;
  300. typedef std::unordered_map<KeyType, ValuePtrType> IndexType;
  301. typedef std::deque<std::pair<KeyType, ValuePtrType>> SeqType;
  302. typedef IndexedListIterator<SeqType,
  303. ValuePtrType,
  304. ValuePtrType&,
  305. ValuePtrType*,
  306. typename SeqType::iterator> iterator;
  307. typedef IndexedListIterator<SeqType,
  308. ValuePtrType,
  309. const ValuePtrType&,
  310. const ValuePtrType*,
  311. typename SeqType::const_iterator> const_iterator;
  312. ValuePtrType& operator[](size_t n)
  313. {
  314. return seq_[n].second;
  315. }
  316. const ValuePtrType& operator[](size_t n) const
  317. {
  318. return seq_[n].second;
  319. }
  320. // Inserts (|key|, |value|) to the end of the list. If the same key
  321. // has been already added, this function fails. This function
  322. // returns true if it succeeds. Complexity: O(1)
  323. bool push_back(KeyType key, ValuePtrType value)
  324. {
  325. auto i = index_.find(key);
  326. if(i == std::end(index_)) {
  327. index_.insert({key, value});
  328. seq_.emplace_back(key, value);
  329. return true;
  330. } else {
  331. return false;
  332. }
  333. }
  334. // Inserts (|key|, |value|) to the front of the list. If the same
  335. // key has been already added, this function fails. This function
  336. // returns true if it succeeds. Complexity: O(1)
  337. bool push_front(KeyType key, ValuePtrType value)
  338. {
  339. auto i = index_.find(key);
  340. if(i == std::end(index_)) {
  341. index_.insert({key, value});
  342. seq_.emplace_front(key, value);
  343. return true;
  344. } else {
  345. return false;
  346. }
  347. }
  348. // Inserts (|key|, |value|) to the position |dest|. If the same key
  349. // has been already added, this function fails. This function
  350. // returns the iterator to the newly added element if it is
  351. // succeeds, or end(). Complexity: O(N)
  352. iterator insert(size_t dest, KeyType key, ValuePtrType value)
  353. {
  354. if(dest > size()) {
  355. return std::end(seq_);
  356. }
  357. auto i = index_.find(key);
  358. if(i == std::end(index_)) {
  359. auto j = std::begin(seq_);
  360. std::advance(j, dest);
  361. index_.insert({key, value});
  362. return iterator(seq_.insert(j, {key, value}));
  363. } else {
  364. return iterator(std::end(seq_));
  365. }
  366. }
  367. // Inserts (|key|, |value|) to the position |dest|. If the same key
  368. // has been already added, this function fails. This function
  369. // returns the iterator to the newly added element if it is
  370. // succeeds, or end(). Complexity: O(1) if inserted to the first or
  371. // last, otherwise O(N)
  372. iterator insert(iterator dest, KeyType key, ValuePtrType value)
  373. {
  374. auto i = index_.find(key);
  375. if(i == std::end(index_)) {
  376. index_.insert({key, value});
  377. return iterator(seq_.insert(dest.p, {key, value}));
  378. } else {
  379. return iterator(std::end(seq_));
  380. }
  381. }
  382. // Inserts values in iterator range [first, last). The key for each
  383. // value is retrieved by functor |keyFunc|. The insertion position
  384. // is given by |dest|.
  385. template<typename KeyFunc, typename InputIterator>
  386. void insert(iterator dest, KeyFunc keyFunc,
  387. InputIterator first, InputIterator last)
  388. {
  389. std::vector<typename SeqType::value_type> v;
  390. v.reserve(std::distance(first, last));
  391. for(; first != last; ++first) {
  392. auto key = keyFunc(*first);
  393. auto i = index_.find(key);
  394. if(i == std::end(index_)) {
  395. index_.insert({key, *first});
  396. v.emplace_back(key, *first);
  397. }
  398. }
  399. seq_.insert(dest.p, std::begin(v), std::end(v));
  400. }
  401. template<typename KeyFunc, typename InputIterator>
  402. void insert(size_t pos, KeyFunc keyFunc,
  403. InputIterator first, InputIterator last)
  404. {
  405. if(pos > size()) {
  406. return;
  407. }
  408. std::vector<typename SeqType::value_type> v;
  409. v.reserve(std::distance(first, last));
  410. for(; first != last; ++first) {
  411. auto key = keyFunc(*first);
  412. auto i = index_.find(key);
  413. if(i == std::end(index_)) {
  414. index_.insert({key, *first});
  415. v.emplace_back(key, *first);
  416. }
  417. }
  418. seq_.insert(std::begin(seq_) + pos, std::begin(v), std::end(v));
  419. }
  420. // Removes |key| from the list. If the element is not found, this
  421. // function fails. This function returns true if it
  422. // succeeds. Complexity: O(N)
  423. bool remove(KeyType key)
  424. {
  425. auto i = index_.find(key);
  426. if(i == std::end(index_)) {
  427. return false;
  428. }
  429. for(auto j = std::begin(seq_), eoj = std::end(seq_); j != eoj; ++j) {
  430. if((*j).first == key) {
  431. seq_.erase(j);
  432. break;
  433. }
  434. }
  435. index_.erase(i);
  436. return true;
  437. }
  438. // Removes element pointed by iterator |k| from the list. If the
  439. // iterator must be valid. This function returns the iterator
  440. // pointing to the element following the erased element. Complexity:
  441. // O(N)
  442. iterator erase(iterator k)
  443. {
  444. index_.erase((*k.p).first);
  445. return iterator(seq_.erase(k.p));
  446. }
  447. // Removes elements for which Pred returns true. The pred is called
  448. // against each each element once per each.
  449. template<typename Pred>
  450. void remove_if(Pred pred)
  451. {
  452. auto first = std::begin(seq_), last = std::end(seq_);
  453. for(; first != last && !pred((*first).second); ++first);
  454. if(first == last) {
  455. return;
  456. }
  457. index_.erase((*first).first);
  458. auto store = first;
  459. ++first;
  460. for(; first != last; ++first) {
  461. if(pred((*first).second)) {
  462. index_.erase((*first).first);
  463. } else {
  464. *store++ = *first;
  465. }
  466. }
  467. seq_.erase(store, last);
  468. }
  469. // Removes element at the front of the list. If the list is empty,
  470. // this function fails. This function returns true if it
  471. // succeeds. Complexity: O(1)
  472. bool pop_front()
  473. {
  474. if(seq_.empty()) {
  475. return false;
  476. }
  477. index_.erase(seq_.front().first);
  478. seq_.pop_front();
  479. return true;
  480. }
  481. // Moves element with |key| to the specified position. If |how| is
  482. // OFFSET_MODE_CUR, the element is moved to the position |offset|
  483. // relative to the current position. If |how| is OFFSET_MODE_SET,
  484. // the element is moved to the position |offset|. If |how| is
  485. // OFFSET_MODE_END, the element is moved to the position |offset|
  486. // relative to the end of the list. This function returns the
  487. // position the elment is moved to if it succeeds, or -1 if no
  488. // element with |key| is found or |how| is invalid. Complexity:
  489. // O(N)
  490. ssize_t move(KeyType key, ssize_t offset, OffsetMode how)
  491. {
  492. auto idxent = index_.find(key);
  493. if(idxent == std::end(index_)) {
  494. return -1;
  495. }
  496. auto x = std::begin(seq_), eseq = std::end(seq_);
  497. for(; x != eseq; ++x) {
  498. if((*x).first == (*idxent).first) {
  499. break;
  500. }
  501. }
  502. ssize_t xp = std::distance(std::begin(seq_), x);
  503. ssize_t size = index_.size();
  504. ssize_t dest;
  505. if(how == OFFSET_MODE_CUR) {
  506. if(offset > 0) {
  507. dest = std::min(xp+offset, static_cast<ssize_t>(size-1));
  508. } else {
  509. dest = std::max(xp+offset, static_cast<ssize_t>(0));
  510. }
  511. } else {
  512. if(how == OFFSET_MODE_END) {
  513. dest = std::min(size-1+offset, size-1);
  514. } else if(how == OFFSET_MODE_SET) {
  515. dest = std::min(offset, size-1);
  516. } else {
  517. return -1;
  518. }
  519. dest = std::max(dest, static_cast<ssize_t>(0));
  520. }
  521. auto d = std::begin(seq_);
  522. std::advance(d, dest);
  523. if(xp < dest) {
  524. std::rotate(x, x+1, d+1);
  525. } else {
  526. std::rotate(d, x, x+1);
  527. }
  528. return dest;
  529. }
  530. // Returns the value associated by |key|. If it is not found,
  531. // returns ValuePtrType(). Complexity: O(1)
  532. ValuePtrType get(KeyType key) const
  533. {
  534. auto idxent = index_.find(key);
  535. if(idxent == std::end(index_)) {
  536. return ValuePtrType();
  537. } else {
  538. return (*idxent).second;
  539. }
  540. }
  541. size_t size() const
  542. {
  543. return index_.size();
  544. }
  545. size_t empty() const
  546. {
  547. return index_.empty();
  548. }
  549. iterator begin()
  550. {
  551. return iterator(std::begin(seq_));
  552. }
  553. iterator end()
  554. {
  555. return iterator(std::end(seq_));
  556. }
  557. const_iterator begin() const
  558. {
  559. return const_iterator(std::begin(seq_));
  560. }
  561. const_iterator end() const
  562. {
  563. return const_iterator(std::end(seq_));
  564. }
  565. // Removes all elements from the list.
  566. void clear()
  567. {
  568. index_.clear();
  569. seq_.clear();
  570. }
  571. private:
  572. SeqType seq_;
  573. IndexType index_;
  574. };
  575. } // namespace aria2
  576. #endif // D_INDEXED_LIST_H