forked from jasp-stats/jasp-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolumn.cpp
2409 lines (1815 loc) · 62.1 KB
/
column.cpp
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
#include "log.h"
#include "column.h"
#include "timers.h"
#include "dataset.h"
#include "columnutils.h"
#include "databaseinterface.h"
#include <cassert>
bool Column::_autoSortByValuesByDefault = true;
bool Column::autoSortByValuesByDefault()
{
return _autoSortByValuesByDefault;
}
void Column::setAutoSortByValuesByDefault(bool autoSort)
{
_autoSortByValuesByDefault = autoSort;
}
Column::Column(DataSet * data, int id)
: DataSetBaseNode(dataSetBaseNodeType::column, data->dataNode()),
_data( data),
_id( id),
_emptyValues( new EmptyValues(data->emptyValues())),
_doubleDummy( new Label(this)),
_autoSortByValue( _autoSortByValuesByDefault)
{
if(_id != -1)
db().columnSetAutoSort(_id, _autoSortByValue); //Store autosort in db
}
Column::~Column()
{
labelsTempReset();
delete _emptyValues;
delete _doubleDummy;
}
void Column::dbCreate(int index)
{
JASPTIMER_SCOPE(Column::dbCreate);
assert(_id == -1);
db().columnInsert(_id, index);
}
void Column::dbLoad(int id, bool getValues)
{
JASPTIMER_SCOPE(Column::dbLoad);
assert(_id == id || (id != -1 && _id == -1) || _id != -1);
if(id != -1)
_id = id;
db().transactionReadBegin();
Json::Value emptyVals;
db().columnGetBasicInfo( _id, _name, _title, _description, _type, _revision, emptyVals, _autoSortByValue);
db().columnGetComputedInfo( _id, _analysisId, _invalidated, _codeType, _rCode, _error, _constructorJson);
_emptyValues->fromJson(emptyVals);
_resetLabelValueMap();
db().labelsLoad(this);
if(getValues)
db().columnGetValues(_id, _ints, _dbls);
db().transactionReadEnd();
}
void Column::dbLoadIndex(int index, bool getValues)
{
JASPTIMER_SCOPE(Column::dbLoadIndex);
_id = db().columnIdForIndex(_data->id(), index);
dbLoad(_id, getValues);
}
void Column::dbDelete(bool cleanUpRest)
{
assert(_id != -1);
labelsClear(false);
db().columnDelete(_id, cleanUpRest);
_id = -1;
}
void Column::loadComputedColumnJsonBackwardsCompatibly(const Json::Value & json)
{
//Columnname is used to find this:
//_column = DataSetPackage::pkg()->dataSet()->column(json["name"].asString());
const std::string & rCode = json["rCode"].asString();
setCompColStuff
(
json["invalidated"].asBool(),
false,
computedColumnTypeFromString(json["codeType"].asString()),
rCode,
json["error"].asString(),
json["constructorCode"]
);
}
void Column::invalidateDependents()
{
for(Column * col : _data->columns())
if(col->isComputed() && col->dependsOn(name()))
col->invalidate();
}
bool Column::setName(const std::string &name)
{
JASPTIMER_SCOPE(Column::setName);
if(_name == name || name.empty())
return false;
std::string orgName = _name;
_name = getUniqueName(name);
if(_title.empty() || _title == orgName)
setTitle(_name);
db().columnSetName(_id, _name);
incRevision();
return true;
}
void Column::setTitle(const std::string &title)
{
JASPTIMER_SCOPE(Column::setTitle);
if(_title == title)
return;
_title = title;
db().columnSetTitle(_id, _title);
incRevision();
}
void Column::setDescription(const std::string &description)
{
JASPTIMER_SCOPE(Column::setDescription);
if(_description == description)
return;
_description = description;
db().columnSetDescription(_id, _description);
incRevision();
}
void Column::setType(columnType colType)
{
JASPTIMER_SCOPE(Column::setType);
if(_type == colType)
return;
_type = colType;
db().columnSetType(_id, _type);
incRevision();
}
bool Column::hasCustomEmptyValues() const
{
return _emptyValues->hasEmptyValues();
}
void Column::setHasCustomEmptyValues(bool hasCustom)
{
JASPTIMER_SCOPE(Column::setHasCustomEmptyValues);
if(hasCustomEmptyValues() == hasCustom)
return;
_emptyValues->setHasCustomEmptyValues(hasCustom);
db().columnSetEmptyVals(_id, _emptyValues->toJson().toStyledString());
incRevision(false);
}
bool Column::setCustomEmptyValues(const stringset& customEmptyValues)
{
JASPTIMER_SCOPE(Column::setCustomEmptyValues);
if (_emptyValues->emptyStrings() == customEmptyValues)
return false;
_emptyValues->setEmptyValues(customEmptyValues, _emptyValues->hasEmptyValues());
db().columnSetEmptyVals(_id, _emptyValues->toJson().toStyledString());
incRevision(false);
return true;
}
void Column::dbUpdateComputedColumnStuff()
{
db().columnSetComputedInfo(_id, _analysisId, _invalidated, _codeType, _rCode, _error, constructorJsonStr());
incRevision();
}
void Column::setInvalidated(bool invalidated)
{
JASPTIMER_SCOPE(Column::setInvalidated);
if(_invalidated == invalidated)
return;
_invalidated = invalidated;
db().columnSetInvalidated(_id, _invalidated);
incRevision(false);
}
void Column::setCodeType(computedColumnType codeType)
{
JASPTIMER_SCOPE(Column::setCodeType);
if(codeType == _codeType)
return;
if(_codeType == computedColumnType::notComputed)
{
_constructorJson = Json::objectValue;
_constructorJson["formulas"] = Json::arrayValue;
}
if(_codeType == computedColumnType::analysisNotComputed)
_analysisId = -1;
_codeType = codeType;
dbUpdateComputedColumnStuff();
}
bool Column::setConstructorJson(const std::string & constructorJson)
{
Json::Value parsed;
Json::Reader().parse(constructorJson, parsed);
return setConstructorJson(parsed);
}
void Column::setAutoSortByValue(bool sort)
{
JASPTIMER_SCOPE(Column::setAutoSortByValue);
if(sort == _autoSortByValue)
return;
_autoSortByValue = sort;
db().columnSetAutoSort(_id, _autoSortByValue);
labelsHandleAutoSort();
return;
}
bool Column::setConstructorJson(const Json::Value &constructorJson)
{
JASPTIMER_SCOPE(Column::setConstructorJson);
//Log::log() << "setConstructorJson got " << constructorJson.toStyledString() << " and had: " << _constructorJson.toStyledString() << std::endl;
if(_constructorJson == constructorJson)
return false;
_constructorJson = constructorJson;
dbUpdateComputedColumnStuff();
return true;
}
bool Column::setRCode(const std::string & rCode)
{
JASPTIMER_SCOPE(Column::setRCodes);
if(_rCode == rCode)
return false;
_rCode = rCode;
findDependencies();
invalidate();
dbUpdateComputedColumnStuff();
return true;
}
bool Column::setError(const std::string & error)
{
JASPTIMER_SCOPE(Column::setError);
if(error == _error)
return false;
_error = error;
dbUpdateComputedColumnStuff();
return true;
}
void Column::setAnalysisId(int analysisId)
{
JASPTIMER_SCOPE(Column::setAnalysisId);
if(_analysisId == analysisId)
return;
_analysisId = analysisId;
dbUpdateComputedColumnStuff();
}
void Column::setIndex(int index)
{
JASPTIMER_SCOPE(Column::setIndex);
db().columnSetIndex(id(), index);
incRevision();
}
bool Column::iShouldBeSentAgain()
{
if(!invalidated())
return false;
for(Column * col : _data->columns())
if(col->isComputed() && dependsOn(col->name()) && col->invalidated())
return false;
return true;
}
void Column::setCompColStuff(bool invalidated, bool forceSourceColType, computedColumnType codeType, const std::string &rCode, const std::string &error, const Json::Value &constructorJson)
{
JASPTIMER_SCOPE(Column::setCompColStuff);
_invalidated = invalidated;
_codeType = codeType;
_rCode = rCode;
_constructorJson = constructorJson;
dbUpdateComputedColumnStuff();
}
intset Column::getUniqueLabelValues() const
{
JASPTIMER_SCOPE(Column::getUniqueLabelValues);
intset vals;
for(const Label * label : _labels)
vals.insert(label->intsId());
return vals;
}
void Column::_convertVectorIntToDouble(intvec & intValues, doublevec & doubleValues)
{
JASPTIMER_SCOPE(Column::_convertVectorIntToDouble);
doubleValues.clear();
for (int intValue : intValues)
doubleValues.push_back(intValue == EmptyValues::missingValueInteger ? EmptyValues::missingValueDouble : double(intValue));
}
columnTypeChangeResult Column::changeType(columnType colType)
{
JASPTIMER_SCOPE(Column::changeType);
if(!isComputed())
{
setType(colType);
return columnTypeChangeResult::changed;
}
else
{
if(codeType() == computedColumnType::analysis)
return columnTypeChangeResult::generatedFromAnalysis;
setDefaultValues(colType);
invalidate();
return columnTypeChangeResult::changed;
}
}
void Column::setDefaultValues(enum columnType columnType)
{
JASPTIMER_SCOPE(Column::setDefaultValues);
if(columnType != columnType::unknown)
setType(columnType);
for(size_t i=0; i<_ints.size(); i++)
{
_ints[i] = EmptyValues::missingValueInteger;
_dbls[i] = EmptyValues::missingValueDouble;
}
labelsClear();
dbUpdateValues(false);
}
void Column::dbUpdateValues(bool labelsTempCanBeMaintained)
{
if(!_data->writeBatchedToDB())
db().columnSetValues(_id, _ints, _dbls);
incRevision(labelsTempCanBeMaintained);
}
columnType Column::resetValues(int thresholdScale)
{
return setValues(valuesAsStrings(), labelsAsStrings(), thresholdScale);
}
stringset Column::mergeOldMissingDataMap(const Json::Value &missingData)
{
stringset foundEmpty;
std::map<std::string, Label*> displayToLabel; //Keep track of which labels we added because only those could possibly be derived from missingDataMap
for(size_t r=0; r<_ints.size(); r++)
{
const std::string row = std::to_string(r);
if(missingData.isMember(row))
{
double dbl = EmptyValues::missingValueDouble;
const std::string & displayValue = missingData[row].asString();
foundEmpty . insert(displayValue);
bool isDbl = ColumnUtils::getDoubleValue( displayValue, dbl);
Label * label = displayToLabel.count(displayValue)
? displayToLabel.at(displayValue) // Get previously created label, or:
: displayToLabel[displayValue] = isDbl // Be careful, we add something to the map and use the returnvalue from the assignment to also get it into `label`
? nullptr // Numbers dont need a label
: labelByIntsId(labelsAdd(displayValue)); // And here we do, because where else are we going to store that string?
_ints[r] = label ? label->intsId() : Label::DOUBLE_LABEL_VALUE;
_dbls[r] = dbl;
}
}
foundEmpty.erase(""); //So for some currently inscrutable reason empty strings were also stored in the missing data map... Remove any occurences.
return foundEmpty;
}
columnType Column::setValues(const stringvec & values, const stringvec & labels, int thresholdScale, bool * aChange)
{
JASPTIMER_SCOPE(Column::setValues);
if(aChange && _dbls.size() != values.size())
(*aChange) = true;
assert(values.size() == labels.size() || labels.size() == 0);
size_t prevSize = _ints.size();
_dbls.resize(values.size());
_ints.resize(values.size());
for(size_t resetRow=prevSize; resetRow<_ints.size(); resetRow++)
{
_ints[resetRow] = EmptyValues::missingValueInteger;
_dbls[resetRow] = EmptyValues::missingValueDouble;
}
bool onlyDoubles = true,
onlyInts = true;
//Weve already made sure we have only 1 label per value and display combo, because otherwise this will get too complicated
intset ints; // to suggest whether this is a scalar or not we need to know whether we have more than treshold ints or not.
int tmpInt;
double tmpDbl;
for(size_t i=0; i<values.size(); i++)
{
if(setValue(i, values[i], labels.size() ? labels[i] : "", false) && aChange)
(*aChange) = true;
if(values[i] != "" || (labels.size() && labels[i] != ""))
{
if(ColumnUtils::getIntValue(values[i], tmpInt))
ints.insert(tmpInt);
else
onlyInts = false;
if(!ColumnUtils::getDoubleValue(values[i], tmpDbl))
onlyDoubles = false;
}
}
if(labelsRemoveOrphans() && aChange)
(*aChange) = true;
dbUpdateValues(false);
//Now determine what the most logical columntype would be given the current values AND empty values!
if(onlyInts && ints.size() <= thresholdScale && ints.size() > 0)
{
if(ints.size() == 2) return columnType::nominal;
if(ints.size() <= thresholdScale) return columnType::ordinal;
return columnType::scale;
}
if(onlyDoubles)
return columnType::scale;
//So not everything was an int or a dbl so lets go through the current ints, count total labels and determine whether it might be nominal or ordinal
std::set<double> doublesNonNA;
std::set<Label*> labelsNonNA;
for(size_t i=0; i<_ints.size(); i++)
{
int value = _ints[i];
if(value != Label::DOUBLE_LABEL_VALUE)
{
Label * label = labelByIntsId(value);
if(label && !label->isEmptyValue())
labelsNonNA.insert(label);
}
else if(!isEmptyValue(_dbls[i]))
doublesNonNA.insert(_dbls[i]);
}
size_t howManyLabelLike = doublesNonNA.size() && labelsNonNA.size();
if(howManyLabelLike <= 2 || howManyLabelLike > thresholdScale)
return columnType::nominal;
return columnType::ordinal;
}
bool Column::setDescriptions(strstrmap labelToDescriptionMap)
{
JASPTIMER_SCOPE(Column::setDescriptions);
bool anyChange = false;
for(const auto & labelDesc : labelToDescriptionMap)
{
Label * label = labelByDisplay(labelDesc.first);
if(label)
{
anyChange = label->setDescription(labelDesc.second) || anyChange;
continue;
}
if(labelDesc.first == labelDesc.second) //If we have no label but the map doesnt make any changes then ignore it
continue;
//Maybe it looks like a double? Then we should probably change add a double?
double doubleValue = EmptyValues::missingValueDouble;
if(ColumnUtils::getDoubleValue(labelDesc.first, doubleValue) && !std::isnan(doubleValue))
{
//Lets create a label
int labelValue = labelsAdd(labelDesc.first, labelDesc.second, doubleValue);
anyChange = true;
//Now we need to make sure that the _ints category points to the label
for(size_t row=0; row<_ints.size(); row++)
if(Utils::isEqual(_dbls[row], doubleValue))
{
if(_ints[row] != Label::DOUBLE_LABEL_VALUE && _ints[row] != labelValue)
Log::log() << "Column(" << name() << ")::setDescriptions(...)\n" << "_ints[" << row << "] != Label::DOUBLE_LABEL_VALUE && _ints[" << row << "] != labelValue (" << labelValue << ")" << std::endl;
_ints[row] = labelValue;
}
}
}
return anyChange;
}
bool Column::overwriteDataAndType(stringvec data, columnType colType)
{
JASPTIMER_SCOPE(Column::overwriteDataAndType);
if(data.size() != _data->rowCount())
{
if(data.size() == _data->filter()->filteredRowCount())
{
const boolvec & filtered = _data->filter()->filtered();
stringvec newData;
newData . reserve(filtered.size());
for(size_t iFilter=0, iData=0; iFilter < filtered.size() && iData < data.size(); iFilter++)
newData.push_back(filtered[iFilter] ? data[iData++] : "");
data = newData;
}
else
data.resize(_data->rowCount());
}
bool changes = _type != colType,
toScale = colType == columnType::scale;
stringvec otherData = data,
& values = toScale ? data : otherData,
& labels = !toScale ? data : otherData;
// If we are going to allow users to edit things it would be nice if we didnt just throw it away so rough
// See: https://github.com/jasp-stats/INTERNAL-jasp/issues/2680
// All we have to do is find the value/label per label/value given depending on the selected columnType
strstrmap replacePerKey;
std::function<std::string(const std::string &)> getOther = [&](const std::string & in)
{
if(!replacePerKey.count(in))
{
Label * tmp = toScale ? labelByValue(in) : labelByDisplay(in);
replacePerKey[in] = !tmp ? in : toScale ? tmp->label() : tmp->originalValueAsString();
}
return replacePerKey.at(in);
};
for(size_t i=0; i<data.size(); i++)
otherData[i] = getOther(data[i]);
setValues(values, labels, 0, &changes);
setType(colType);
labelsTempReset();
labelsHandleAutoSort();
return changes;
}
void Column::_dbUpdateLabelOrder(bool noIncRevisionWhenBatchedPlease)
{
JASPTIMER_SCOPE(Column::_dbUpdateLabelOrder);
if(batchedLabelDepth())
{
if(!noIncRevisionWhenBatchedPlease)
incRevision();
return;
}
labelsHandleAutoSort(false);
intintmap orderPerDbIds;
_highestIntsId = 0;
for(size_t i=0; i<_labels.size(); i++)
{
_labels[i]->setOrder(i);
orderPerDbIds[_labels[i]->dbId()] = i;
_highestIntsId = std::max(_highestIntsId, _labels[i]->intsId());
}
db().labelsSetOrder(orderPerDbIds);
incRevision(false);
}
void Column::_sortLabelsByOrder()
{
std::sort(_labels.begin(), _labels.end(), [](const Label * l, const Label * r) { return l->order() < r->order(); });
}
void Column::labelsClear(bool doIncRevision)
{
db().labelsClear(_id);
_labels.clear();
_labelByIntsIdMap.clear();
_labelByValDis.clear();
_highestIntsId = 0;
if(doIncRevision)
incRevision(false);
}
void Column::beginBatchedLabelsDB()
{
_batchedLabelDepth++;
}
void Column::endBatchedLabelsDB(bool wasWritingBatch)
{
assert(_batchedLabelDepth > 0);
_batchedLabelDepth--;
for(size_t i=0; i<_labels.size(); i++)
_labels[i]->setOrder(i);
if(_batchedLabelDepth == 0)
{
if(wasWritingBatch)
{
db().labelsWrite(this);
incRevision(); //Should trigger reload at engine end
}
}
}
int Column::labelsAdd(int display)
{
return labelsAdd(std::to_string(display));
}
int Column::labelsAdd(const std::string &display)
{
JASPTIMER_SCOPE(Column::labelsAdd displaystring);
if(display == "")
return EmptyValues::missingValueInteger;
int anInt;
double aDouble;
Json::Value original = display;
if (ColumnUtils::getIntValue( display, anInt)) original = anInt;
else if (ColumnUtils::getDoubleValue( display, aDouble)) original = aDouble;
return labelsAdd(display, "", original);
}
int Column::labelsAdd(const std::string & display, const std::string & description, const Json::Value & originalValue)
{
JASPTIMER_SCOPE(Column::labelsAdd 3 args);
return labelsAdd(++_highestIntsId, display, true, description, originalValue);
}
int Column::_labelMapIt(Label * label)
{
_labelByIntsIdMap[label->intsId()] = label;
_labelByValDis[label->origValDisplay()] = label;
_highestIntsId = std::max(_highestIntsId, label->intsId());
_dbUpdateLabelOrder(true);
return label->intsId();
}
int Column::labelsAdd(int value, const std::string & display, bool filterAllows, const std::string & description, const Json::Value & originalValue, int order, int id)
{
JASPTIMER_SCOPE(Column::labelsAdd lotsa arg);
auto valDisplay = std::make_pair(Label::originalValueAsString(this, originalValue), display);
if(_labelByValDis.count(valDisplay))
return _labelByValDis.at(valDisplay)->intsId();
Label * label = new Label(this, display, value, filterAllows, description, originalValue, order, id);
_labels.push_back(label);
return _labelMapIt(label);
}
int Column::labelsSet(int labelIndex, int value, const std::string &display, bool filterAllows, const std::string &description, const Json::Value &originalValue, int order, int id)
{
JASPTIMER_SCOPE(Column::labelsSet);
Label * label = nullptr;
if(_labels.size() > labelIndex)
{
label = _labels[labelIndex];
label->setInformation(this, id, order, display, value, filterAllows, description, originalValue);
}
else
{
if(_labels.size() != labelIndex)
Log::log() << "Labels are not being set in a sensible order it seems." << std::endl;
if(id == -1)
Log::log() << "This functions expects a label to be set from the DB, so why is dbId -1?" << std::endl;
if(_labels.size() < labelIndex+1)
_labels . resize(labelIndex+1);
_labels[labelIndex] = new Label(this, display, value, filterAllows, description, originalValue, order, id);
label = _labels[labelIndex];
}
assert(label);
return _labelMapIt(label);
}
void Column::labelsRemoveByIntsId(std::set<int> valuesToRemove, bool updateOrder)
{
if (valuesToRemove.empty()) return;
JASPTIMER_SCOPE(Column::labelsRemoveValues);
_labels.erase(
std::remove_if(
_labels.begin(),
_labels.end(),
[&](Label * label) {
if(std::find(valuesToRemove.begin(), valuesToRemove.end(), label->intsId()) != valuesToRemove.end())
{
_labelByIntsIdMap.erase(label->intsId());
auto valDis = std::make_pair(label->originalValueAsString(), label->labelDisplay());
if(_labelByValDis.count(valDis) && _labelByValDis.at(valDis) == label)
_labelByValDis.erase(valDis);
label->dbDelete();
delete label;
return true;
}
return false;
}),
_labels.end());
if(updateOrder)
_dbUpdateLabelOrder();
}
strintmap Column::labelsResetValues(int & maxValue)
{
JASPTIMER_SCOPE(Column::labelsResetValues);
beginBatchedLabelsDB();
strintmap result;
int labelValue = 0;
_labelByIntsIdMap.clear();
for (Label * label : _labels)
{
if (label->intsId() != labelValue)
label->setIntsId(labelValue);
result[label->label()] = labelValue;
_labelByIntsIdMap[labelValue] = label;
labelValue++;
}
maxValue = labelValue;
_highestIntsId = maxValue;
endBatchedLabelsDB();
return result;
}
void Column::labelsRemoveBeyond(size_t desiredLabelsSize)
{
for(size_t i=desiredLabelsSize; i<_labels.size(); i++)
delete _labels[i];
if(desiredLabelsSize < _labels.size())
_labels.resize(desiredLabelsSize);
_resetLabelValueMap();
}
void Column::labelsTempReset()
{
_labelsTemp . clear();
_labelsTempDbls . clear();
_labelsTempToIndex . clear();
_labelsTempRevision = -1;
_labelsTempMaxWidth = 0;
_labelsTempNumerics = 0;
_labelByNonEmptyIndex .clear();
_labelNonEmptyIndexByLabel .clear();
nonFilteredCountersReset();
}
int Column::labelsTempCount()
{
if(_revision != _labelsTempRevision)
{
//first collect the labels that are actually Label
labelsTempReset();
_labelsTemp . reserve(_labels.size());
_labelsTempDbls . reserve(_labels.size());
size_t nonEmptyIndex = 0;
for(size_t r=0; r<_labels.size(); r++)
if(!_labels[r]->isEmptyValue())
{
_labelsTemp . push_back(_labels[r]->label());
_labelsTempDbls . push_back(_labels[r]->originalValue().isDouble() ? _labels[r]->originalValue().asDouble() : EmptyValues::missingValueDouble);
_labelsTempToIndex[_labelsTemp[_labelsTemp.size()-1]] = _labelsTemp.size()-1; //We store the index in _labelsTemp in a map.
_labelByNonEmptyIndex[nonEmptyIndex] = _labels[r];
_labelNonEmptyIndexByLabel[_labels[r]] = nonEmptyIndex;
if(!isEmptyValue(*_labelsTempDbls.rbegin()))
_labelsTempNumerics++;
nonEmptyIndex++;
}
doubleset dblset;
for(size_t r=0; r<rowCount(); r++)
{
if(!isEmptyValue(_dbls[r]) && _ints[r] == Label::DOUBLE_LABEL_VALUE)
dblset.insert(_dbls[r]);
}
doublevec dbls(dblset.begin(), dblset.end());
std::sort(dbls.begin(), dbls.end());
//There might also be "double" values that should also be shown in the editor so we go through everything and add them to _labelsTemp and _labelsTempToIndex
for(double dbl : dbls)
{
const std::string doubleLabel = doubleToDisplayString(dbl, false);
if(!doubleLabel.empty() && !_labelsTempToIndex.count(doubleLabel))
{
_labelsTemp . push_back(doubleLabel);
_labelsTempDbls . push_back(dbl);
_labelsTempToIndex[doubleLabel] = _labelsTemp.size()-1;
_labelsTempMaxWidth = std::max(_labelsTempMaxWidth, size_t(_labelsTemp[_labelsTemp.size()-1].size()));
_labelsTempNumerics ++;
}
}
//Make sure we dont do this too often be remembering at which revision we created the temp values:
_labelsTempRevision = _revision;
}
return _labelsTemp.size();
}
int Column::nonFilteredNumericsCount()
{
if (_nonFilteredNumericsCount == -1)
{
doubleset numerics;
for(size_t r=0; r<_data->rowCount(); r++)
if(_data->filter()->filtered()[r] && !isEmptyValue(_dbls[r]))
numerics.insert(_dbls[r]);
_nonFilteredNumericsCount = numerics.size();
}
return _nonFilteredNumericsCount;
}
stringvec Column::nonFilteredLevels()
{
if (_nonFilteredLevels.empty())
{
stringset levels;
for(size_t r=0; r<_data->rowCount(); r++)
if(_data->filter()->filtered()[r])
{
if(_ints[r] != Label::DOUBLE_LABEL_VALUE)
{
Label * label = labelByIntsId(_ints[r]);
if(label && !label->isEmptyValue())
levels.insert(label->label());
}
else if(!isEmptyValue(_dbls[r]))
levels.insert(ColumnUtils::doubleToString(_dbls[r]));
}
// Use the right label order
for (std::string& label : _labelsTemp)
if (levels.find(label) != levels.end())
_nonFilteredLevels.push_back(label);
}
return _nonFilteredLevels;
}
void Column::nonFilteredCountersReset()
{