File: IndexedList.h

package info (click to toggle)
aria2 1.18.8-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 19,392 kB
  • ctags: 16,036
  • sloc: cpp: 115,823; sh: 12,015; ansic: 7,394; makefile: 1,445; ruby: 462; python: 216; xml: 176; asm: 58; sed: 16
file content (638 lines) | stat: -rw-r--r-- 20,732 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/* <!-- copyright */
/*
 * aria2 - The high speed download utility
 *
 * Copyright (C) 2012 Tatsuhiro Tsujikawa
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations
 * including the two.
 * You must obey the GNU General Public License in all respects
 * for all of the code used other than OpenSSL.  If you modify
 * file(s) with this exception, you may extend this exception to your
 * version of the file(s), but you are not obligated to do so.  If you
 * do not wish to do so, delete this exception statement from your
 * version.  If you delete this exception statement from all source
 * files in the program, then also delete it here.
 */
/* copyright --> */
#ifndef D_INDEXED_LIST_H
#define D_INDEXED_LIST_H

#include "common.h"

#include <deque>
#include <unordered_map>
#include <vector>
#include <algorithm>

#include <aria2/aria2.h>

namespace aria2 {

template<typename SeqType, typename ValueType, typename ReferenceType,
         typename PointerType, typename SeqIteratorType>
struct IndexedListIterator {
  typedef IndexedListIterator<SeqType,
                              ValueType,
                              ValueType&,
                              ValueType*,
                              typename SeqType::iterator> iterator;
  typedef IndexedListIterator<SeqType,
                              ValueType,
                              const ValueType&,
                              const ValueType*,
                              typename SeqType::const_iterator> const_iterator;

  typedef typename SeqIteratorType::iterator_category iterator_category;
  typedef ValueType value_type;
  typedef PointerType pointer;
  typedef ReferenceType reference;
  typedef typename SeqType::size_type size_type;
  typedef typename SeqType::difference_type difference_type;
  typedef IndexedListIterator SelfType;

  IndexedListIterator() {}
  IndexedListIterator(const iterator& other)
    : p(other.p) {}
  IndexedListIterator(const SeqIteratorType& p)
    : p(p) {}

  reference operator*() const
  {
    return (*p).second;
  }

  pointer operator->() const
  {
    return &(*p).second;
  }

  SelfType& operator++()
  {
    ++p;
    return *this;
  }

  SelfType operator++(int)
  {
    SelfType copy = *this;
    ++*this;
    return copy;
  }

  SelfType& operator--()
  {
    --p;
    return *this;
  }

  SelfType operator--(int)
  {
    SelfType copy = *this;
    --*this;
    return copy;
  }

  SelfType& operator+=(difference_type n)
  {
    std::advance(p, n);
    return *this;
  }

  SelfType operator+(difference_type n) const
  {
    SelfType copy = *this;
    return copy += n;
  }

  SelfType& operator-=(difference_type n)
  {
    std::advance(p, -n);
    return *this;
  }

  SelfType operator-(difference_type n) const
  {
    SelfType copy = *this;
    return copy -= n;
  }

  reference operator[](size_type n) const
  {
    return p[n].second;
  }

