Skip to content

Commit b2790fb

Browse files
Владимир СеверовВладимир Северов
authored andcommitted
17.04.11 19:42 - Bugs Fix
1 parent 43b0ce9 commit b2790fb

File tree

2 files changed

+37
-34
lines changed

2 files changed

+37
-34
lines changed

HW-1/HW-1/Source.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ int main()
6868
std::cout << test_2.BucketSize(j) << " ";
6969
}
7070
std::cout << std::endl;
71+
std::cout << *(test_2.Find(3)) << std::endl;
7172
std::cout << *(test_2.Begin()) << std::endl;
7273
std::cout << *(--test_2.End()) << std::endl;
7374
test_2.Erase(2);

HW-1/HW-1/UnorderedSet.h

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ template< class Key,
2626

2727
// Iterators
2828
class Iterator
29-
: public std::iterator<std::random_access_iterator_tag, value_type>
29+
: public std::iterator<std::bidirectional_iterator_tag, value_type>
3030
{
3131
public:
3232
Iterator() = default;
@@ -84,7 +84,7 @@ template< class Key,
8484

8585
Iterator& operator--()
8686
{
87-
if (!IsBegin()) {
87+
if (!IsBeforeBegin()) {
8888
if (valueIndex_ > 0) {
8989
--valueIndex_;
9090
}
@@ -102,21 +102,25 @@ template< class Key,
102102
return *this;
103103
}
104104

105-
bool IsBegin() {
105+
bool IsBeforeBegin() {
106106
return bucketIndex_ < 0;
107107
}
108108

109109
bool IsEnd() {
110110
return bucketIndex_ > (buckets_.size() - 1);
111111
}
112112

113+
bool IsBucketBegin() {
114+
return valueIndex_ == 0;
115+
}
116+
113117
protected:
114118
buckets_type & buckets_;
115119
int bucketIndex_;
116120
int valueIndex_;
117121
value_type * ptr_;
118122

119-
Iterator(buckets_type & buckets, bool isEnd = false)
123+
Iterator(buckets_type & buckets, bool isEnd)
120124
: buckets_(buckets)
121125
{
122126
if (isEnd) {
@@ -135,7 +139,7 @@ template< class Key,
135139
ptr_ = nullptr;
136140
}
137141

138-
Iterator(buckets_type & buckets, int bucketIndex, bool isEnd = false)
142+
Iterator(buckets_type & buckets, int bucketIndex, bool isEnd)
139143
: buckets_(buckets)
140144
, bucketIndex_(bucketIndex)
141145
{
@@ -246,17 +250,17 @@ template< class Key,
246250
// First iterator
247251
iterator Begin()
248252
{
249-
return iterator(buckets_);
253+
return iterator(buckets_, false);
250254
}
251255

252256
const_iterator Begin() const
253257
{
254-
return const_iterator(buckets_);
258+
return const_iterator(buckets_, false);
255259
}
256260

257261
const_iterator CBegin() const
258262
{
259-
return const_iterator(buckets_);
263+
return const_iterator(buckets_, false);
260264
}
261265

262266
// Iterator after last
@@ -315,14 +319,14 @@ template< class Key,
315319
{
316320
iterator it = Find(value);
317321
if (!it.IsEnd()) {
318-
return std::pair<iterator, bool>(it, false);
322+
return std::make_pair(it, false);
319323
}
320324

321325
if (float(Size() + 1) / (float)BucketCount() >= MaxLoadFactor()) {
322326
Rehash(buckets_.size() * MIN_BUCKETS_COUNT);
323327
}
324328
buckets_[Bucket(value)].push_back(value);
325-
return std::pair<iterator, bool>(iterator(buckets_, &(buckets_[Bucket(value)].back())), true);
329+
return std::make_pair(iterator(buckets_, &(buckets_[Bucket(value)].back())), true);
326330
}
327331

328332
// Insert elements from [first, last)
@@ -406,45 +410,43 @@ template< class Key,
406410
// Element with this value
407411
iterator Find(const value_type& value)
408412
{
409-
for (iterator it(buckets_); !it.IsEnd(); ++it) {
410-
if (keyEqual_(*it, value)) {
411-
return it;
412-
}
413+
iterator it = iterator(buckets_, Bucket(value), false);
414+
if (it.ptr_ != nullptr) {
415+
do {
416+
if (keyEqual_(*it, value)) {
417+
return it;
418+
}
419+
++it;
420+
} while (!it.IsBucketBegin());
413421
}
414422
return iterator(buckets_, true);
415423
}
416424

417425
const_iterator Find(const value_type& value) const
418426
{
419-
for (const_iterator it(buckets_); !it.IsEnd(); ++it) {
420-
if (keyEqual_(*it, value)) {
421-
return it;
422-
}
427+
const_iterator it = const_iterator(buckets_, Bucket(value), false);
428+
if (it.ptr_ != nullptr) {
429+
do {
430+
if (keyEqual_(*it, value)) {
431+
return it;
432+
}
433+
++it;
434+
} while (!it.IsBucketBegin());
423435
}
424436
return const_iterator(buckets_, true);
425437
}
426438

427439
// Range of element with this value
428440
std::pair<iterator, iterator> EqualRange(const value_type& value)
429441
{
430-
for (iterator it(buckets_); !it.IsEnd(); ++it) {
431-
if (keyEqual_(*it, value)) {
432-
return std::pair<iterator, iterator>(it, it);
433-
}
434-
}
435-
iterator it = iterator(buckets_, true);
436-
return std::pair<iterator, iterator>(it, it);
442+
iterator it = Find(value);
443+
return std::make_pair(it, it);
437444
}
438445

439446
std::pair<const_iterator, const_iterator> EqualRange(const value_type& value) const
440447
{
441-
for (const_iterator it(buckets_); !it.IsEnd(); ++it) {
442-
if (keyEqual_(*it, value)) {
443-
return std::pair<const_iterator, const_iterator>(it, it);
444-
}
445-
}
446-
const_iterator it = const_iterator(buckets_, true);
447-
return std::pair<const_iterator, const_iterator>(it, it);
448+
iterator it = Find(value);
449+
return std::make_pair(it, it);
448450
}
449451

450452
/* BUCKET INTERFACE */
@@ -532,7 +534,7 @@ template< class Key,
532534
// Sets the number of buckets to count and rehashes the container
533535
void Rehash(size_type count)
534536
{
535-
if (count < Size() / MaxLoadFactor()) {
537+
if (count < Size() / MaxLoadFactor()) {
536538
count = std::ceil(Size() / MaxLoadFactor());
537539
};
538540
size_type newCount = MIN_BUCKETS_COUNT;
@@ -548,7 +550,7 @@ template< class Key,
548550

549551
buckets_.clear();
550552
buckets_.resize(newCount);
551-
for (iterator it(bucketsCopy); !it.IsEnd(); ++it) {
553+
for (iterator it(bucketsCopy, false); !it.IsEnd(); ++it) {
552554
Insert(*it);
553555
}
554556
}

0 commit comments

Comments
 (0)