World Ocean Simulation System (WOSS) library
woss-creator-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
33#ifndef WOSS_CREATOR_CONTAINER_DEFINITIONS_H
34#define WOSS_CREATOR_CONTAINER_DEFINITIONS_H
35
36
37#include <string>
38#include <map>
39#include <memory>
40#include <iostream>
41#include <definitions-handler.h>
42
43
44namespace woss {
45
53
61 CustomTransducer( const std::string& name = "", double bearing = 0.0, double vert_rot = 0.0, double horiz_rot = 0.0, double mult = 1.0, double add = 0.0 )
62 : type(name), initial_bearing(bearing), initial_vert_rotation(vert_rot), initial_horiz_rotation(horiz_rot), multiply_costant(mult), add_costant(add) { }
63
64 friend std::ostream& operator<<( std::ostream& os, const CustomTransducer& instance ) {
65 os << "type name = " << instance.type <<"; initial bearing = " << instance.initial_bearing
66 << "; initial vertical rotation = " << instance.initial_vert_rotation
67 << "; initial horizontal rotation = " << instance.initial_horiz_rotation
68 << "; mult costant = " << instance.multiply_costant << "; add costant = "
69 << instance.add_costant;
70 return os;
71 }
72
76 std::string type;
77
82
87
92
97
102
103 };
104
111 template< typename Data >
113
114 public:
115
119 static std::shared_ptr<Location> ALL_LOCATIONS;
120
124 static const CoordZ ALL_COORDZ;
125
127
129
134 bool isEmpty() const;
135
140 int size() const;
141
149 bool insert( const Data& data, const std::shared_ptr<Location>& tx, const std::shared_ptr<Location>& rx );
150
158 bool insert( const Data& data, const CoordZ& tx, const CoordZ& rx );
159
166 Data get( const std::shared_ptr<Location>& tx, const std::shared_ptr<Location>& rx ) const;
167
174 Data get( const CoordZ& tx, const CoordZ& rx ) const;
175
182
188 void erase( const std::shared_ptr<Location>& tx, const std::shared_ptr<Location>& rx );
189
195 void erase( const CoordZ& tx, const CoordZ& rx );
196
204 void replace( const Data& data, const std::shared_ptr<Location>& tx, const std::shared_ptr<Location>& rx );
205
213 void replace( const Data& data, const CoordZ& tx, const CoordZ& rx );
214
218 void clear();
219
224 void setDebug( bool flag ) { debug = flag; }
225
230 bool isUsingDebug() const { return debug; }
231
232 protected:
233
237 using InnerContainer = std::map< std::shared_ptr<Location>, Data >;
238 using ICIter = typename InnerContainer::iterator;
239 using ICRIter = typename InnerContainer::reverse_iterator;
240 using ICCIter = typename InnerContainer::const_iterator;
241 using ICCRIter = typename InnerContainer::const_reverse_iterator;
242
246 using DataContainer = std::map< std::shared_ptr<Location>, InnerContainer >;
247 using DCIter = typename DataContainer::iterator;
248 using DCCIter = typename DataContainer::const_iterator;
249 using DCRIter = typename DataContainer::reverse_iterator;
250 using DCRCIter = typename DataContainer::const_reverse_iterator;
251
258 DCIter find( const CoordZ& coordinates );
259
267 ICIter find( const CoordZ& coordinates, const DCIter& iter );
268
274 std::shared_ptr<Location> createLocation( const CoordZ& coordinates );
275
280
284 bool debug = false;
285
286 };
287
288
289 template< typename Data >
290 std::shared_ptr<Location> WossCreatorContainer< Data >::ALL_LOCATIONS = nullptr;
291
292
293 template< typename Data >
295
296
297 template< typename Data >
299 return data_container.empty();
300 }
301
302
303 template< typename Data >
305 return data_container.size();
306 }
307
308
309 template< typename Data >
310 inline std::shared_ptr<Location> WossCreatorContainer< Data >::createLocation( const CoordZ& coordinates ) {
311 if ( coordinates == ALL_COORDZ )
312 return nullptr;
313 return std::move( SDefHandler::instance().createLocation(coordinates) );
314 }
315
316
317 template< typename Data >
319 auto it_all_loc = data_container.end();
320
321 for ( auto it = data_container.begin(); it != data_container.end(); it++ ) {
322 if ( it->first == ALL_LOCATIONS ) {
323 it_all_loc = it;
324 if ( coordinates == ALL_COORDZ )
325 break;
326 else
327 continue;
328 }
329 if ( it->first->isEquivalentTo( coordinates ) ) {
330 if (debug)
331 std::cout << "WossCreatorContainer::find() tx coordinates found = "
332 << coordinates << std::endl;
333 return it;
334 }
335 }
336
337 if ( coordinates == ALL_COORDZ && it_all_loc != data_container.end() ) {
338 if (debug)
339 std::cout << "WossCreatorContainer::find() tx ALL_COORDZ found" << std::endl;
340 return it_all_loc;
341 }
342
343 if (debug && coordinates != ALL_COORDZ )
344 std::cout << "WossCreatorContainer::find() tx coordinates not found = "
345 << coordinates << std::endl;
346
347 if (debug && coordinates == ALL_COORDZ )
348 std::cout << "WossCreatorContainer::find() tx ALL_COORDZ not found" << std::endl;
349 return data_container.end();
350 }
351
352
353 template< typename Data >
354 typename WossCreatorContainer< Data >::ICIter WossCreatorContainer< Data >::find( const CoordZ& coordinates, const DCIter& iter ) {
355 auto it_all_loc = iter->second.end();
356
357 for ( auto it = iter->second.begin(); it != iter->second.end(); it++ ) {
358 if ( it->first == ALL_LOCATIONS ) {
359 it_all_loc = it;
360 if ( coordinates == ALL_COORDZ )
361 break;
362 else
363 continue;
364 }
365 if ( it->first->isEquivalentTo( coordinates ) ) {
366 if (debug)
367 std::cout << "WossCreatorContainer::find() rx coordinates found = "
368 << coordinates << std::endl;
369 return it;
370 }
371 }
372
373 if ( coordinates == ALL_COORDZ && it_all_loc != iter->second.end() ) {
374 if (debug)
375 std::cout << "WossCreatorContainer::find() rx ALL_COORDZ found" << std::endl;
376 return it_all_loc;
377 }
378
379 if (debug && coordinates != ALL_COORDZ )
380 std::cout << "WossCreatorContainer::find() rx coordinates not found = "
381 << coordinates << std::endl;
382
383 if (debug && coordinates == ALL_COORDZ )
384 std::cout << "WossCreatorContainer::find() rx ALL_COORDZ not found" << std::endl;
385 return iter->second.end();
386 }
387
388
389 template< typename Data >
390 inline bool WossCreatorContainer< Data >::insert( const Data& data, const std::shared_ptr<Location>& tx, const std::shared_ptr<Location>& rx ) {
391 auto it = data_container.find(tx);
392 if ( it == data_container.end() ) {
393 data_container[tx][rx] = data;
394 return true;
395 }
396 auto it2 = it->second.find(rx);
397 if ( it2 == it->second.end() ) {
398 (it->second)[rx] = data;
399 return true;
400 }
401 return false;
402 }
403
404
405 template< typename Data >
406 inline bool WossCreatorContainer< Data >::insert( const Data& data, const CoordZ& tx, const CoordZ& rx ) {
407 auto it = find( tx );
408 if ( it == data_container.end() ) {
409 data_container[ createLocation(tx) ][ createLocation(rx) ] = data;
410 return true;
411 }
412 auto it2 = find( rx, it );
413 if ( it2 == it->second.end() ) {
414 (it->second)[ createLocation(rx) ] = data;
415 return true;
416 }
417 return false;
418 }
419
420
421 template< typename Data >
422 inline Data WossCreatorContainer< Data >::get( const std::shared_ptr<Location>& tx, const std::shared_ptr<Location>& rx ) const {
423 auto it = const_cast< typename WossCreatorContainer< Data >::DataContainer& >(data_container).find( tx );
424
425 if ( tx != ALL_LOCATIONS ) {
426 if ( it == data_container.end() ) {
427 if ( debug )
428 std::cout << "WossCreatorContainer::get() no tx location found = " << *tx
429 << "; trying ALL_LOCATIONS"<< std::endl;
430
431 it = const_cast< typename WossCreatorContainer< Data >::DataContainer& >(data_container).find( ALL_LOCATIONS );
432 }
433
434 if ( debug )
435 std::cout << "WossCreatorContainer::get() tx location found = " << *tx << std::endl;
436 }
437
438 if ( it != data_container.end() ) {
439 auto it2 = it->second.find( rx );
440
441 if ( rx != ALL_LOCATIONS ) {
442 if ( it2 == it->second.end() ) {
443 if ( debug )
444 std::cout << "WossCreatorContainer::get() no rx location found = " << *rx
445 << "; trying ALL_LOCATIONS"<< std::endl;
446
447 it2 = it->second.find( ALL_LOCATIONS );
448 }
449
450 if ( debug )
451 std::cout << "WossCreatorContainer::get() rx location found = " << *rx << std::endl;
452 }
453
454 if ( it2 != it->second.end() )
455 return it2->second;
456 }
457
458 std::cerr << "WARNING: WossCreatorContainer::get() no tx nor rx location found, returning default constructor!!" << std::endl;
459
460 return Data();
461 }
462
463
464 template< typename Data >
466 return data_container[ALL_LOCATIONS][ALL_LOCATIONS];
467 }
468
469
470 template< typename Data >
471 inline Data WossCreatorContainer< Data >::get( const CoordZ& tx, const CoordZ& rx ) const {
472 auto it = const_cast< WossCreatorContainer< Data >& >(*this).find( tx );
473
474 if ( tx != ALL_COORDZ ) {
475 if ( it == data_container.end() )
476 it = const_cast< WossCreatorContainer< Data >& >(*this).find( ALL_COORDZ );
477 }
478
479 if ( it != data_container.end() ) {
480 auto it2 = const_cast< WossCreatorContainer< Data >& >(*this).find( rx, it );
481
482 if ( rx != ALL_COORDZ ) {
483 if ( it2 == it->second.end() )
484 it2 = const_cast< WossCreatorContainer< Data >& >(*this).find( ALL_COORDZ, it );
485 }
486
487 if ( it2 != it->second.end() ) {
488 if ( debug )
489 std::cout << "WossCreatorContainer::get() value found = " << it2->second << std::endl;
490
491 return it2->second;
492 }
493 }
494 std::cerr << "WARNING: WossCreatorContainer::get() no tx nor rx coordinates found, returning default constructor!!" << std::endl;
495
496 return Data();
497 }
498
499
500 template< typename Data >
501 inline void WossCreatorContainer< Data >::erase( const std::shared_ptr<Location>& tx, const std::shared_ptr<Location>& rx ) {
502 auto it = data_container.find(tx);
503 if ( it != data_container.end() )
504 it->second.erase( rx );
505 if ( it->second.empty() )
506 data_container.erase(it);
507 }
508
509
510 template< typename Data >
511 inline void WossCreatorContainer< Data >::erase( const CoordZ& tx, const CoordZ& rx ) {
512 for ( auto it = data_container.begin(); it != data_container.end(); ) {
513 bool tx_found = false;
514
515 if ( it->first != ALL_LOCATIONS ) {
516 if ( it->first->isEquivalentTo( tx ) )
517 tx_found = true;
518 } else {
519 if ( tx == ALL_COORDZ )
520 tx_found = true;
521 }
522
523 if ( tx_found ) {
524 for ( auto it2 = it->second.begin(); it2 != it->second.end(); ) {
525 bool rx_found = false;
526
527 if ( it2->first != ALL_LOCATIONS ) {
528 if ( it2->first->isEquivalentTo( rx ) )
529 rx_found = true;
530 } else {
531 if ( rx == ALL_COORDZ )
532 rx_found = true;
533 }
534
535 if ( rx_found )
536 it->second.erase(it2++);
537 else
538 ++it2;
539 }
540
541 }
542
543 if ( it->second.empty() )
544 data_container.erase(it++);
545 else ++it;
546 }
547 }
548
549
550 template< typename Data >
551 inline void WossCreatorContainer< Data >::replace( const Data& data, const std::shared_ptr<Location>& tx, const std::shared_ptr<Location>& rx ) {
552 data_container[tx][rx] = data;
553 }
554
555
556 template< typename Data >
557 inline void WossCreatorContainer< Data >::replace( const Data& data, const CoordZ& tx, const CoordZ& rx ) {
558 data_container[ createLocation(tx) ][ createLocation(rx) ] = data;
559 }
560
561
562 template< typename Data >
564 data_container.clear();
565 }
566
567
569
575 template<>
577
578 public:
579
580 static std::shared_ptr<Location> ALL_LOCATIONS;
581
582 static const CoordZ ALL_COORDZ;
583
585
587
588 bool isEmpty() const;
589
590 int size() const;
591
600 bool insert( const CustomTransducer& data, const std::shared_ptr<Location>& tx, const std::shared_ptr<Location>& rx );
601
610 bool insert( const CustomTransducer& data, const CoordZ& tx, const CoordZ& rx );
611
618 CustomTransducer get( const std::shared_ptr<Location>& tx, const std::shared_ptr<Location>& rx ) const;
619
626 CustomTransducer get( const CoordZ& tx, const CoordZ& rx ) const;
627
635
642 void erase( const std::shared_ptr<Location>& tx, const std::shared_ptr<Location>& rx );
643
650 void erase( const CoordZ& tx, const CoordZ& rx );
651
659 void replace( const CustomTransducer& data, const std::shared_ptr<Location>& tx, const std::shared_ptr<Location>& rx );
660
668 void replace( const CustomTransducer& data, const CoordZ& tx, const CoordZ& rx );
669
670 void clear();
671
672 void setDebug( bool flag ) { debug = flag; }
673
674 bool isUsingDebug() const { return debug; }
675
676 protected:
677
678 using InnerContainer = std::map< std::shared_ptr<Location>, CustomTransducer >;
679 using ICIter = InnerContainer::iterator;
680 using ICRIter = InnerContainer::reverse_iterator;
681 using ICCIter = InnerContainer::const_iterator ;
682 using ICCRIter = InnerContainer::const_reverse_iterator;
683
684 using DataContainer = std::map< std::shared_ptr<Location>, InnerContainer >;
685 using DCIter = DataContainer::iterator;
686 using DCCIter = DataContainer::const_iterator;
687 using DCRIter = DataContainer::reverse_iterator;
688 using DCRCIter = DataContainer::const_reverse_iterator;
689
690 DCIter find( const CoordZ& coordinates );
691
692 ICIter find( const CoordZ& coordinates, const DCIter& iter );
693
694 std::shared_ptr<Location> createLocation( const CoordZ& coordinates );
695
697
698 bool debug = false;
699
700 };
701
702
704 return data_container.empty();
705 }
706
707
709 return data_container.size();
710 }
711
712
713 inline std::shared_ptr<Location> WossCreatorContainer< CustomTransducer >::createLocation( const CoordZ& coordinates ) {
714 if ( coordinates == ALL_COORDZ )
715 return nullptr;
716 return std::move(SDefHandler::instance().createLocation(coordinates));
717 }
718
719
720 inline bool WossCreatorContainer< CustomTransducer >::insert( const CustomTransducer& data, const std::shared_ptr<Location>& tx, const std::shared_ptr<Location>& rx ) {
721 auto it = data_container.find(tx);
722 if ( it == data_container.end() ) {
723 data_container[tx][rx] = data;
724 return true;
725 }
726 auto it2 = it->second.find(rx);
727 if ( it2 == it->second.end() ) {
728 (it->second)[rx] = data;
729 return true;
730 }
731 return false;
732 }
733
734
735 inline bool WossCreatorContainer< CustomTransducer >::insert( const CustomTransducer& data, const CoordZ& tx, const CoordZ& rx ) {
736 auto it = find( tx );
737 if ( it == data_container.end() ) {
738 data_container[ createLocation(tx) ][ createLocation(rx) ] = data;
739 return true;
740 }
741 auto it2 = find( rx, it );
742 if ( it2 == it->second.end() ) {
743 (it->second)[ createLocation(rx) ] = data;
744 return true;
745 }
746 return false;
747 }
748
749
753
754
755 inline void WossCreatorContainer< CustomTransducer >::erase( const std::shared_ptr<Location>& tx, const std::shared_ptr<Location>& rx ) {
756 DCIter it = data_container.find(tx);
757 if ( it != data_container.end() ) {
758
759 ICIter it2 = it->second.find( rx );
760
761 if ( it2 != it->second.end() ) {
762 it->second.erase( it2 );
763 }
764
765 }
766 if ( it->second.empty() ) {
767 data_container.erase(it);
768 }
769 }
770
771
772 inline void WossCreatorContainer< CustomTransducer >::erase( const CoordZ& tx, const CoordZ& rx ) {
773 for ( DCIter it = data_container.begin(); it != data_container.end(); ) {
774 bool tx_found = false;
775
776 if ( it->first != ALL_LOCATIONS ) {
777 if ( it->first->isEquivalentTo( tx ) )
778 tx_found = true;
779 } else {
780 if ( tx == ALL_COORDZ )
781 tx_found = true;
782 }
783
784 if ( tx_found ) {
785 for ( ICIter it2 = it->second.begin(); it2 != it->second.end(); ) {
786 bool rx_found = false;
787
788 if ( it2->first != ALL_LOCATIONS ) {
789 if ( it2->first->isEquivalentTo( rx ) )
790 rx_found = true;
791 } else {
792 if ( rx == ALL_COORDZ )
793 rx_found = true;
794 }
795
796 if ( rx_found ) {
797 it->second.erase(it2++);
798 }
799 else ++it2;
800 }
801 }
802 if ( it->second.empty() ) {
803 data_container.erase(it++);
804 }
805 else
806 ++it;
807 }
808 }
809
810
811 inline void WossCreatorContainer< CustomTransducer >::replace( const CustomTransducer& data, const std::shared_ptr<Location>& tx, const std::shared_ptr<Location>& rx ) {
812 data_container[tx][rx] = data;
813 }
814
815
816 inline void WossCreatorContainer< CustomTransducer >::replace( const CustomTransducer& data, const CoordZ& tx, const CoordZ& rx ) {
817 std::shared_ptr<Location> tx_loc = createLocation(tx);
818 std::shared_ptr<Location> rx_loc = createLocation(rx);
819 data_container[tx_loc][rx_loc] = data;
820 }
821
822
826
827}
828
829#endif // WOSS_CREATOR_CONTAINER_DEFINITIONS_H
3D-Coordinates (lat, long, depth) class definitions and functions library
Definition coordinates-definitions.h:384
static T & instance()
Definition singleton-definitions.h:90
DataContainer::reverse_iterator DCRIter
Definition woss-creator-container.h:687
DataContainer::iterator DCIter
Definition woss-creator-container.h:685
ICIter find(const CoordZ &coordinates, const DCIter &iter)
InnerContainer::reverse_iterator ICRIter
Definition woss-creator-container.h:680
void setDebug(bool flag)
Definition woss-creator-container.h:672
std::map< std::shared_ptr< Location >, InnerContainer > DataContainer
Definition woss-creator-container.h:684
InnerContainer::const_reverse_iterator ICCRIter
Definition woss-creator-container.h:682
DataContainer data_container
Definition woss-creator-container.h:696
static const CoordZ ALL_COORDZ
Definition woss-creator-container.h:582
DataContainer::const_reverse_iterator DCRCIter
Definition woss-creator-container.h:688
CustomTransducer get(const std::shared_ptr< Location > &tx, const std::shared_ptr< Location > &rx) const
InnerContainer::iterator ICIter
Definition woss-creator-container.h:679
bool isUsingDebug() const
Definition woss-creator-container.h:674
std::map< std::shared_ptr< Location >, CustomTransducer > InnerContainer
Definition woss-creator-container.h:678
InnerContainer::const_iterator ICCIter
Definition woss-creator-container.h:681
CustomTransducer get(const CoordZ &tx, const CoordZ &rx) const
DCIter find(const CoordZ &coordinates)
DataContainer::const_iterator DCCIter
Definition woss-creator-container.h:686
static std::shared_ptr< Location > ALL_LOCATIONS
Definition woss-creator-container.h:580
Class that stores WossCreator parameters.
Definition woss-creator-container.h:112
std::map< std::shared_ptr< Location >, Data > InnerContainer
Definition woss-creator-container.h:237
static const CoordZ ALL_COORDZ
Definition woss-creator-container.h:124
typename DataContainer::const_iterator DCCIter
Definition woss-creator-container.h:248
bool insert(const Data &data, const CoordZ &tx, const CoordZ &rx)
Definition woss-creator-container.h:406
typename DataContainer::iterator DCIter
Definition woss-creator-container.h:247
DCIter find(const CoordZ &coordinates)
Definition woss-creator-container.cpp:45
typename InnerContainer::reverse_iterator ICRIter
Definition woss-creator-container.h:239
Data & accessAllLocations()
Definition woss-creator-container.h:465
typename InnerContainer::const_reverse_iterator ICCRIter
Definition woss-creator-container.h:241
bool debug
Definition woss-creator-container.h:284
bool insert(const Data &data, const std::shared_ptr< Location > &tx, const std::shared_ptr< Location > &rx)
Definition woss-creator-container.h:390
Data get(const std::shared_ptr< Location > &tx, const std::shared_ptr< Location > &rx) const
Definition woss-creator-container.cpp:153
DataContainer data_container
Definition woss-creator-container.h:279
void clear()
Definition woss-creator-container.h:563
void replace(const Data &data, const std::shared_ptr< Location > &tx, const std::shared_ptr< Location > &rx)
Definition woss-creator-container.h:551
static std::shared_ptr< Location > ALL_LOCATIONS
Definition woss-creator-container.h:119
std::shared_ptr< Location > createLocation(const CoordZ &coordinates)
Definition woss-creator-container.h:310
typename InnerContainer::iterator ICIter
Definition woss-creator-container.h:238
typename DataContainer::reverse_iterator DCRIter
Definition woss-creator-container.h:249
typename DataContainer::const_reverse_iterator DCRCIter
Definition woss-creator-container.h:250
typename InnerContainer::const_iterator ICCIter
Definition woss-creator-container.h:240
void setDebug(bool flag)
Definition woss-creator-container.h:224
int size() const
Definition woss-creator-container.h:304
void replace(const Data &data, const CoordZ &tx, const CoordZ &rx)
Definition woss-creator-container.h:557
void erase(const CoordZ &tx, const CoordZ &rx)
Definition woss-creator-container.h:511
std::map< std::shared_ptr< Location >, InnerContainer > DataContainer
Definition woss-creator-container.h:246
bool isUsingDebug() const
Definition woss-creator-container.h:230
void erase(const std::shared_ptr< Location > &tx, const std::shared_ptr< Location > &rx)
Definition woss-creator-container.h:501
bool isEmpty() const
Definition woss-creator-container.h:298
Provides the interface for woss::DefHandler class.
Definition ac-toolbox-arr-asc-reader.h:44
Initial set up of a transducer.
Definition woss-creator-container.h:52
double initial_vert_rotation
Definition woss-creator-container.h:86
double initial_bearing
Definition woss-creator-container.h:81
double add_costant
Definition woss-creator-container.h:101
friend std::ostream & operator<<(std::ostream &os, const CustomTransducer &instance)
Definition woss-creator-container.h:64
CustomTransducer(const std::string &name="", double bearing=0.0, double vert_rot=0.0, double horiz_rot=0.0, double mult=1.0, double add=0.0)
Definition woss-creator-container.h:61
double initial_horiz_rotation
Definition woss-creator-container.h:91
std::string type
Definition woss-creator-container.h:76
double multiply_costant
Definition woss-creator-container.h:96