summaryrefslogtreecommitdiffstats
path: root/libs/hwui/utils/SortedListImpl.cpp
blob: 35171d5b1a5bd1e159d41c430ea9fc25f94a8868 (plain)
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
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "SortedListImpl.h"

namespace android {
namespace uirenderer {

///////////////////////////////////////////////////////////////////////////////
// Sorted list implementation, not for direct use
///////////////////////////////////////////////////////////////////////////////

SortedListImpl::SortedListImpl(size_t itemSize, uint32_t flags): VectorImpl(itemSize, flags) {
}

SortedListImpl::SortedListImpl(const VectorImpl& rhs): VectorImpl(rhs) {
}

SortedListImpl::~SortedListImpl() {
}

SortedListImpl& SortedListImpl::operator =(const SortedListImpl& rhs) {
    return static_cast<SortedListImpl&>
            (VectorImpl::operator =(static_cast<const VectorImpl&> (rhs)));
}

ssize_t SortedListImpl::indexOf(const void* item) const {
    return _indexOrderOf(item);
}

size_t SortedListImpl::orderOf(const void* item) const {
    size_t o;
    _indexOrderOf(item, &o);
    return o;
}

ssize_t SortedListImpl::_indexOrderOf(const void* item, size_t* order) const {
    // binary search
    ssize_t err = NAME_NOT_FOUND;
    ssize_t l = 0;
    ssize_t h = size() - 1;
    ssize_t mid;
    const void* a = arrayImpl();
    const size_t s = itemSize();
    while (l <= h) {
        mid = l + (h - l) / 2;
        const void* const curr = reinterpret_cast<const char *> (a) + (mid * s);
        const int c = do_compare(curr, item);
        if (c == 0) {
            err = l = mid;
            break;
        } else if (c < 0) {
            l = mid + 1;
        } else {
            h = mid - 1;
        }
    }
    if (order) {
        *order = l;
    }
    return err;
}

ssize_t SortedListImpl::add(const void* item) {
    size_t order;
    ssize_t index = _indexOrderOf(item, &order);
    index = VectorImpl::insertAt(item, order, 1);
    return index;
}

ssize_t SortedListImpl::merge(const VectorImpl& vector) {
    // naive merge...
    if (!vector.isEmpty()) {
        const void* buffer = vector.arrayImpl();
        const size_t is = itemSize();
        size_t s = vector.size();
        for (size_t i = 0; i < s; i++) {
            ssize_t err = add(reinterpret_cast<const char*> (buffer) + i * is);
            if (err < 0) {
                return err;
            }
        }
    }
    return NO_ERROR;
}

ssize_t SortedListImpl::merge(const SortedListImpl& vector) {
    // we've merging a sorted vector... nice!
    ssize_t err = NO_ERROR;
    if (!vector.isEmpty()) {
        // first take care of the case where the vectors are sorted together
        if (do_compare(vector.itemLocation(vector.size() - 1), arrayImpl()) <= 0) {
            err = VectorImpl::insertVectorAt(static_cast<const VectorImpl&> (vector), 0);
        } else if (do_compare(vector.arrayImpl(), itemLocation(size() - 1)) >= 0) {
            err = VectorImpl::appendVector(static_cast<const VectorImpl&> (vector));
        } else {
            // this could be made a little better
            err = merge(static_cast<const VectorImpl&> (vector));
        }
    }
    return err;
}

ssize_t SortedListImpl::remove(const void* item) {
    ssize_t i = indexOf(item);
    if (i >= 0) {
        VectorImpl::removeItemsAt(i, 1);
    }
    return i;
}

}; // namespace uirenderer
}; // namespace android