File: setrepository.cpp

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (1209 lines) | stat: -rw-r--r-- 43,096 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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
/***************************************************************************
   Copyright 2007 David Nolden <david.nolden.kdevelop@art-master.de>
***************************************************************************/

/***************************************************************************
*                                                                         *
*   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.                                   *
*                                                                         *
***************************************************************************/

#include "setrepository.h"
#include <debug.h>
#include <list>
#include <util/kdevvarlengtharray.h>
#include <iostream>
#include <limits>
#include <serialization/itemrepository.h>
#include <serialization/indexedstring.h>
#include <QString>
#include <QMutex>
#include <algorithm>

//#define DEBUG_SETREPOSITORY

#ifdef DEBUG_SETREPOSITORY
#define ifDebug(X) X
#else
#define ifDebug(x)
#undef Q_ASSERT
#define Q_ASSERT(x)
#endif

#ifndef DEBUG_SETREPOSITORY
#define CHECK_SPLIT_POSITION(Node)
#else
#define CHECK_SPLIT_POSITION(node) Q_ASSERT(!(node).leftNode || \
                                            (getLeftNode(&node)->end() <= \
                                             splitPositionForRange((node).start, \
                                                                   (node).end) && \
                                             getRightNode(&node)->start() >= \
                                             splitPositionForRange((node).start, (node).end)))
#endif

namespace Utils {
/**
 * To achieve a maximum re-usage of nodes, we make sure that sub-nodes of a node always split at specific boundaries.
 * For each range we can compute a position where that range should be split into its child-nodes.
 * When creating a new node with 2 sub-nodes, we re-create those child-nodes if their boundaries don't represent those split-positions.
 *
 * We pick the split-positions deterministically, they are in order of priority:
 * ((1<<31)*n, n = [0,...]
 * ((1<<30)*n, n = [0,...]
 * ((1<<29)*n, n = [0,...]
 * ((1<<...)*n, n = [0,...]
 * ...
 * */

using Index = BasicSetRepository::Index;

///The returned split position shall be the end of the first sub-range, and the start of the second
///@param splitBit should be initialized with 31, unless you know better. The value can then be used on while computing child split positions.
///In the end, it will contain the bit used to split the range. It will also contain zero if no split-position exists(length 1)
uint splitPositionForRange(uint start, uint end, uchar& splitBit)
{
    if (end - start == 1) {
        splitBit = 0;
        return 0;
    }

    while (true) {
        uint position = ((end - 1) >> splitBit) << splitBit; //Round to the split-position in this interval that is smaller than end
        if (position > start && position < end)
            return position;
        Q_ASSERT(splitBit != 0);
        --splitBit;
    }

    return 0;
}

uint splitPositionForRange(uint start, uint end)
{
    uchar splitBit = 31;
    return splitPositionForRange(start, end, splitBit);
}

class SetNodeDataRequest;

    #define getLeftNode(node) repository.itemFromIndex(node->leftNode())
    #define getRightNode(node) repository.itemFromIndex(node->rightNode())
    #define nodeFromIndex(index) repository.itemFromIndex(index)
struct SetRepositoryAlgorithms
{
    SetRepositoryAlgorithms(SetDataRepository& _repository,
                            BasicSetRepository* _setRepository) : repository(_repository)
        , setRepository(_setRepository)
    {
    }

    ///Expensive
    Index count(const SetNodeData* node) const;

    void localCheck(const SetNodeData* node);

    void check(uint node);

    void check(const SetNodeData* node);

    QString shortLabel(const SetNodeData& node) const;

    uint set_union(uint firstNode, uint secondNode, const SetNodeData* first, const SetNodeData* second,
                   uchar splitBit = 31);
    uint createSetFromNodes(uint leftNode, uint rightNode, const SetNodeData* left = nullptr,
                            const SetNodeData* right = nullptr);
    uint computeSetFromNodes(uint leftNode, uint rightNode, const SetNodeData* left, const SetNodeData* right,
                             uchar splitBit);
    uint set_intersect(uint firstNode, uint secondNode, const SetNodeData* first, const SetNodeData* second,
                       uchar splitBit = 31);
    bool set_contains(const SetNodeData* node, Index index);
    uint set_subtract(uint firstNode, uint secondNode, const SetNodeData* first, const SetNodeData* second,
                      uchar splitBit = 31);

    //Required both nodes to be split correctly
    bool set_equals(const SetNodeData* lhs, const SetNodeData* rhs);

    QString dumpDotGraph(uint node) const;

    ///Finds or inserts the given ranges into the repository, and returns the set-index that represents them
    uint setForIndices(std::vector<uint>::const_iterator begin, std::vector<uint>::const_iterator end,
                       uchar splitBit = 31)
    {
        Q_ASSERT(begin != end);

        uint startIndex = *begin;
        uint endIndex = *(end - 1) + 1;

        if (endIndex == startIndex + 1) {
            SetNodeData data(startIndex, endIndex);

            return repository.index(SetNodeDataRequest(&data, repository, setRepository));
        }

        uint split = splitPositionForRange(startIndex, endIndex, splitBit);
        Q_ASSERT(split);

        auto splitIterator = std::lower_bound(begin, end, split);
        Q_ASSERT(*splitIterator >= split);
        Q_ASSERT(splitIterator > begin);
        Q_ASSERT(*(splitIterator - 1) < split);

        return createSetFromNodes(setForIndices(begin, splitIterator, splitBit),
                                  setForIndices(splitIterator, end, splitBit));
    }

private:
    QString dumpDotGraphInternal(uint node, bool master = false) const;