  SeqIteratorType p;
};

template<typename SeqType, typename ValueType, typename ReferenceType,
         typename PointerType, typename SeqIteratorType>
bool operator==(const IndexedListIterator<SeqType, ValueType, ReferenceType,
                PointerType, SeqIteratorType>& lhs,
                const IndexedListIterator<SeqType, ValueType, ReferenceType,
                PointerType, SeqIteratorType>& rhs)
{
  return lhs.p == rhs.p;
}

template<typename SeqType, typename ValueType,
         typename ReferenceTypeL, typename PointerTypeL,
         typename SeqIteratorTypeL,
         typename ReferenceTypeR, typename PointerTypeR,
         typename SeqIteratorTypeR>
bool operator==(const IndexedListIterator<SeqType, ValueType, ReferenceTypeL,
                PointerTypeL, SeqIteratorTypeL>& lhs,
                const IndexedListIterator<SeqType, ValueType, ReferenceTypeR,
                PointerTypeR, SeqIteratorTypeR>& rhs)
{
  return lhs.p == rhs.p;
}

template<typename SeqType, typename ValueType, typename ReferenceType,
         typename PointerType, typename SeqIteratorType>
bool operator!=(const IndexedListIterator<SeqType, ValueType, ReferenceType,
                PointerType, SeqIteratorType>& lhs,
                const IndexedListIterator<SeqType, ValueType, ReferenceType,
                PointerType, SeqIteratorType>& rhs)
{
  return lhs.p != rhs.p;
}

template<typename SeqType, typename ValueType,
         typename ReferenceTypeL, typename PointerTypeL,
         typename SeqIteratorTypeL,
         typename ReferenceTypeR, typename PointerTypeR,
         typename SeqIteratorTypeR>
bool operator!=(const IndexedListIterator<SeqType, ValueType, ReferenceTypeL,
                PointerTypeL, SeqIteratorTypeL>& lhs,
                const IndexedListIterator<SeqType, ValueType, ReferenceTypeR,
                PointerTypeR, SeqIteratorTypeR>& rhs)
{
  return lhs.p != rhs.p;
}

template<typename SeqType, typename ValueType, typename ReferenceType,
         typename PointerType, typename SeqIteratorType>
bool operator<(const IndexedListIterator<SeqType, ValueType, ReferenceType,
                                         PointerType, SeqIteratorType>& lhs,
               const IndexedListIterator<SeqType, ValueType, ReferenceType,
                                         PointerType, SeqIteratorType>& rhs)
{
  return lhs.p < rhs.p;
}

template<typename SeqType, typename ValueType,
         typename ReferenceTypeL, typename PointerTypeL,
         typename SeqIteratorTypeL,
         typename ReferenceTypeR, typename PointerTypeR,
         typename SeqIteratorTypeR>
bool operator<(const IndexedListIterator<SeqType, ValueType, ReferenceTypeL,
                                         PointerTypeL, SeqIteratorTypeL>& lhs,
               const IndexedListIterator<SeqType, ValueType, ReferenceTypeR,
                                         PointerTypeR, SeqIteratorTypeR>& rhs)
{
  return lhs.p < rhs.p;
}

template<typename SeqType, typename ValueType, typename ReferenceType,
         typename PointerType, typename SeqIteratorType>
bool operator>(const IndexedListIterator<SeqType, ValueType, ReferenceType,
                                         PointerType, SeqIteratorType>& lhs,
               const IndexedListIterator<SeqType, ValueType, ReferenceType,
                                         PointerType, SeqIteratorType>& rhs)
{
  return lhs.p > rhs.p;
}

template<typename SeqType, typename ValueType,
         typename ReferenceTypeL, typename PointerTypeL,
         typename SeqIteratorTypeL,
         typename ReferenceTypeR, typename PointerTypeR,
         typename SeqIteratorTypeR>
bool operator>(const IndexedListIterator<SeqType, ValueType, ReferenceTypeL,
                                         PointerTypeL, SeqIteratorTypeL>& lhs,
               const IndexedListIterator<SeqType, ValueType, ReferenceTypeR,
                                         PointerTypeR, SeqIteratorTypeR>& rhs)
{
  return lhs.p > rhs.p;
}

template<typename SeqType, typename ValueType, typename ReferenceType,
         typename PointerType, typename SeqIteratorType>
bool operator<=(const IndexedListIterator<SeqType, ValueType, ReferenceType,
                                          PointerType, SeqIteratorType>& lhs,
                const IndexedListIterator<SeqType, ValueType, ReferenceType,
                                          PointerType, SeqIteratorType>& rhs)
{
  return lhs.p <= rhs.p;
}

template<typename SeqType, typename ValueType,
         typename ReferenceTypeL, typename PointerTypeL,
         typename SeqIteratorTypeL,
         typename ReferenceTypeR, typename PointerTypeR,
         typename SeqIteratorTypeR>
bool operator<=(const IndexedListIterator<SeqType, ValueType, ReferenceTypeL,
                                          PointerTypeL, SeqIteratorTypeL>& lhs,
                const IndexedListIterator<SeqType, ValueType, ReferenceTypeR,
                                          PointerTypeR, SeqIteratorTypeR>& rhs)
{
  return lhs.p <= rhs.p;
}

template<typename SeqType, typename ValueType, typename ReferenceType,
         typename PointerType, typename SeqIteratorType>
bool operator>=(const IndexedListIterator<SeqType, ValueType, ReferenceType,
                                          PointerType, SeqIteratorType>& lhs,
                const IndexedListIterator<SeqType, ValueType, ReferenceType,
                                          PointerType, SeqIteratorType>& rhs)
{
  return lhs.p >= rhs.p;
}

template<typename SeqType, typename ValueType,
         typename ReferenceTypeL, typename PointerTypeL,
         typename SeqIteratorTypeL,
         typename ReferenceTypeR, typename PointerTypeR,
         typename SeqIteratorTypeR>
bool operator>=(const IndexedListIterator<SeqType, ValueType, ReferenceTypeL,
                                          PointerTypeL, SeqIteratorTypeL>& lhs,
                const IndexedListIterator<SeqType, ValueType, ReferenceTypeR,
                                          PointerTypeR, SeqIteratorTypeR>& rhs)
{
  return lhs.p >= rhs.p;
}

template<typename SeqType, typename ValueType, typename ReferenceType,
         typename PointerType, typename SeqIteratorType>
IndexedListIterator<SeqType, ValueType, ReferenceType, PointerType,
                    SeqIteratorType>
operator+(typename IndexedListIterator<SeqType, ValueType, ReferenceType,
          PointerType, SeqIteratorType>::difference_type n,
          const IndexedListIterator<SeqType, ValueType, ReferenceType,
                                    PointerType, SeqIteratorType>& lhs)
{
  return lhs + n;
}

template<typename SeqType, typename ValueType, typename ReferenceType,
         typename PointerType, typename SeqIteratorType>
typename IndexedListIterator<SeqType, ValueType, ReferenceType, PointerType,
                             SeqIteratorType>::difference_type
operator-(const IndexedListIterator<SeqType, ValueType, ReferenceType,
                                    PointerType, SeqIteratorType>& lhs,
          const IndexedListIterator<SeqType, ValueType, ReferenceType,
                                    PointerType, SeqIteratorType>& rhs)
{
  return typename IndexedListIterator<SeqType, ValueType, ReferenceType,
                                      PointerType,
                                      SeqIteratorType>::difference_type
    (lhs.p - rhs.p);
}

template<typename SeqType, typename ValueType,
         typename ReferenceTypeL, typename PointerTypeL,
         typename SeqIteratorTypeL,
         typename ReferenceTypeR, typename PointerTypeR,
         typename SeqIteratorTypeR>
typename IndexedListIterator<SeqType, ValueType, ReferenceTypeL, PointerTypeL,
                             SeqIteratorTypeL>::difference_type
operator-(const IndexedListIterator<SeqType, ValueType, ReferenceTypeL,
                                    PointerTypeL, SeqIteratorTypeL>& lhs,
          const IndexedListIterator<SeqType, ValueType, ReferenceTypeR,
                                    PointerTypeR, SeqIteratorTypeR>& rhs)
{
  return typename IndexedListIterator<SeqType, ValueType, ReferenceTypeL,
                                      PointerTypeL,
                                      SeqIteratorTypeL>::difference_type
    (lhs.p - rhs.p);
}

template<typename KeyType, typename ValuePtrType>
class IndexedList {
public:
  IndexedList() {}
  ~IndexedList() {}

