World Ocean Simulation System (WOSS) library
woss-db-custom-data-container.h
Go to the documentation of this file.
1/* WOSS - World Ocean Simulation System -
2 *
3 * Copyright (C) 2009 2025 Federico Guerra
4 * and regents of the SIGNET lab, University of Padova
5 *
6 * Author: Federico Guerra - WOSS@guerra-tlc.com
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation;
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22
31
32
33#ifndef WOSS_DB_CUSTOM_DATA_CONTAINER_H
34#define WOSS_DB_CUSTOM_DATA_CONTAINER_H
35
36
37#include <map>
38#include <iostream>
39#include <time-definitions.h>
40#include <complex>
41#include <optional>
42#include <memory>
43#include <type_traits>
44
45namespace woss {
46
47 template <typename T>
48 struct is_shared_ptr : std::false_type {};
49
50 template <typename U>
51 struct is_shared_ptr<std::shared_ptr<U>> : std::true_type {};
52
53 template <typename T>
55
56
64 template < class T, class MidFunctor, class InFunctor, class Data, class OutComp = std::less<T>, class MidComp = std::less<double>, class InComp = std::less<double> >
66
67 protected:
68
72 using InnerData = typename std::map< double, Data, InComp >;
73 using CDCInnerIt = typename InnerData::iterator;
74 using CDCInnerRIt = typename InnerData::reverse_iterator;
75 using CDCInnerCIt = typename InnerData::const_iterator;
76 using CDCInnerCRIt = typename InnerData::const_reverse_iterator;
77
81 using MediumData = typename std::map< double, InnerData, MidComp >;
82 using CDCMediumIt = typename MediumData::iterator;
83 using CDCMediumCIt = typename MediumData::const_iterator;
84 using CDCMediumRIt = typename MediumData::reverse_iterator;
85 using CDCMediumCRIt = typename MediumData::const_reverse_iterator;
86
90 using CustomContainer = typename std::map< T, MediumData, OutComp >;
91 using CDCIt = typename CustomContainer::iterator;
92 using CDCRIt = typename CustomContainer::reverse_iterator;
93 using CDCCIt = typename CustomContainer::const_iterator;
94 using CDCCRIt = typename CustomContainer::const_reverse_iterator;
95
96 public:
97
98 #if __cplusplus >= 201103L // C++11 or later
99 static constexpr double DB_CDATA_ALL_MEDIUM_KEYS = -190.0;
100
101 static constexpr double DB_CDATA_ALL_INNER_KEYS = -10.0;
102 #else
103 static const double DB_CDATA_ALL_MEDIUM_KEYS = -190.0;
104
105 static const double DB_CDATA_ALL_INNER_KEYS = -10.0;
106 #endif // __cplusplus == 201103L
107
109
110
115
120
121
127 MediumData& operator[] ( const T& key ) { return data_map[key]; }
128
129
134 bool empty() const { return data_map.empty(); }
135
136
141 int size() const { return data_map.size(); }
142
143
154 const std::optional< Data > get( const T& t = DB_CDATA_ALL_OUTER_KEYS, double b = DB_CDATA_ALL_MEDIUM_KEYS, double r = DB_CDATA_ALL_INNER_KEYS ) const;
155
162 const std::optional< Data > get( const T& tx, const T& rx ) const;
163
175 bool insert( const Data& data, const T& t = DB_CDATA_ALL_OUTER_KEYS, double b = DB_CDATA_ALL_MEDIUM_KEYS, double r = DB_CDATA_ALL_INNER_KEYS );
176
188 void replace( const Data& data, const T& t = DB_CDATA_ALL_OUTER_KEYS, double b = DB_CDATA_ALL_MEDIUM_KEYS, double r = DB_CDATA_ALL_INNER_KEYS );
189
190
202
203
207 void clear();
208
209
214 void setDebug( bool flag ) { debug = flag; }
215
216
221 bool usingDebug() { return debug; }
222
223
224 protected:
225
226
230 bool debug = false;
231
236
247 std::optional< Data > find( const T& t = DB_CDATA_ALL_OUTER_KEYS, double b = DB_CDATA_ALL_MEDIUM_KEYS, double r = DB_CDATA_ALL_INNER_KEYS ) const;
248
249 };
250
251
252 template < class T, class MidFunctor, class InFunctor, class Data, class OutComp, class MidComp, class InComp >
254
255
256 template < class T, class MidFunctor, class InFunctor, class Data, class OutComp, class MidComp, class InComp >
257 const std::optional< Data > CustomDataContainer< T, MidFunctor, InFunctor, Data, OutComp, MidComp, InComp >::get( const T& t, double b, double r ) const {
258 if ( data_map.empty() )
259 return {};
260
261 if ( debug )
262 std::cout << "CustomDataContainer::get() t = " << t << "; b = " << b << "; r = " << r << std::endl;
263
264 auto val = find();
265 if ( val )
266 return val;
267
268 CDCCIt it = data_map.find( t );
269 if ( it == data_map.end() )
270 return {};
271
272 CDCMediumCIt it2 = it->second.lower_bound( b );
273 CDCMediumCRIt rit2 = it->second.rbegin();
274
275 CDCInnerCIt it3;
276 CDCInnerCRIt rit3;
277
278 if ( it2 == it->second.end() ) {
279 if( rit2 == it->second.rend() )
280 return {};
281 it3 = rit2->second.lower_bound( r );
282 rit3 = rit2->second.rbegin();
283 if ( it3 != rit2->second.end() )
284 return it3->second;
285 if ( rit3 != rit2->second.rend() )
286 return rit3->second;
287 return {};
288 }
289 else {
290 it3 = it2->second.lower_bound( r );
291 rit3 = it2->second.rbegin();
292 if ( it3 != it2->second.end() )
293 return it3->second;
294 if ( rit3 != it2->second.rend() )
295 return rit3->second;
296 return {};
297 }
298 if ( it3 == it2->second.end() )
299 return {};
300 return (it3->second);
301 }
302
303
304 template < class T, class MidFunctor, class InFunctor, class Data, class OutComp, class MidComp, class InComp >
305 const std::optional< Data > CustomDataContainer< T, MidFunctor, InFunctor, Data, OutComp, MidComp, InComp >::get( const T& tx, const T& rx ) const {
306 MidFunctor mid_funct;
307 InFunctor in_funct;
308
309 double curr_b;
310 double curr_r;
311 double delta_b;
312 double curr_dist;
313 double min_dist = INFINITY;
314 std::optional< Data > ret_val = {};
315
316 if ( data_map.empty() == true ) {
317 if ( debug ) std::cout << "CustomDataContainer::get() data_map is empty " << std::endl;
318
319 return {};
320 }
321
322 for ( auto it = data_map.cbegin(); it != data_map.cend(); it++ ) {
323 if ( debug )
324 std::cout << "CustomDataContainer::get() start T = " << it->first << "; end T = " << rx << std::endl;
325
326 if ( it->first == DB_CDATA_ALL_OUTER_KEYS ) {
327 if ( debug )
328 std::cout << "CustomDataContainer::get() overriding start T = " << tx << "; end T = " << rx << std::endl;
329
330 curr_b = mid_funct(tx,rx);
331 curr_r = in_funct(tx,rx);
332 }
333 else {
334 curr_b = mid_funct(it->first,rx);
335 curr_r = in_funct(it->first,rx);
336 }
337
338 if ( debug )
339 std::cout << "CustomDataContainer::get() curr bearing = " << curr_b * 180.0 / M_PI
340 << "; curr range = " << curr_r << std::endl;
341
342 auto itb = it->second.cbegin();
343 if ( itb->first == DB_CDATA_ALL_MEDIUM_KEYS )
344 delta_b = 0;
345 else {
346 itb = it->second.lower_bound( curr_b );
347 if ( itb == it->second.cend() )
348 itb = (++(it->second.rbegin())).base();
349
350 delta_b = curr_b - itb->first;
351 if (delta_b < 0.0)
352 delta_b = -delta_b;
353 if (delta_b > M_PI)
354 delta_b = 2.0*M_PI - delta_b ;
355 }
356
357 double ort_dist = curr_r * sin(delta_b);
358 double ort_projection = std::sqrt( curr_r*curr_r - ort_dist*ort_dist );
359
360 if ( debug )
361 std::cout << "CustomDataContainer::get() nearest bearing = " << itb->first * 180.0 / M_PI
362 << "; diff bearing = " << delta_b * 180.0 / M_PI << "; orthog distance = " << ort_dist
363 << "; orthog range projection = " << ort_projection << std::endl;
364
365 auto itr = itb->second.cbegin();
366 if ( itr->first == DB_CDATA_ALL_INNER_KEYS )
367 curr_dist = ort_dist;
368 else {
369 itr = itb->second.lower_bound( ort_projection );
370 if ( itr == itb->second.cbegin() || itr == itb->second.cend() || itr->first == ort_projection ) {
371 if ( itr == itb->second.cend() )
372 itr = (++(itb->second.rbegin())).base();
373 double adj_distance = std::abs( ort_projection - itr->first );
374 curr_dist = std::sqrt( ort_projection*ort_projection + adj_distance*adj_distance );
375 }
376 else {
377 double adj_distance = std::abs( ort_projection - itr->first );
378 double first_dist = std::sqrt( ort_projection*ort_projection + adj_distance*adj_distance );
379
380 if(debug)
381 std::cout << "CustomDataContainer::get() first try, range = " << itr->first
382 << "; dist = " << first_dist << std::endl;
383
384 itr--;
385 adj_distance = std::abs( ort_projection - itr->first );
386 double before_dist = std::sqrt( ort_projection*ort_projection + adj_distance*adj_distance );
387
388 if (debug)
389 std::cout << "CustomDataContainer::get() second try, range = " << itr->first
390 << "; dist = " << before_dist << std::endl;
391
392 curr_dist = std::min( first_dist, before_dist );
393 if ( curr_dist == first_dist )
394 itr++;
395 }
396 }
397 if ( debug )
398 std::cout << "CustomDataContainer::get() nearest range = " << itr->first << "; distance = " << curr_dist
399 << "; min distance = " << min_dist << std::endl;
400
401 if ( curr_dist < min_dist ) {
402 min_dist = curr_dist;
403 ret_val = itr->second;
404 if ( curr_dist == 0 )
405 break;
406 }
407
408 }
409
410 if ( debug && ret_val )
411 std::cout << "CustomDataContainer::get() ret value " << *ret_val << std::endl;
412
413 return ret_val;
414 }
415
416
417 template < class T, class MidFunctor, class InFunctor, class Data, class OutComp, class MidComp, class InComp >
418 std::optional< Data > CustomDataContainer< T, MidFunctor, InFunctor, Data, OutComp, MidComp, InComp >::find( const T& t, double b, double r ) const {
419
420 if (this->debug)
421 std::cout << "CustomDataContainer::find() << t = " << t << "; b = " << b << "; r = " << r << std::endl;
422
423 CDCCIt it = data_map.find( t );
424 if ( it == data_map.end() ) {
425
426 if ( debug )
427 std::cout << "CustomDataContainer::find() t not found" << std::endl;
428
429 return {};
430 }
431
432 if ( debug )
433 std::cout << "CustomDataContainer::find() t found" << std::endl;
434
435 CDCMediumCIt it2 = it->second.find( b );
436 if ( it2 == it->second.end() ) {
437 if ( debug )
438 std::cout << "CustomDataContainer::find() b not found" << std::endl;
439
440 return {};
441 }
442
443 if ( debug )
444 std::cout << "CustomDataContainer::find() b found" << std::endl;
445
446 CDCInnerCIt it3 = it2->second.find( r );
447 if ( it3 == it2->second.end() ) {
448
449 if ( debug )
450 std::cout << "CustomDataContainer::find() r not found" << std::endl;
451
452 return {};
453 }
454 if ( debug )
455 std::cout << "CustomDataContainer::find() r found, data = " << it3->second << std::endl;
456 return it3->second;
457 }
458
459
460 template < class T, class MidFunctor, class InFunctor, class Data, class OutComp, class MidComp, class InComp >
462 auto res = find( t, b, r );
463 if ( res )
464 return false;
465 data_map[t][b][r] = d;
466 return true;
467 }
468
469
470 template < class T, class MidFunctor, class InFunctor, class Data, class OutComp, class MidComp, class InComp >
472 data_map[t][b].erase(r);
473 data_map[t][b][r] = d;
474 }
475
476
477 template < class T, class MidFunctor, class InFunctor, class Data, class OutComp, class MidComp, class InComp >
479 data_map[t][b].erase(r);
480 if ( data_map[t][b].empty() )
481 data_map[t].erase(b);
482 if ( data_map[t].empty() )
483 data_map.erase(t);
484 }
485
486
487 template < class T, class MidFunctor, class InFunctor, class Data, class OutComp, class MidComp, class InComp >
491
492
500 template < class T, class MidFunctor, class InFunctor, class Data, class OutComp = std::less<T>, class MidComp = std::less<double>, class InComp = std::less<double> >
502
503 protected:
504
508 using TimeData = typename std::map< time_t, Data >;
509 using CDTCTimeIt = typename TimeData::iterator;
510 using CDTCTimeRIt = typename TimeData::reverse_iterator;
511 using CDTCTimeCIt = typename TimeData::const_iterator;
512 using CDTCTimeCRIt = typename TimeData::const_reverse_iterator;
513
517 using InnerData = typename std::map< double, TimeData, InComp >;
518 using CDCInnerIt = typename InnerData::iterator;
519 using CDCInnerRIt = typename InnerData::reverse_iterator;
520 using CDCInnerCIt = typename InnerData::const_iterator;
521 using CDCInnerCRIt = typename InnerData::const_reverse_iterator;
522
526 using MediumData = typename std::map< double, InnerData, MidComp >;
527 using CDCMediumIt = typename MediumData::iterator;
528 using CDCMediumCIt = typename MediumData::const_iterator;
529 using CDCMediumRIt = typename MediumData::reverse_iterator;
530 using CDCMediumCRIt = typename MediumData::const_reverse_iterator;
531
535 using CustomContainer = typename std::map< T, MediumData, OutComp >;
536 using CDCIt = typename CustomContainer::iterator;
537 using CDCRIt = typename CustomContainer::reverse_iterator;
538 using CDCCIt = typename CustomContainer::const_iterator;
539 using CDCCRIt = typename CustomContainer::const_reverse_iterator;
540
541 public:
542
543 static constexpr inline double DB_CDATA_ALL_MEDIUM_KEYS = -190.0;
544
545 static constexpr inline double DB_CDATA_ALL_INNER_KEYS = -10.0;
546
547 static const T DB_CDATA_ALL_OUTER_KEYS;
548
550
555
560
566 MediumData& operator[] ( const T& key ) { return data_map[key]; }
567
568
573 bool empty() const { return data_map.empty(); }
574
575
580 int size() const { return data_map.size(); }
581
582
595 std::optional<Data> get( const T& t = DB_CDATA_ALL_OUTER_KEYS, double b = DB_CDATA_ALL_MEDIUM_KEYS, double r = DB_CDATA_ALL_INNER_KEYS, const Time& time_key = DB_CDATA_ALL_TIME_KEYS ) const;
596
605 std::optional<Data> get( const T& tx, const T& rx, const Time& time_key = DB_CDATA_ALL_TIME_KEYS ) const;
606
620 bool insert( const Data& data, const T& t = DB_CDATA_ALL_OUTER_KEYS, double b = DB_CDATA_ALL_MEDIUM_KEYS, double r = DB_CDATA_ALL_INNER_KEYS, const Time& time_key = DB_CDATA_ALL_TIME_KEYS );
621
635 void replace( const Data& data, const T& t = DB_CDATA_ALL_OUTER_KEYS, double b = DB_CDATA_ALL_MEDIUM_KEYS, double r = DB_CDATA_ALL_INNER_KEYS, const Time& time_key = DB_CDATA_ALL_TIME_KEYS );
636
649 void erase( const T& t = DB_CDATA_ALL_OUTER_KEYS, double b = DB_CDATA_ALL_MEDIUM_KEYS, double r = DB_CDATA_ALL_INNER_KEYS, const Time& time_key = DB_CDATA_ALL_TIME_KEYS);
650
651
655 void clear();
656
657
662 void setDebug( bool flag ) { debug = flag; }
663
664
669 bool usingDebug() const { return debug; }
670
671
672 protected:
673
674
678 bool debug;
679
684
697 std::optional<Data> find( const T& t = DB_CDATA_ALL_OUTER_KEYS, double b = DB_CDATA_ALL_MEDIUM_KEYS, double r = DB_CDATA_ALL_INNER_KEYS, const Time& time_key = DB_CDATA_ALL_TIME_KEYS ) const;
698
711 std::optional<Data> calculateData( const TimeData& time_data , const Time& time_key = DB_CDATA_ALL_TIME_KEYS ) const;
712
713 };
714
715 template < class T, class MidFunctor, class InFunctor, class Data, class OutComp, class MidComp, class InComp >
717
718
719 template < class T, class MidFunctor, class InFunctor, class Data, class OutComp, class MidComp, class InComp >
721
722
723 template < class T, class MidFunctor, class InFunctor, class Data, class OutComp, class MidComp, class InComp >
724 std::optional<Data> CustomDataTimeContainer< T, MidFunctor, InFunctor, Data, OutComp, MidComp, InComp >::get( const T& t, double b, double r, const Time& time_key ) const {
725 if ( data_map.empty() )
726 return { };
727
728 if ( debug )
729 std::cout << "CustomDataTimeContainer::get() t = " << t << "; b = " << b << "; r = " << r
730 << "; time_key = " << time_key << std::endl;
731
732 auto ret_val = find();
733 if ( ret_val )
734 return ret_val;
735
736 CDCCIt it = data_map.find( t );
737 if ( it == data_map.end() )
738 return { };
739
740 CDCMediumCIt it2 = it->second.lower_bound( b );
741 CDCMediumCRIt rit2 = it->second.rbegin();
742
743 CDCInnerCIt it3;
744 CDCInnerCRIt rit3;
745
746 if ( it2 == it->second.end() ) {
747 if ( rit2 == it->second.rend() )
748 return { };
749 it3 = rit2->second.lower_bound( r );
750 rit3 = rit2->second.rbegin();
751 if ( it3 != rit2->second.end() )
752 return calculateData( it3->second, time_key);
753 if ( rit3 != rit2->second.rend() )
754 return calculateData( rit3->second, time_key);
755 return { };
756 }
757 else {
758 it3 = it2->second.lower_bound( r );
759 rit3 = it2->second.rbegin();
760 if ( it3 != it2->second.end() )
761 return calculateData(it3->second, time_key);
762 if ( rit3 != it2->second.rend() )
763 return calculateData(rit3->second, time_key);
764 return { };
765 }
766 if ( it3 == it2->second.end() )
767 return { };
768 return calculateData(it3->second, time_key);
769 }
770
771
772 template < class T, class MidFunctor, class InFunctor, class Data, class OutComp, class MidComp, class InComp >
773 std::optional< Data > CustomDataTimeContainer< T, MidFunctor, InFunctor, Data, OutComp, MidComp, InComp >::get( const T& tx, const T& rx, const Time& time_key ) const {
774 MidFunctor mid_funct;
775 InFunctor in_funct;
776
777 double curr_b;
778 double curr_r;
779 double delta_b;
780
781 double curr_dist;
782 double min_dist = INFINITY;
783
784 if ( data_map.empty() == true ) {
785 if ( debug )
786 std::cout << "CustomDataTimeContainer::get() data_map is empty " << std::endl;
787
788 return {};
789 }
790 const TimeData* time_data_ptr = nullptr;
791
792 for ( CDCCIt it = data_map.begin(); it != data_map.end(); it++ ) {
793 if ( debug )
794 std::cout << "CustomDataTimeContainer::get() start T = " << it->first << "; end T = " << rx << std::endl;
795
796 if ( it->first == DB_CDATA_ALL_OUTER_KEYS ) {
797 if ( debug )
798 std::cout << "CustomDataTimeContainer::get() overriding start T = " << tx << "; end T = " << rx << std::endl;
799
800 curr_b = mid_funct(tx,rx);
801 curr_r = in_funct(tx,rx);
802 }
803 else {
804 curr_b = mid_funct(it->first,rx);
805 curr_r = in_funct(it->first,rx);
806 }
807
808 if ( debug )
809 std::cout << "CustomDataTimeContainer::get() curr bearing = " << curr_b * 180.0 / M_PI
810 << "; curr range = " << curr_r << std::endl;
811
812 CDCMediumCIt itb = it->second.begin();
813 if ( itb->first == DB_CDATA_ALL_MEDIUM_KEYS )
814 delta_b = 0;
815 else {
816 itb = it->second.lower_bound( curr_b );
817 if ( itb == it->second.end() )
818 itb = (++(it->second.rbegin())).base();
819
820 delta_b = curr_b - itb->first;
821 if (delta_b < 0.0)
822 delta_b = -delta_b;
823 if (delta_b > M_PI)
824 delta_b = 2.0*M_PI - delta_b ;
825 }
826
827 double ort_dist = curr_r * sin(delta_b);
828 double ort_projection = std::sqrt( curr_r*curr_r - ort_dist*ort_dist );
829
830 if ( debug )
831 std::cout << "CustomDataTimeContainer::get() nearest bearing = " << itb->first * 180.0 / M_PI
832 << "; diff bearing = " << delta_b * 180.0 / M_PI << "; orthog distance = " << ort_dist
833 << "; orthog range projection = " << ort_projection << std::endl;
834
835 CDCInnerCIt itr = itb->second.begin();
836 if ( itr->first == DB_CDATA_ALL_INNER_KEYS )
837 curr_dist = ort_dist;
838 else {
839 itr = itb->second.lower_bound( ort_projection );
840 if ( itr == itb->second.begin() || itr == itb->second.end() || itr->first == ort_projection ) {
841 if ( itr == itb->second.end() ) itr = (++(itb->second.rbegin())).base();
842 double adj_distance = std::abs( ort_projection - itr->first );
843 curr_dist = std::sqrt( ort_projection*ort_projection + adj_distance*adj_distance );
844 }
845 else {
846 double adj_distance = std::abs( ort_projection - itr->first );
847 double first_dist = std::sqrt( ort_projection*ort_projection + adj_distance*adj_distance );
848
849 if (debug)
850 std::cout << "CustomDataTimeContainer::get() first try, range = " << itr->first
851 << "; dist = " << first_dist << std::endl;
852
853 itr--;
854 adj_distance = std::abs( ort_projection - itr->first );
855 double before_dist = std::sqrt( ort_projection*ort_projection + adj_distance*adj_distance );
856
857 if (debug)
858 std::cout << "CustomDataTimeContainer::get() second try, range = " << itr->first
859 << "; dist = " << before_dist << std::endl;
860
861 curr_dist = std::min( first_dist, before_dist );
862 if ( curr_dist == first_dist )
863 itr++;
864 }
865 }
866 if ( debug )
867 std::cout << "CustomDataTimeContainer::get() nearest range = " << itr->first << "; distance = " << curr_dist
868 << "; min distance = " << min_dist << std::endl;
869
870 if ( curr_dist < min_dist ) {
871 min_dist = curr_dist;
872 time_data_ptr = &(itr->second);
873 if ( curr_dist == 0 )
874 break;
875 }
876
877 }
878
879 if ( time_data_ptr != nullptr )
880 return calculateData( *time_data_ptr, time_key);
881 return {};
882 }
883
884
885 template < class T, class MidFunctor, class InFunctor, class Data, class OutComp, class MidComp, class InComp >
886 std::optional<Data> CustomDataTimeContainer< T, MidFunctor, InFunctor, Data, OutComp, MidComp, InComp >::find( const T& t, double b, double r, const Time& time_key ) const {
887 if (debug)
888 std::cout << "CustomDataTimeContainer::find() << t = " << t << "; b = " << b << "; r = "
889 << r << "; time_key = " << time_key << std::endl;
890
891 CDCCIt it = data_map.find( t );
892 if ( it == data_map.end() ) {
893
894 if ( debug )
895 std::cout << "CustomDataTimeContainer::find() t not found" << std::endl;
896
897 return { };
898 }
899
900 if ( debug ) std::cout << "CustomDataTimeContainer::find() t found" << std::endl;
901
902 CDCMediumCIt it2 = it->second.find( b );
903 if ( it2 == it->second.end() ) {
904 if ( debug )
905 std::cout << "CustomDataTimeContainer::find() b not found" << std::endl;
906
907 return {};
908 }
909
910 if ( debug ) std::cout << "CustomDataTimeContainer::find() b found" << std::endl;
911
912 CDCInnerCIt it3 = it2->second.find( r );
913 if ( it3 == it2->second.end() ) {
914 if ( debug )
915 std::cout << "CustomDataTimeContainer::find() r not found" << std::endl;
916
917 return {};
918 }
919 if ( debug ) std::cout << "CustomDataTimeContainer::find() r found" << std::endl;
920
921 CDTCTimeCIt it4 = it3->second.find( time_key );
922 if ( it4 == it3->second.end() ) {
923 if ( debug )
924 std::cout << "CustomDataTimeContainer::find() time_key not found" << std::endl;
925
926 return {};
927 }
928
929 if ( debug ) std::cout << "CustomDataTimeContainer::find() time_key found, Data = " << it4->second << std::endl;
930
931 return it4->second;
932 }
933
934
935 template < class T, class MidFunctor, class InFunctor, class Data, class OutComp, class MidComp, class InComp >
936 bool CustomDataTimeContainer< T, MidFunctor, InFunctor, Data, OutComp, MidComp, InComp >::insert( const Data& d, const T& t, double b, double r, const Time& time_key ) {
937 auto data = find( t, b, r, time_key );
938 if ( data )
939 return false;
940 data_map[t][b][r][time_key] = d;
941 return true;
942 }
943
944
945 template < class T, class MidFunctor, class InFunctor, class Data, class OutComp, class MidComp, class InComp >
946 void CustomDataTimeContainer< T, MidFunctor, InFunctor, Data, OutComp, MidComp, InComp >::replace( const Data& d, const T& t, double b, double r, const Time& time_key ) {
947 data_map[t][b][r][time_key] = d;
948 }
949
950
951 template < class T, class MidFunctor, class InFunctor, class Data, class OutComp, class MidComp, class InComp >
953 data_map[t][b][r].erase(time_key);
954 if ( data_map[t][b][r].empty() )
955 data_map[t][b].erase(r);
956 if ( data_map[t][b].empty() )
957 data_map[t].erase(b);
958 if ( data_map[t].empty() )
959 data_map.erase(t);
960 }
961
962
963 template < class T, class MidFunctor, class InFunctor, class Data, class OutComp, class MidComp, class InComp >
967
968 template < class T, class MidFunctor, class InFunctor, class Data, class OutComp, class MidComp, class InComp >
970 if (debug)
971 std::cout << "CustomDataTimeContainer::calculateData() time_key = " << time_key << std::endl;
972
973 if ( time_data.empty() ) {
974 if (debug)
975 std::cout << "CustomDataTimeContainer::calculateData() time_data is empty." << std::endl;
976
977 return {};
978 }
979
980 if ( time_data.size() == 1 ) {
981 if (debug)
982 std::cout << "CustomDataTimeContainer::calculateData() time_data has size 1. Data created "
983 << time_data.begin()->second << std::endl;
984
985 return time_data.begin()->second;
986 }
987
988 time_t normalized_time = time_key;
989
990 if ( normalized_time < time_data.begin()->first ) {
991 if (debug)
992 std::cout << "CustomDataTimeContainer::calculateData() time_key has time < first key. Data created "
993 << time_data.begin()->second << std::endl;
994
995 return time_data.begin()->second;
996 }
997
998 normalized_time %= ( time_data.rbegin()->first - time_data.begin()->first );
999 if ( normalized_time == 0 )
1000 return time_data.begin()->second;
1001
1002 normalized_time += time_data.begin()->first;
1003 auto upper_it = time_data.upper_bound(normalized_time);
1004 auto lower_it = upper_it;
1005 --lower_it;
1006
1007 double alpha = ( std::abs((double)(upper_it->first - normalized_time)) / std::abs((double)( upper_it->first - lower_it->first )) );
1008 double beta = ( std::abs((double)(normalized_time - lower_it->first)) / std::abs((double)( upper_it->first - lower_it->first )) );
1009
1010 if (debug)
1011 std::cout << "CustomDataTimeContainer::calculateData() normalized_time = " << normalized_time
1012 << "; lower_it time = " << lower_it->first
1013 << "; upper_it time = " << upper_it->first
1014 << "; alpha = " << alpha << "; beta " << beta << std::endl;
1015
1016 if constexpr (is_shared_ptr_v<Data>) {
1017 Data ret_val = std::move(lower_it->second->clone());
1018
1019 *ret_val *= alpha;
1020 *ret_val += *(upper_it->second) * beta;
1021
1022 if (debug)
1023 std::cout << "CustomDataTimeContainer::calculateData() return value = "
1024 << ret_val << std::endl;
1025
1026 return ret_val;
1027 } else {
1028 Data ret_val = lower_it->second * alpha + upper_it->second * beta;
1029
1030 if (debug)
1031 std::cout << "CustomDataTimeContainer::calculateData() return value = "
1032 << ret_val << std::endl;
1033
1034 return ret_val;
1035 }
1036 }
1037}
1038
1039#endif /* WOSS_DB_CUSTOM_DATA_CONTAINER_H */
1040
1041
MediumData & operator[](const T &key)
Definition woss-db-custom-data-container.h:127
typename CustomContainer::reverse_iterator CDCRIt
Definition woss-db-custom-data-container.h:92
typename std::map< double, InnerData, MidComp > MediumData
Definition woss-db-custom-data-container.h:81
typename CustomContainer::const_reverse_iterator CDCCRIt
Definition woss-db-custom-data-container.h:94
int size() const
Definition woss-db-custom-data-container.h:141
bool usingDebug()
Definition woss-db-custom-data-container.h:221
static constexpr double DB_CDATA_ALL_MEDIUM_KEYS
Definition woss-db-custom-data-container.h:99
typename MediumData::const_reverse_iterator CDCMediumCRIt
Definition woss-db-custom-data-container.h:85
typename InnerData::iterator CDCInnerIt
Definition woss-db-custom-data-container.h:73
bool insert(const Data &data, const T &t=DB_CDATA_ALL_OUTER_KEYS, double b=DB_CDATA_ALL_MEDIUM_KEYS, double r=DB_CDATA_ALL_INNER_KEYS)
Definition woss-db-custom-data-container.h:461
typename InnerData::reverse_iterator CDCInnerRIt
Definition woss-db-custom-data-container.h:74
typename MediumData::reverse_iterator CDCMediumRIt
Definition woss-db-custom-data-container.h:84
const std::optional< Data > get(const T &tx, const T &rx) const
Definition woss-db-custom-data-container.h:305
void replace(const Data &data, const T &t=DB_CDATA_ALL_OUTER_KEYS, double b=DB_CDATA_ALL_MEDIUM_KEYS, double r=DB_CDATA_ALL_INNER_KEYS)
Definition woss-db-custom-data-container.h:471
typename std::map< T, MediumData, OutComp > CustomContainer
Definition woss-db-custom-data-container.h:90
const std::optional< Data > get(const T &t=DB_CDATA_ALL_OUTER_KEYS, double b=DB_CDATA_ALL_MEDIUM_KEYS, double r=DB_CDATA_ALL_INNER_KEYS) const
Definition woss-db-custom-data-container.h:257
bool empty() const
Definition woss-db-custom-data-container.h:134
typename InnerData::const_iterator CDCInnerCIt
Definition woss-db-custom-data-container.h:75
typename CustomContainer::iterator CDCIt
Definition woss-db-custom-data-container.h:91
void setDebug(bool flag)
Definition woss-db-custom-data-container.h:214
void clear()
Definition woss-db-custom-data-container.h:488
CustomContainer data_map
Definition woss-db-custom-data-container.h:235
typename MediumData::iterator CDCMediumIt
Definition woss-db-custom-data-container.h:82
std::optional< Data > find(const T &t=DB_CDATA_ALL_OUTER_KEYS, double b=DB_CDATA_ALL_MEDIUM_KEYS, double r=DB_CDATA_ALL_INNER_KEYS) const
Definition woss-db-custom-data-container.h:418
typename InnerData::const_reverse_iterator CDCInnerCRIt
Definition woss-db-custom-data-container.h:76
typename CustomContainer::const_iterator CDCCIt
Definition woss-db-custom-data-container.h:93
typename MediumData::const_iterator CDCMediumCIt
Definition woss-db-custom-data-container.h:83
static constexpr double DB_CDATA_ALL_INNER_KEYS
Definition woss-db-custom-data-container.h:101
void erase(const T &t=DB_CDATA_ALL_OUTER_KEYS, double b=DB_CDATA_ALL_MEDIUM_KEYS, double r=DB_CDATA_ALL_INNER_KEYS)
Definition woss-db-custom-data-container.h:478
typename std::map< double, Data, InComp > InnerData
Definition woss-db-custom-data-container.h:72
static const Coord DB_CDATA_ALL_OUTER_KEYS
Definition woss-db-custom-data-container.h:108
typename MediumData::const_iterator CDCMediumCIt
Definition woss-db-custom-data-container.h:528
typename CustomContainer::iterator CDCIt
Definition woss-db-custom-data-container.h:536
std::optional< Data > calculateData(const TimeData &time_data, const Time &time_key=DB_CDATA_ALL_TIME_KEYS) const
Definition woss-db-custom-data-container.h:969
bool insert(const Data &data, const T &t=DB_CDATA_ALL_OUTER_KEYS, double b=DB_CDATA_ALL_MEDIUM_KEYS, double r=DB_CDATA_ALL_INNER_KEYS, const Time &time_key=DB_CDATA_ALL_TIME_KEYS)
Definition woss-db-custom-data-container.h:936
bool empty() const
Definition woss-db-custom-data-container.h:573
typename std::map< T, MediumData, OutComp > CustomContainer
Definition woss-db-custom-data-container.h:535
typename std::map< double, InnerData, MidComp > MediumData
Definition woss-db-custom-data-container.h:526
typename CustomContainer::const_reverse_iterator CDCCRIt
Definition woss-db-custom-data-container.h:539
typename MediumData::iterator CDCMediumIt
Definition woss-db-custom-data-container.h:527
static constexpr double DB_CDATA_ALL_INNER_KEYS
Definition woss-db-custom-data-container.h:545
typename TimeData::iterator CDTCTimeIt
Definition woss-db-custom-data-container.h:509
typename std::map< time_t, Data > TimeData
Definition woss-db-custom-data-container.h:508
typename InnerData::const_reverse_iterator CDCInnerCRIt
Definition woss-db-custom-data-container.h:521
std::optional< Data > find(const T &t=DB_CDATA_ALL_OUTER_KEYS, double b=DB_CDATA_ALL_MEDIUM_KEYS, double r=DB_CDATA_ALL_INNER_KEYS, const Time &time_key=DB_CDATA_ALL_TIME_KEYS) const
Definition woss-db-custom-data-container.h:886
bool usingDebug() const
Definition woss-db-custom-data-container.h:669
static constexpr double DB_CDATA_ALL_MEDIUM_KEYS
Definition woss-db-custom-data-container.h:543
void erase(const T &t=DB_CDATA_ALL_OUTER_KEYS, double b=DB_CDATA_ALL_MEDIUM_KEYS, double r=DB_CDATA_ALL_INNER_KEYS, const Time &time_key=DB_CDATA_ALL_TIME_KEYS)
Definition woss-db-custom-data-container.h:952
typename TimeData::const_iterator CDTCTimeCIt
Definition woss-db-custom-data-container.h:511
typename std::map< double, TimeData, InComp > InnerData
Definition woss-db-custom-data-container.h:517
typename InnerData::iterator CDCInnerIt
Definition woss-db-custom-data-container.h:518
typename CustomContainer::reverse_iterator CDCRIt
Definition woss-db-custom-data-container.h:537
MediumData & operator[](const T &key)
Definition woss-db-custom-data-container.h:566
std::optional< Data > get(const T &t=DB_CDATA_ALL_OUTER_KEYS, double b=DB_CDATA_ALL_MEDIUM_KEYS, double r=DB_CDATA_ALL_INNER_KEYS, const Time &time_key=DB_CDATA_ALL_TIME_KEYS) const
Definition woss-db-custom-data-container.h:724
void replace(const Data &data, const T &t=DB_CDATA_ALL_OUTER_KEYS, double b=DB_CDATA_ALL_MEDIUM_KEYS, double r=DB_CDATA_ALL_INNER_KEYS, const Time &time_key=DB_CDATA_ALL_TIME_KEYS)
Definition woss-db-custom-data-container.h:946
typename TimeData::reverse_iterator CDTCTimeRIt
Definition woss-db-custom-data-container.h:510
static const Time DB_CDATA_ALL_TIME_KEYS
Definition woss-db-custom-data-container.h:549
typename InnerData::const_iterator CDCInnerCIt
Definition woss-db-custom-data-container.h:520
CustomContainer data_map
Definition woss-db-custom-data-container.h:683
typename MediumData::reverse_iterator CDCMediumRIt
Definition woss-db-custom-data-container.h:529
typename TimeData::const_reverse_iterator CDTCTimeCRIt
Definition woss-db-custom-data-container.h:512
int size() const
Definition woss-db-custom-data-container.h:580
CustomDataTimeContainer()
Definition woss-db-custom-data-container.h:554
static const Coord DB_CDATA_ALL_OUTER_KEYS
Definition woss-db-custom-data-container.h:547
typename MediumData::const_reverse_iterator CDCMediumCRIt
Definition woss-db-custom-data-container.h:530
void clear()
Definition woss-db-custom-data-container.h:964
std::optional< Data > get(const T &tx, const T &rx, const Time &time_key=DB_CDATA_ALL_TIME_KEYS) const
Definition woss-db-custom-data-container.h:773
typename InnerData::reverse_iterator CDCInnerRIt
Definition woss-db-custom-data-container.h:519
void setDebug(bool flag)
Definition woss-db-custom-data-container.h:662
typename CustomContainer::const_iterator CDCCIt
Definition woss-db-custom-data-container.h:538
a class for time date manipulation
Definition time-definitions.h:83
Definition ac-toolbox-arr-asc-reader.h:44
constexpr bool is_shared_ptr_v
Definition woss-db-custom-data-container.h:54
Definition woss-db-custom-data-container.h:48
Definitions and library for woss::Time, woss::SimTime, woss::TimeReference and woss::TimeReferenceTcl...