    SetDataRepository& repository;
    BasicSetRepository* setRepository;
};

void SetNodeDataRequest::destroy(SetNodeData* data, KDevelop::AbstractItemRepository& _repository)
{
    auto& repository(static_cast<SetDataRepository&>(_repository));

    if (repository.setRepository->delayedDeletion()) {
        if (data->leftNode()) {
            SetDataRepositoryBase::MyDynamicItem left = repository.dynamicItemFromIndex(data->leftNode());
            SetDataRepositoryBase::MyDynamicItem right = repository.dynamicItemFromIndex(data->rightNode());
            Q_ASSERT(left->m_refCount > 0);
            --left->m_refCount;
            Q_ASSERT(right->m_refCount > 0);
            --right->m_refCount;
        } else {
            //Deleting a leaf
            Q_ASSERT(data->end() - data->start() == 1);
            repository.setRepository->itemRemovedFromSets(data->start());
        }
    }
}

SetNodeDataRequest::SetNodeDataRequest(const SetNodeData* _data, SetDataRepository& _repository,
                                       BasicSetRepository* _setRepository) : data(*_data)
    , m_hash(_data->hash())
    , repository(_repository)
    , setRepository(_setRepository)
    , m_created(false)
{
    ifDebug(SetRepositoryAlgorithms alg(repository); alg.check(_data));
}

SetNodeDataRequest::~SetNodeDataRequest()
{
    //Eventually increase the reference-count of direct children
    if (m_created) {
        if (data.leftNode())
            ++repository.dynamicItemFromIndex(data.leftNode())->m_refCount;
        if (data.rightNode())
            ++repository.dynamicItemFromIndex(data.rightNode())->m_refCount;
    }
}

//Should create an item where the information of the requested item is permanently stored. The pointer
//@param item equals an allocated range with the size of itemSize().
void SetNodeDataRequest::createItem(SetNodeData* item) const
{
    Q_ASSERT((data.rightNode() && data.leftNode()) || (!data.rightNode() && !data.leftNode()));

    m_created = true;

    *item = data;

    Q_ASSERT((item->rightNode() && item->leftNode()) || (!item->rightNode() && !item->leftNode()));

#ifdef DEBUG_SETREPOSITORY
    //Make sure we split at the correct split position
    if (item->hasSlaves()) {
        uint split = splitPositionForRange(data.start, data.end);
        const SetNodeData* left = repository.itemFromIndex(item->leftNode());
        const SetNodeData* right = repository.itemFromIndex(item->rightNode());
        Q_ASSERT(split >= left->end() && split <= right->start());
    }
#endif
    if (!data.leftNode() && setRepository) {
        for (uint a = item->start(); a < item->end(); ++a)
            setRepository->itemAddedToSets(a);
    }
}

bool SetNodeDataRequest::equals(const SetNodeData* item) const
{
    Q_ASSERT((item->rightNode() && item->leftNode()) || (!item->rightNode() && !item->leftNode()));
    //Just compare child nodes, since data must be correctly split, this is perfectly ok
    //Since this happens in very tight loops, we don't call an additional function here, but just do the check.
    return item->leftNode() == data.leftNode() && item->rightNode() == data.rightNode() &&
           item->start() == data.start() && item->end() == data.end();
}

Set::Set()
{
}

Set::~Set()
{
}

unsigned int Set::count() const
{
    if (!m_repository || !m_tree)
        return 0;
    QMutexLocker lock(m_repository->m_mutex);

    SetRepositoryAlgorithms alg(m_repository->m_dataRepository, m_repository);
    return alg.count(m_repository->m_dataRepository.itemFromIndex(m_tree));
}

Set::Set(uint treeNode, BasicSetRepository* repository) : m_tree(treeNode)
    , m_repository(repository)
{
}

Set::Set(const Set& rhs)
{
    m_repository = rhs.m_repository;
    m_tree = rhs.m_tree;
}

Set& Set::operator=(const Set& rhs)
{
    m_repository = rhs.m_repository;
    m_tree = rhs.m_tree;
    return *this;
}

QString Set::dumpDotGraph() const
{
    if (!m_repository || !m_tree)
        return QString();

    QMutexLocker lock(m_repository->m_mutex);

    SetRepositoryAlgorithms alg(m_repository->m_dataRepository, m_repository);
    return alg.dumpDotGraph(m_tree);
}

Index SetRepositoryAlgorithms::count(const SetNodeData* node) const
{
    if (node->leftNode() && node->rightNode())
        return count(getLeftNode(node)) + count(getRightNode(node));
    else
        return node->end() - node->start();
}

void SetRepositoryAlgorithms::localCheck(const SetNodeData* ifDebug(node))
{
//   Q_ASSERT(node->start() > 0);
    Q_ASSERT(node->start() < node->end());
    Q_ASSERT((node->leftNode() && node->rightNode()) || (!node->leftNode() && !node->rightNode()));
    Q_ASSERT(!node->leftNode() ||
             (getLeftNode(node())->start() == node->start() && getRightNode(node)->end() == node->end()));
    Q_ASSERT(!node->leftNode() || (getLeftNode(node())->end() <= getRightNode(node)->start()));
}

void SetRepositoryAlgorithms::check(uint node)
{
    if (!node)
        return;

    check(nodeFromIndex(node));
}

void SetRepositoryAlgorithms::check(const SetNodeData* node)
{
    localCheck(node);
    if (node->leftNode())
        check(getLeftNode(node));
    if (node->rightNode())
        check(getRightNode(node));
//  CHECK_SPLIT_POSITION(*node); Re-enable this
}

QString SetRepositoryAlgorithms::shortLabel(const SetNodeData& node) const
{
    return QStringLiteral("n%1_%2").arg(node.start()).arg(node.end());
}

QString SetRepositoryAlgorithms::dumpDotGraphInternal(uint nodeIndex, bool master) const
{
    if (!nodeIndex)
        return QStringLiteral("empty node");

    const SetNodeData& node(*repository.itemFromIndex(nodeIndex));

    QString color = QStringLiteral("blue");
    if (master)
        color = QStringLiteral("red");

    QString label = QStringLiteral("%1 -> %2").arg(node.start()).arg(node.end());
    if (!node.contiguous())
        label += QLatin1String(", with gaps");

    QString ret = QStringLiteral("%1[label=\"%2\", color=\"%3\"];\n").arg(shortLabel(node), label, color);

    if (node.leftNode()) {
        const SetNodeData& left(*repository.itemFromIndex(node.leftNode()));
        const SetNodeData& right(*repository.itemFromIndex(node.rightNode()));
        Q_ASSERT(node.rightNode());
        ret += QStringLiteral("%1 -> %2;\n").arg(shortLabel(node), shortLabel(left));
        ret += QStringLiteral("%1 -> %2;\n").arg(shortLabel(node), shortLabel(right));
        ret += dumpDotGraphInternal(node.leftNode());
        ret += dumpDotGraphInternal(node.rightNode());
    }

    return ret;
}

QString SetRepositoryAlgorithms::dumpDotGraph(uint nodeIndex) const
{
    QString ret = QStringLiteral("digraph Repository {\n");
    ret += dumpDotGraphInternal(nodeIndex, true);
    ret += QLatin1String("}\n");
    return ret;
}

const int nodeStackAlloc = 500;

class Set::IteratorPrivate
{
public:
    IteratorPrivate()
    {
        nodeStackData.resize(nodeStackAlloc);
        nodeStack = nodeStackData.data();
    }