  typedef KeyType key_type;
  typedef ValuePtrType value_type;
  typedef std::unordered_map<KeyType, ValuePtrType> IndexType;
  typedef std::deque<std::pair<KeyType, ValuePtrType>> SeqType;



  typedef IndexedListIterator<SeqType,
                              ValuePtrType,
                              ValuePtrType&,
                              ValuePtrType*,
                              typename SeqType::iterator> iterator;
  typedef IndexedListIterator<SeqType,
                              ValuePtrType,
                              const ValuePtrType&,
                              const ValuePtrType*,
                              typename SeqType::const_iterator> const_iterator;

  ValuePtrType& operator[](size_t n)
  {
    return seq_[n].second;
  }

  const ValuePtrType& operator[](size_t n) const
  {
    return seq_[n].second;
  }

  // Inserts (|key|, |value|) to the end of the list. If the same key
  // has been already added, this function fails. This function
  // returns true if it succeeds. Complexity: O(1)
  bool push_back(KeyType key, ValuePtrType value)
  {
    auto i = index_.find(key);
    if(i == std::end(index_)) {
      index_.insert({key, value});
      seq_.emplace_back(key, value);
      return true;
    } else {
      return false;
    }
  }

  // Inserts (|key|, |value|) to the front of the list. If the same
  // key has been already added, this function fails. This function
  // returns true if it succeeds. Complexity: O(1)
  bool push_front(KeyType key, ValuePtrType value)
  {
    auto i = index_.find(key);
    if(i == std::end(index_)) {
      index_.insert({key, value});
      seq_.emplace_front(key, value);
      return true;
    } else {
      return false;
    }
  }

