aboutsummaryrefslogtreecommitdiff
path: root/common/array.h
diff options
context:
space:
mode:
authorEugene Sandulenko2016-06-04 13:50:15 +0200
committerEugene Sandulenko2016-06-04 13:50:15 +0200
commit603fccf74b9710ce542dfb2745475a530173d96e (patch)
treecf31692feee656dd608b4a3ced999b56f83023e4 /common/array.h
parente0d445bb74d6081c6e0dbbd6fe790acf3e61b7cb (diff)
downloadscummvm-rg350-603fccf74b9710ce542dfb2745475a530173d96e.tar.gz
scummvm-rg350-603fccf74b9710ce542dfb2745475a530173d96e.tar.bz2
scummvm-rg350-603fccf74b9710ce542dfb2745475a530173d96e.zip
COMMON: Fixed SortedArray implementation
Diffstat (limited to 'common/array.h')
-rw-r--r--common/array.h31
1 files changed, 17 insertions, 14 deletions
diff --git a/common/array.h b/common/array.h
index e9b97aa38a..04ec9f9ccb 100644
--- a/common/array.h
+++ b/common/array.h
@@ -383,56 +383,59 @@ public:
return;
}
- T *where = (T *)bsearchMin(element, this->front(), this->_size, sizeof(T), _comparator);
- insert(where, element);
+ T *where = bsearchMin(element);
+
+ if (where > this->_storage + this->_size)
+ Array<T>::push_back(element);
+ else
+ Array<T>::insert(where, element);
}
T &operator[](size_type idx) {
- error("Operation not allowed with SortedArray");
+ error("Operation []= not allowed with SortedArray");
}
void insert_at(size_type idx, const T &element) {
- error("Operation not allowed with SortedArray");
+ error("Operation insert_at(idx, element) not allowed with SortedArray");
}
void insert_at(size_type idx, const Array<T> &array) {
- error("Operation not allowed with SortedArray");
+ error("Operation insert_at(idx, array) not allowed with SortedArray");
}
void insert(iterator pos, const T &element) {
- error("Operation not allowed with SortedArray");
+ error("Operation insert(pos, elemnet) not allowed with SortedArray");
}
void push_back(const T &element) {
- error("Operation not allowed with SortedArray");
+ error("Operation push_back(element) not allowed with SortedArray");
}
void push_back(const Array<T> &array) {
- error("Operation not allowed with SortedArray");
+ error("Operation push_back(array) not allowed with SortedArray");
}
private:
// Based on code Copyright (C) 2008-2009 Ksplice, Inc.
// Author: Tim Abbott <tabbott@ksplice.com>
// Licensed under GPLv2+
- void *bsearchMin(void *key, void *base, uint num, uint size_,
- int (*cmp)(const void *key, const void *elt)) {
- uint start_ = 0, end_ = num;
+ T *bsearchMin(void *key) {
+ uint start_ = 0, end_ = this->_size;
int result;
while (start_ < end_) {
uint mid = start_ + (end_ - start_) / 2;
- result = cmp(key, (byte *)base + mid * size_);
+ result = this->_comparator(key, this->_storage[mid]);
if (result < 0)
end_ = mid;
else if (result > 0)
start_ = mid + 1;
else
- return (void *)((byte *)base + mid * size_);
+ return &this->_storage[mid];
}
- return (void *)((byte *)base + start_ * size_);
+ return &this->_storage[start_];
}
private: