sortedvector.cpp 3.11 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/********************************************************************************************
 *                                                                                          *
 * Copyright (C) 2017 Armin Felder, Dennis Beier                                            *
 * This file is part of RocketChatMobileEngine <https://git.fairkom.net/chat/fairchat>.     *
 *                                                                                          *
 * RocketChatMobileEngine 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 3 of the License, or                        *
 * (at your option) any later version.                                                      *
 *                                                                                          *
 * RocketChatMobileEngine 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 RocketChatMobileEngine. If not, see <http://www.gnu.org/licenses/>.           *
 *                                                                                          *
 ********************************************************************************************/


armin's avatar
fixes  
armin committed
22
23
24
25
#include "sortedvector.h"


template<typename T>
armin's avatar
cleanup    
armin committed
26
int SortedVector<T>::insertSort( const QSharedPointer<T> &pointer )
armin's avatar
fixes  
armin committed
27
28
{
    int row = -1;
29

armin's avatar
fixes  
armin committed
30
31
32
33
    if ( this->isEmpty() ) {
        this->append( pointer );
        row = 0;
    } else {
armin's avatar
asd    
armin committed
34
        auto elementSmallerThanNew = std::lower_bound( this->begin(), this->end(), pointer, []( const QSharedPointer<T> &first, const QSharedPointer<T> &second )->bool{
armin's avatar
iqw    
armin committed
35
36
37
38
39
40
            return ( *first ) > ( *second );
        } );

        this->insert( elementSmallerThanNew, pointer );
        row = elementSmallerThanNew - this->begin();
    }
41

armin's avatar
fixes  
armin committed
42
43
44
45
    return row;
}

template<typename T>
46
int SortedVector<T>::findInsertPosition( const QSharedPointer<T> &pointer ) const
armin's avatar
fixes  
armin committed
47
{
armin's avatar
asd    
armin committed
48
    auto elementSmallerThanNew = std::lower_bound( this->begin(), this->end(), pointer, []( const QSharedPointer<T> &first, const QSharedPointer<T> &second )->bool{
Armin Felder's avatar
Armin Felder committed
49
        return ( *first ) < ( *second );
armin's avatar
iqw    
armin committed
50
    } );
armin's avatar
fixes  
armin committed
51

Armin Felder's avatar
Armin Felder committed
52
    return elementSmallerThanNew - this->begin();;
53

armin's avatar
fixes  
armin committed
54
}
armin's avatar
armin committed
55

56
template<typename T>
armin's avatar
sad    
armin committed
57
int SortedVector<T>::findPosition( const QSharedPointer<T> &pointer ) const
58
{
armin's avatar
iqw    
armin committed
59
60
    auto pos = find( this->begin(), this->end(), pointer );
    return pos - this->begin();
61
62
}

Armin Felder's avatar
Armin Felder committed
63
64
65
template<typename T>
void SortedVector<T>::reOrder()
{
armin's avatar
asd    
armin committed
66
    std::sort( this->begin(), this->end(), [ = ]( const QSharedPointer<T> &first, const QSharedPointer<T> &second )->bool{
armin's avatar
iqw    
armin committed
67
68
        return ( *first ) > ( *second );
    } );
Armin Felder's avatar
Armin Felder committed
69
70
}