  // Inserts (|key|, |value|) to the position |dest|. If the same key
  // has been already added, this function fails. This function
  // returns the iterator to the newly added element if it is
  // succeeds, or end(). Complexity: O(N)
  iterator insert(size_t dest, KeyType key, ValuePtrType value)
  {
    if(dest > size()) {
      return std::end(seq_);
    }
    auto i = index_.find(key);
    if(i == std::end(index_)) {
      auto j = std::begin(seq_);
      std::advance(j, dest);
      index_.insert({key, value});
      return iterator(seq_.insert(j, {key, value}));
    } else {
      return iterator(std::end(seq_));
    }
  }

  // Inserts (|key|, |value|) to the position |dest|. If the same key
  // has been already added, this function fails. This function
  // returns the iterator to the newly added element if it is
  // succeeds, or end(). Complexity: O(1) if inserted to the first or
  // last, otherwise O(N)
  iterator insert(iterator dest, KeyType key, ValuePtrType value)
  {
    auto i = index_.find(key);
    if(i == std::end(index_)) {
      index_.insert({key, value});
      return iterator(seq_.insert(dest.p, {key, value}));
    } else {
      return iterator(std::end(seq_));
    }
  }

  // Inserts values in iterator range [first, last). The key for each
  // value is retrieved by functor |keyFunc|. The insertion position
  // is given by |dest|.
  template<typename KeyFunc, typename InputIterator>
  void insert(iterator dest, KeyFunc keyFunc,
              InputIterator first, InputIterator last)
  {
    std::vector<typename SeqType::value_type> v;
    v.reserve(std::distance(first, last));
    for(; first != last; ++first) {
      auto key = keyFunc(*first);
      auto i = index_.find(key);
      if(i == std::end(index_)) {
        index_.insert({key, *first});
        v.emplace_back(key, *first);
      }
    }
    seq_.insert(dest.p, std::begin(v), std::end(v));
  }

  template<typename KeyFunc, typename InputIterator>
  void insert(size_t pos, KeyFunc keyFunc,
              InputIterator first, InputIterator last)
  {
    if(pos > size()) {
      return;
    }
    std::vector<typename SeqType::value_type> v;
    v.reserve(std::distance(first, last));
    for(; first != last; ++first) {
      auto key = keyFunc(*first);
      auto i = index_.find(key);
      if(i == std::end(index_)) {
        index_.insert({key, *first});
        v.emplace_back(key, *first);
      }
    }
    seq_.insert(std::begin(seq_) + pos, std::begin(v), std::end(v));
  }

  // Removes |key| from the list. If the element is not found, this
  // function fails. This function returns true if it
  // succeeds. Complexity: O(N)
  bool remove(KeyType key)
  {
    auto i = index_.find(key);
    if(i == std::end(index_)) {
      return false;
    }
    for(auto j = std::begin(seq_), eoj = std::end(seq_); j != eoj; ++j) {
      if((*j).first == key) {
        seq_.erase(j);
        break;
      }
    }
    index_.erase(i);
    return true;
  }