    IteratorPrivate(const Set::IteratorPrivate& rhs)
        : nodeStackData(rhs.nodeStackData)
        , nodeStackSize(rhs.nodeStackSize)
        , currentIndex(rhs.currentIndex)
        , repository(rhs.repository)
    {
        nodeStack = nodeStackData.data();
    }

    Set::IteratorPrivate& operator=(const Set::IteratorPrivate& rhs)
    {
        nodeStackData = rhs.nodeStackData;
        nodeStackSize = rhs.nodeStackSize;
        currentIndex = rhs.currentIndex;
        repository = rhs.repository;
        nodeStack = nodeStackData.data();

        return *this;
    }

    void resizeNodeStack()
    {
        nodeStackData.resize(nodeStackSize + 1);
        nodeStack = nodeStackData.data();
    }

    KDevVarLengthArray<const SetNodeData*, nodeStackAlloc> nodeStackData;
    const SetNodeData** nodeStack;
    int nodeStackSize = 0;
    Index currentIndex = 0;
    BasicSetRepository* repository = nullptr;

    /**
     * Pushes the noed on top of the stack, changes currentIndex, and goes as deep as necessary for iteration.
     * */
    void startAtNode(const SetNodeData* node)
    {
        Q_ASSERT(node->start() != node->end());
        currentIndex = node->start();

        do {
            nodeStack[nodeStackSize++] = node;

            if (nodeStackSize >= nodeStackAlloc)
                resizeNodeStack();

            if (node->contiguous())
                break; //We need no finer granularity, because the range is contiguous
            node = Set::Iterator::getDataRepository(repository).itemFromIndex(node->leftNode());
        } while (node);
        Q_ASSERT(currentIndex >= nodeStack[0]->start());
    }
};

std::set<Index> Set::stdSet() const
{
    Set::Iterator it = iterator();
    std::set<Index> ret;

    while (it) {
        Q_ASSERT(ret.find(*it) == ret.end());
        ret.insert(*it);
        ++it;
    }

    return ret;
}

Set::Iterator::Iterator(const Iterator& rhs)
    : d_ptr(new Set::IteratorPrivate(*rhs.d_ptr))
{
}

Set::Iterator& Set::Iterator::operator=(const Iterator& rhs)
{
    *d_ptr = *rhs.d_ptr;
    return *this;
}

Set::Iterator::Iterator()
    : d_ptr(new Set::IteratorPrivate)
{
}

Set::Iterator::~Iterator() = default;

Set::Iterator::operator bool() const
{
    Q_D(const Iterator);

    return d->nodeStackSize;
}

Set::Iterator& Set::Iterator::operator++()
{
    Q_D(Iterator);

    Q_ASSERT(d->nodeStackSize);

    if (d->repository->m_mutex)
        d->repository->m_mutex->lock();

    ++d->currentIndex;

    //const SetNodeData** currentNode = &d->nodeStack[d->nodeStackSize - 1];
    if (d->currentIndex >= d->nodeStack[d->nodeStackSize - 1]->end()) {
        //Advance to the next node
        while (d->nodeStackSize && d->currentIndex >= d->nodeStack[d->nodeStackSize - 1]->end()) {
            --d->nodeStackSize;
        }

        if (!d->nodeStackSize) {
            //ready
        } else {
            //++d->nodeStackSize;
            //We were iterating the left slave of the node, now continue with the right.
            ifDebug(const SetNodeData& left =
                        *d->repository->m_dataRepository.itemFromIndex(
                            d->nodeStack[d->nodeStackSize - 1]->leftNode()); Q_ASSERT(left.end == d->currentIndex); )

            const SetNodeData& right = *d->repository->m_dataRepository.itemFromIndex(
                d->nodeStack[d->nodeStackSize - 1]->rightNode());

            d->startAtNode(&right);
        }
    }

    Q_ASSERT(d->nodeStackSize == 0 || d->currentIndex < d->nodeStack[0]->end());

    if (d->repository->m_mutex)
        d->repository->m_mutex->unlock();

    return *this;
}

BasicSetRepository::Index Set::Iterator::operator*() const
{
    Q_D(const Iterator);

    return d->currentIndex;
}

Set::Iterator Set::iterator() const
{
    if (!m_tree || !m_repository)
        return Iterator();

    QMutexLocker lock(m_repository->m_mutex);

    Iterator ret;
    ret.d_ptr->repository = m_repository;

    if (m_tree)
        ret.d_ptr->startAtNode(m_repository->m_dataRepository.itemFromIndex(m_tree));
    return ret;
}

//Creates a set item with the given children., they must be valid, and they must be split around their split-position.
uint SetRepositoryAlgorithms::createSetFromNodes(uint leftNode, uint rightNode, const SetNodeData* left,
                                                 const SetNodeData* right)
{
    if (!left)
        left = nodeFromIndex(leftNode);
    if (!right)
        right = nodeFromIndex(rightNode);

    Q_ASSERT(left->end() <= right->start());

    SetNodeData set(left->start(), right->end(), leftNode, rightNode);

    Q_ASSERT(set.start() < set.end());

    uint ret = repository.index(SetNodeDataRequest(&set, repository, setRepository));
    Q_ASSERT(set.leftNode() >= 0x10000);
    Q_ASSERT(set.rightNode() >= 0x10000);
    Q_ASSERT(ret == repository.findIndex(SetNodeDataRequest(&set, repository, setRepository)));
    ifDebug(check(ret));
    return ret;
}

//Constructs a set node from the given two sub-nodes. Those must be valid, they must not intersect, and they must have a correct split-hierarchy.
//The do not need to be split around their computed split-position.
uint SetRepositoryAlgorithms::computeSetFromNodes(uint leftNode, uint rightNode, const SetNodeData* left,
                                                  const SetNodeData* right, uchar splitBit)
{
    Q_ASSERT(left->end() <= right->start());
    uint splitPosition = splitPositionForRange(left->start(), right->end(), splitBit);

    Q_ASSERT(splitPosition);

    if (splitPosition < left->end()) {
        //The split-position intersects the left node
        uint leftLeftNode = left->leftNode();
        uint leftRightNode = left->rightNode();

        const SetNodeData* leftLeft = this->getLeftNode(left);
        const SetNodeData* leftRight = this->getRightNode(left);

        Q_ASSERT(splitPosition >= leftLeft->end() && splitPosition <= leftRight->start());

        //Create a new set from leftLeft, and from leftRight + right. That set will have the correct split-position.
        uint newRightNode = computeSetFromNodes(leftRightNode, rightNode, leftRight, right, splitBit);

        return createSetFromNodes(leftLeftNode, newRightNode, leftLeft);
    } else if (splitPosition > right->start()) {
        //The split-position intersects the right node
        uint rightLeftNode = right->leftNode();
        uint rightRightNode = right->rightNode();

        const SetNodeData* rightLeft = this->getLeftNode(right);
        const SetNodeData* rightRight = this->getRightNode(right);

        Q_ASSERT(splitPosition >= rightLeft->end() && splitPosition <= rightRight->start());

        //Create a new set from left + rightLeft, and from rightRight. That set will have the correct split-position.
        uint newLeftNode = computeSetFromNodes(leftNode, rightLeftNode, left, rightLeft, splitBit);

        return createSetFromNodes(newLeftNode, rightRightNode, nullptr, rightRight);
    } else {
        return createSetFromNodes(leftNode, rightNode, left, right);
    }
}

uint SetRepositoryAlgorithms::set_union(uint firstNode, uint secondNode, const SetNodeData* first,
                                        const SetNodeData* second, uchar splitBit)
{
    if (firstNode == secondNode)
        return firstNode;

    uint firstStart = first->start(), secondEnd = second->end();

    if (firstStart >= secondEnd)
        return computeSetFromNodes(secondNode, firstNode, second, first, splitBit);

    uint firstEnd = first->end(), secondStart = second->start();

    if (secondStart >= firstEnd)
        return computeSetFromNodes(firstNode, secondNode, first, second, splitBit);

    //The ranges of first and second do intersect

    uint newStart = firstStart < secondStart ? firstStart : secondStart;
    uint newEnd = firstEnd > secondEnd ? firstEnd : secondEnd;

    //Compute the split-position for the resulting merged node
    uint splitPosition = splitPositionForRange(newStart, newEnd, splitBit);

    //Since the ranges overlap, we can be sure that either first or second contain splitPosition.
    //The node that contains it, will also be split by it.

    if (splitPosition > firstStart && splitPosition < firstEnd && splitPosition > secondStart &&
        splitPosition < secondEnd) {
        //The split-position intersect with both first and second. Continue the union on both sides of the split-position, and merge it.

        uint firstLeftNode = first->leftNode();
        uint firstRightNode = first->rightNode();
        uint secondLeftNode = second->leftNode();
        uint secondRightNode = second->rightNode();

        const SetNodeData* firstLeft = repository.itemFromIndex(firstLeftNode);
        const SetNodeData* firstRight = repository.itemFromIndex(firstRightNode);
        const SetNodeData* secondLeft = repository.itemFromIndex(secondLeftNode);
        const SetNodeData* secondRight = repository.itemFromIndex(secondRightNode);

        Q_ASSERT(splitPosition >= firstLeft->end() && splitPosition <= firstRight->start());
        Q_ASSERT(splitPosition >= secondLeft->end() && splitPosition <= secondRight->start());

        return createSetFromNodes(set_union(firstLeftNode, secondLeftNode, firstLeft, secondLeft, splitBit),
                                  set_union(firstRightNode, secondRightNode, firstRight, secondRight, splitBit));
    } else if (splitPosition > firstStart && splitPosition < firstEnd) {
        uint firstLeftNode = first->leftNode();
        uint firstRightNode = first->rightNode();

        const SetNodeData* firstLeft = repository.itemFromIndex(firstLeftNode);
        const SetNodeData* firstRight = repository.itemFromIndex(firstRightNode);

        Q_ASSERT(splitPosition >= firstLeft->end() && splitPosition <= firstRight->start());

        //splitPosition does not intersect second. That means that second is completely on one side of it.
        //So we only need to union that side of first with second.

        if (secondEnd <= splitPosition) {
            return createSetFromNodes(set_union(firstLeftNode, secondNode, firstLeft, second,
                                                splitBit), firstRightNode, nullptr, firstRight);
        } else {
            Q_ASSERT(secondStart >= splitPosition);
            return createSetFromNodes(firstLeftNode, set_union(firstRightNode, secondNode, firstRight, second,
                                                               splitBit), firstLeft);
        }
    } else if (splitPosition > secondStart && splitPosition < secondEnd) {
        uint secondLeftNode = second->leftNode();
        uint secondRightNode = second->rightNode();

        const SetNodeData* secondLeft = repository.itemFromIndex(secondLeftNode);
        const SetNodeData* secondRight = repository.itemFromIndex(secondRightNode);

        Q_ASSERT(splitPosition >= secondLeft->end() && splitPosition <= secondRight->start());

        if (firstEnd <= splitPosition) {
            return createSetFromNodes(set_union(secondLeftNode, firstNode, secondLeft, first,
                                                splitBit), secondRightNode, nullptr, secondRight);
        } else {
            Q_ASSERT(firstStart >= splitPosition);
            return createSetFromNodes(secondLeftNode, set_union(secondRightNode, firstNode, secondRight, first,
                                                                splitBit), secondLeft);
        }
    } else {
        //We would have stopped earlier of first and second don't intersect
        ifDebug(uint test = repository.findIndex(SetNodeDataRequest(first, repository, setRepository)); qCDebug(
                    LANGUAGE) << "found index:" << test; )
        Q_ASSERT(0);
        return 0;
    }
}

bool SetRepositoryAlgorithms::set_equals(const SetNodeData* lhs, const SetNodeData* rhs)
{
    if (lhs->leftNode() != rhs->leftNode() || lhs->rightNode() != rhs->rightNode())
        return false;
    else
        return true;
}

uint SetRepositoryAlgorithms::set_intersect(uint firstNode, uint secondNode, const SetNodeData* first,
                                            const SetNodeData* second, uchar splitBit)
{
    if (firstNode == secondNode)
        return firstNode;

    if (first->start() >= second->end())
        return 0;

    if (second->start() >= first->end())
        return 0;

    //The ranges of first and second do intersect
    uint firstStart = first->start(), firstEnd = first->end(), secondStart = second->start(), secondEnd = second->end();

    uint newStart = firstStart < secondStart ? firstStart : secondStart;
    uint newEnd = firstEnd > secondEnd ? firstEnd : secondEnd;

    //Compute the split-position for the resulting merged node
    uint splitPosition = splitPositionForRange(newStart, newEnd, splitBit);

    //Since the ranges overlap, we can be sure that either first or second contain splitPosition.
    //The node that contains it, will also be split by it.

    if (splitPosition > firstStart && splitPosition < firstEnd && splitPosition > secondStart &&
        splitPosition < secondEnd) {
        //The split-position intersect with both first and second. Continue the intersection on both sides

        uint firstLeftNode = first->leftNode();
        uint firstRightNode = first->rightNode();

        uint secondLeftNode = second->leftNode();
        uint secondRightNode = second->rightNode();

        const SetNodeData* firstLeft = repository.itemFromIndex(firstLeftNode);
        const SetNodeData* firstRight = repository.itemFromIndex(firstRightNode);
        const SetNodeData* secondLeft = repository.itemFromIndex(secondLeftNode);
        const SetNodeData* secondRight = repository.itemFromIndex(secondRightNode);

        Q_ASSERT(splitPosition >= firstLeft->end() && splitPosition <= firstRight->start());
        Q_ASSERT(splitPosition >= secondLeft->end() && splitPosition <= secondRight->start());

        uint newLeftNode = set_intersect(firstLeftNode, secondLeftNode, firstLeft, secondLeft, splitBit);
        uint newRightNode = set_intersect(firstRightNode, secondRightNode, firstRight, secondRight, splitBit);

        if (newLeftNode && newRightNode)
            return createSetFromNodes(newLeftNode, newRightNode);
        else if (newLeftNode)
            return newLeftNode;
        else
            return newRightNode;
    } else if (splitPosition > firstStart && splitPosition < firstEnd) {
        uint firstLeftNode = first->leftNode();
        uint firstRightNode = first->rightNode();

        const SetNodeData* firstLeft = repository.itemFromIndex(firstLeftNode);
        const SetNodeData* firstRight = repository.itemFromIndex(firstRightNode);

        Q_ASSERT(splitPosition >= firstLeft->end() && splitPosition <= firstRight->start());

        //splitPosition does not intersect second. That means that second is completely on one side of it.
        //So we can completely ignore the other side of first.

        if (secondEnd <= splitPosition) {
            return set_intersect(firstLeftNode, secondNode, firstLeft, second, splitBit);
        } else {
            Q_ASSERT(secondStart >= splitPosition);
            return set_intersect(firstRightNode, secondNode, firstRight, second, splitBit);
        }
    } else if (splitPosition > secondStart && splitPosition < secondEnd) {
        uint secondLeftNode = second->leftNode();
        uint secondRightNode = second->rightNode();

        const SetNodeData* secondLeft = repository.itemFromIndex(secondLeftNode);
        const SetNodeData* secondRight = repository.itemFromIndex(secondRightNode);

        Q_ASSERT(splitPosition >= secondLeft->end() && splitPosition <= secondRight->start());

        if (firstEnd <= splitPosition) {
            return set_intersect(secondLeftNode, firstNode, secondLeft, first, splitBit);
        } else {
            Q_ASSERT(firstStart >= splitPosition);
            return set_intersect(secondRightNode, firstNode, secondRight, first, splitBit);
        }
    } else {
        //We would have stopped earlier of first and second don't intersect
        Q_ASSERT(0);
        return 0;
    }
    Q_ASSERT(0);
}

bool SetRepositoryAlgorithms::set_contains(const SetNodeData* node, Index index)
{
    while (true) {
        if (node->start() > index || node->end() <= index)
            return false;

        if (node->contiguous())
            return true;

        const SetNodeData* leftNode = nodeFromIndex(node->leftNode());

        if (index < leftNode->end())
            node = leftNode;
        else {
            const SetNodeData* rightNode = nodeFromIndex(node->rightNode());
            node = rightNode;
        }
    }

    return false;
}

uint SetRepositoryAlgorithms::set_subtract(uint firstNode, uint secondNode, const SetNodeData* first,
                                           const SetNodeData* second, uchar splitBit)
{
    if (firstNode == secondNode)
        return 0;

    if (first->start() >= second->end() || second->start() >= first->end())
        return firstNode;

    //The ranges of first and second do intersect
    uint firstStart = first->start(), firstEnd = first->end(), secondStart = second->start(), secondEnd = second->end();

    uint newStart = firstStart < secondStart ? firstStart : secondStart;
    uint newEnd = firstEnd > secondEnd ? firstEnd : secondEnd;

    //Compute the split-position for the resulting merged node
    uint splitPosition = splitPositionForRange(newStart, newEnd, splitBit);

    //Since the ranges overlap, we can be sure that either first or second contain splitPosition.
    //The node that contains it, will also be split by it.

    if (splitPosition > firstStart && splitPosition < firstEnd && splitPosition > secondStart &&
        splitPosition < secondEnd) {
        //The split-position intersect with both first and second. Continue the subtract on both sides of the split-position, and merge it.

        uint firstLeftNode = first->leftNode();
        uint firstRightNode = first->rightNode();

        uint secondLeftNode = second->leftNode();
        uint secondRightNode = second->rightNode();

        const SetNodeData* firstLeft = repository.itemFromIndex(firstLeftNode);
        const SetNodeData* firstRight = repository.itemFromIndex(firstRightNode);
        const SetNodeData* secondLeft = repository.itemFromIndex(secondLeftNode);
        const SetNodeData* secondRight = repository.itemFromIndex(secondRightNode);

        Q_ASSERT(splitPosition >= firstLeft->end() && splitPosition <= firstRight->start());
        Q_ASSERT(splitPosition >= secondLeft->end() && splitPosition <= secondRight->start());

        uint newLeftNode = set_subtract(firstLeftNode, secondLeftNode, firstLeft, secondLeft, splitBit);
        uint newRightNode = set_subtract(firstRightNode, secondRightNode, firstRight, secondRight, splitBit);

        if (newLeftNode && newRightNode)
            return createSetFromNodes(newLeftNode, newRightNode);
        else if (newLeftNode)
            return newLeftNode;
        else
            return newRightNode;
    } else if (splitPosition > firstStart && splitPosition < firstEnd) {
//    Q_ASSERT(splitPosition >= firstLeft->end() && splitPosition <= firstRight->start());

        uint firstLeftNode = first->leftNode();
        uint firstRightNode = first->rightNode();

        const SetNodeData* firstLeft = repository.itemFromIndex(firstLeftNode);
        const SetNodeData* firstRight = repository.itemFromIndex(firstRightNode);

        //splitPosition does not intersect second. That means that second is completely on one side of it.
        //So we only need to subtract that side of first with second.

        uint newLeftNode = firstLeftNode, newRightNode = firstRightNode;

        if (secondEnd <= splitPosition) {
            newLeftNode = set_subtract(firstLeftNode, secondNode, firstLeft, second, splitBit);
        } else {
            Q_ASSERT(secondStart >= splitPosition);
            newRightNode = set_subtract(firstRightNode, secondNode, firstRight, second, splitBit);
        }

        if (newLeftNode && newRightNode)
            return createSetFromNodes(newLeftNode, newRightNode);
        else if (newLeftNode)
            return newLeftNode;
        else
            return newRightNode;
    } else if (splitPosition > secondStart && splitPosition < secondEnd) {
        uint secondLeftNode = second->leftNode();
        uint secondRightNode = second->rightNode();

        const SetNodeData* secondLeft = repository.itemFromIndex(secondLeftNode);
        const SetNodeData* secondRight = repository.itemFromIndex(secondRightNode);

        Q_ASSERT(splitPosition >= secondLeft->end() && splitPosition <= secondRight->start());

        if (firstEnd <= splitPosition) {
            return set_subtract(firstNode, secondLeftNode, first, secondLeft, splitBit);
        } else {
            Q_ASSERT(firstStart >= splitPosition);
            return set_subtract(firstNode, secondRightNode, first, secondRight, splitBit);
        }
    } else {
        //We would have stopped earlier of first and second don't intersect
        Q_ASSERT(0);
        return 0;
    }
    Q_ASSERT(0);
}

Set BasicSetRepository::createSetFromIndices(const std::vector<Index>& indices)
{
    QMutexLocker lock(m_mutex);

    if (indices.empty())
        return Set();

    SetRepositoryAlgorithms alg(m_dataRepository, this);

    return Set(alg.setForIndices(indices.begin(), indices.end()), this);
}

Set BasicSetRepository::createSet(Index i)
{
    QMutexLocker lock(m_mutex);
    SetNodeData data(i, i + 1);

    return Set(m_dataRepository.index(SetNodeDataRequest(&data, m_dataRepository, this)), this);
}

Set BasicSetRepository::createSet(const std::set<Index>& indices)
{
    if (indices.empty())
        return Set();

    QMutexLocker lock(m_mutex);

    std::vector<Index> indicesVector;
    indicesVector.reserve(indices.size());

    for (unsigned int index : indices) {
        indicesVector.push_back(index);
    }

    return createSetFromIndices(indicesVector);
}

BasicSetRepository::BasicSetRepository(const QString& name, KDevelop::ItemRepositoryRegistry* registry,
                                       bool delayedDeletion)
    : m_dataRepository(this, name, registry)
    , m_mutex(nullptr)
    , m_delayedDeletion(delayedDeletion)
{
    m_mutex = m_dataRepository.mutex();
}

struct StatisticsVisitor
{
    explicit StatisticsVisitor(const SetDataRepository& _rep) : nodeCount(0)
        , badSplitNodeCount(0)
        , zeroRefCountNodes(0)
        , rep(_rep)
    {
    }
    bool operator()(const SetNodeData* item)
    {
        if (item->m_refCount == 0)
            ++zeroRefCountNodes;
        ++nodeCount;
        uint split = splitPositionForRange(item->start(), item->end());
        if (item->hasSlaves())
            if (split < rep.itemFromIndex(item->leftNode())->end() ||
                split > rep.itemFromIndex(item->rightNode())->start())
                ++badSplitNodeCount;
        return true;
    }
    uint nodeCount;
    uint badSplitNodeCount;
    uint zeroRefCountNodes;
    const SetDataRepository& rep;
};

void BasicSetRepository::printStatistics() const
{
    StatisticsVisitor stats(m_dataRepository);
    m_dataRepository.visitAllItems<StatisticsVisitor>(stats);
    qCDebug(LANGUAGE) << "count of nodes:" << stats.nodeCount << "count of nodes with bad split:" <<
        stats.badSplitNodeCount << "count of nodes with zero reference-count:" << stats.zeroRefCountNodes;
}

BasicSetRepository::~BasicSetRepository() = default;

void BasicSetRepository::itemRemovedFromSets(uint /*index*/)
{
}

void BasicSetRepository::itemAddedToSets(uint /*index*/)
{
}

////////////Set convenience functions//////////////////

bool Set::contains(Index index) const
{
    if (!m_tree || !m_repository)
        return false;

    QMutexLocker lock(m_repository->m_mutex);

    SetRepositoryAlgorithms alg(m_repository->m_dataRepository, m_repository);
    return alg.set_contains(m_repository->m_dataRepository.itemFromIndex(m_tree), index);
}

Set Set::operator +(const Set& first) const
{
    if (!first.m_tree)
        return *this;
    else if (!m_tree || !m_repository)
        return first;

    Q_ASSERT(m_repository == first.m_repository);

    QMutexLocker lock(m_repository->m_mutex);

    SetRepositoryAlgorithms alg(m_repository->m_dataRepository, m_repository);

    uint retNode = alg.set_union(m_tree, first.m_tree, m_repository->m_dataRepository.itemFromIndex(
                                     m_tree), m_repository->m_dataRepository.itemFromIndex(first.m_tree));

    ifDebug(alg.check(retNode));

    return Set(retNode, m_repository);
}

Set& Set::operator +=(const Set& first)
{
    if (!first.m_tree)
        return *this;
    else if (!m_tree || !m_repository) {
        m_tree = first.m_tree;
        m_repository = first.m_repository;
        return *this;
    }

    QMutexLocker lock(m_repository->m_mutex);

    SetRepositoryAlgorithms alg(m_repository->m_dataRepository, m_repository);

    m_tree = alg.set_union(m_tree, first.m_tree, m_repository->m_dataRepository.itemFromIndex(
                               m_tree), m_repository->m_dataRepository.itemFromIndex(first.m_tree));

    ifDebug(alg.check(m_tree));
    return *this;
}

Set Set::operator &(const Set& first) const
{
    if (!first.m_tree || !m_tree)
        return Set();

    Q_ASSERT(m_repository);

    QMutexLocker lock(m_repository->m_mutex);

    SetRepositoryAlgorithms alg(m_repository->m_dataRepository, m_repository);

    Set ret(alg.set_intersect(m_tree, first.m_tree, m_repository->m_dataRepository.itemFromIndex(
                                  m_tree), m_repository->m_dataRepository.itemFromIndex(first.m_tree)), m_repository);

    ifDebug(alg.check(ret.m_tree));

    return ret;
}

Set& Set::operator &=(const Set& first)
{
    if (!first.m_tree || !m_tree) {
        m_tree = 0;
        return *this;
    }

    Q_ASSERT(m_repository);

    QMutexLocker lock(m_repository->m_mutex);

    SetRepositoryAlgorithms alg(m_repository->m_dataRepository, m_repository);

    m_tree = alg.set_intersect(m_tree, first.m_tree, m_repository->m_dataRepository.itemFromIndex(
                                   m_tree), m_repository->m_dataRepository.itemFromIndex(first.m_tree));
    ifDebug(alg.check(m_tree));
    return *this;
}

Set Set::operator -(const Set& rhs) const
{
    if (!m_tree || !rhs.m_tree)
        return *this;

    Q_ASSERT(m_repository);

    QMutexLocker lock(m_repository->m_mutex);

    SetRepositoryAlgorithms alg(m_repository->m_dataRepository, m_repository);

    Set ret(alg.set_subtract(m_tree, rhs.m_tree, m_repository->m_dataRepository.itemFromIndex(
                                 m_tree), m_repository->m_dataRepository.itemFromIndex(rhs.m_tree)), m_repository);
    ifDebug(alg.check(ret.m_tree));
    return ret;
}

Set& Set::operator -=(const Set& rhs)
{
    if (!m_tree || !rhs.m_tree)
        return *this;

    Q_ASSERT(m_repository);

    QMutexLocker lock(m_repository->m_mutex);

    SetRepositoryAlgorithms alg(m_repository->m_dataRepository, m_repository);

    m_tree = alg.set_subtract(m_tree, rhs.m_tree, m_repository->m_dataRepository.itemFromIndex(
                                  m_tree), m_repository->m_dataRepository.itemFromIndex(rhs.m_tree));

    ifDebug(alg.check(m_tree));
    return *this;
}

BasicSetRepository* Set::repository() const
{
    return m_repository;
}

void Set::staticRef()
{
    if (!m_tree)
        return;

    QMutexLocker lock(m_repository->m_mutex);
    SetNodeData* data = m_repository->m_dataRepository.dynamicItemFromIndexSimple(m_tree);
    ++data->m_refCount;
}

///Mutex must be locked
void Set::unrefNode(uint current)
{
    SetNodeData* data = m_repository->m_dataRepository.dynamicItemFromIndexSimple(current);
    Q_ASSERT(data->m_refCount);
    --data->m_refCount;
    if (!m_repository->delayedDeletion()) {
        if (data->m_refCount == 0) {
            if (data->leftNode()) {
                Q_ASSERT(data->rightNode());
                unrefNode(data->rightNode());
                unrefNode(data->leftNode());
            } else {
                //Deleting a leaf
                Q_ASSERT(data->end() - data->start() == 1);
                m_repository->itemRemovedFromSets(data->start());
            }

            m_repository->m_dataRepository.deleteItem(current);
        }
    }
}

///Decrease the static reference-count of this set by one. This set must have a reference-count > 1.
///If this set reaches the reference-count zero, it will be deleted, and all sub-nodes that also reach the reference-count zero
///will be deleted as well. @warning Either protect ALL your sets by using reference-counting, or don't use it at all.
void Set::staticUnref()
{
    if (!m_tree)
        return;

    QMutexLocker lock(m_repository->m_mutex);

    unrefNode(m_tree);
}

StringSetRepository::StringSetRepository(const QString& name) : Utils::BasicSetRepository(name)
{
}

void StringSetRepository::itemRemovedFromSets(uint index)
{
    ///Call the IndexedString destructor with enabled reference-counting
    KDevelop::IndexedString string = KDevelop::IndexedString::fromIndex(index);

    KDevelop::enableDUChainReferenceCounting(&string, sizeof(KDevelop::IndexedString));
    string.~IndexedString(); //Call destructor with enabled reference-counting
    KDevelop::disableDUChainReferenceCounting(&string);
}

void StringSetRepository::itemAddedToSets(uint index)
{
    ///Call the IndexedString constructor with enabled reference-counting

    KDevelop::IndexedString string = KDevelop::IndexedString::fromIndex(index);

    char data[sizeof(KDevelop::IndexedString)];

    KDevelop::enableDUChainReferenceCounting(data, sizeof(KDevelop::IndexedString));
    new (data) KDevelop::IndexedString(string); //Call constructor with enabled reference-counting
    KDevelop::disableDUChainReferenceCounting(data);
}
}