  // Removes element pointed by iterator |k| from the list. If the
  // iterator must be valid. This function returns the iterator
  // pointing to the element following the erased element. Complexity:
  // O(N)
  iterator erase(iterator k)
  {
    index_.erase((*k.p).first);
    return iterator(seq_.erase(k.p));
  }

  // Removes elements for which Pred returns true. The pred is called
  // against each each element once per each.
  template<typename Pred>
  void remove_if(Pred pred)
  {
    auto first = std::begin(seq_), last = std::end(seq_);
    for(; first != last && !pred((*first).second); ++first);
    if(first == last) {
      return;
    }
    index_.erase((*first).first);
    auto store = first;
    ++first;
    for(; first != last; ++first) {
      if(pred((*first).second)) {
        index_.erase((*first).first);
      } else {
        *store++ = *first;
      }
    }
    seq_.erase(store, last);
  }

  // Removes element at the front of the list. If the list is empty,
  // this function fails. This function returns true if it
  // succeeds. Complexity: O(1)
  bool pop_front()
  {
    if(seq_.empty()) {
      return false;
    }
    index_.erase(seq_.front().first);
    seq_.pop_front();
    return true;
  }

  // Moves element with |key| to the specified position. If |how| is
  // OFFSET_MODE_CUR, the element is moved to the position |offset|
  // relative to the current position. If |how| is OFFSET_MODE_SET,
  // the element is moved to the position |offset|. If |how| is
  // OFFSET_MODE_END, the element is moved to the position |offset|
  // relative to the end of the list.  This function returns the
  // position the elment is moved to if it succeeds, or -1 if no
  // element with |key| is found or |how| is invalid.  Complexity:
  // O(N)
  ssize_t move(KeyType key, ssize_t offset, OffsetMode how)
  {
    auto idxent = index_.find(key);
    if(idxent == std::end(index_)) {
      return -1;
    }
    auto x = std::begin(seq_), eseq = std::end(seq_);
    for(; x != eseq; ++x) {
      if((*x).first == (*idxent).first) {
        break;
      }
    }
    ssize_t xp = std::distance(std::begin(seq_), x);
    ssize_t size = index_.size();
    ssize_t dest;
    if(how == OFFSET_MODE_CUR) {
      if(offset > 0) {
        dest = std::min(xp+offset, static_cast<ssize_t>(size-1));
      } else {
        dest = std::max(xp+offset, static_cast<ssize_t>(0));
      }
    } else {
      if(how == OFFSET_MODE_END) {
        dest = std::min(size-1+offset, size-1);
      } else if(how == OFFSET_MODE_SET) {
        dest = std::min(offset, size-1);
      } else {
        return -1;
      }
      dest = std::max(dest, static_cast<ssize_t>(0));
    }
    auto d = std::begin(seq_);
    std::advance(d, dest);
    if(xp < dest) {
      std::rotate(x, x+1, d+1);
    } else {
      std::rotate(d, x, x+1);
    }
    return dest;
  }

  // Returns the value associated by |key|. If it is not found,
  // returns ValuePtrType().  Complexity: O(1)
  ValuePtrType get(KeyType key) const
  {
    auto idxent = index_.find(key);
    if(idxent == std::end(index_)) {
      return ValuePtrType();
    } else {
      return (*idxent).second;
    }
  }

  size_t size() const
  {
    return index_.size();
  }

  size_t empty() const
  {
    return index_.empty();
  }

  iterator begin()
  {
    return iterator(std::begin(seq_));
  }

  iterator end()
  {
    return iterator(std::end(seq_));
  }

  const_iterator begin() const
  {
    return const_iterator(std::begin(seq_));
  }

  const_iterator end() const
  {
    return const_iterator(std::end(seq_));
  }

  // Removes all elements from the list.
  void clear()
  {
    index_.clear();
    seq_.clear();
  }
private:
  SeqType seq_;
  IndexType index_;
};

} // namespace aria2

#endif // D_INDEXED_LIST_H