Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/eckit stats #154

Draft
wants to merge 149 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 146 commits
Commits
Show all changes
149 commits
Select commit Hold shift + click to select a range
9c060b9
Move mir::action::statistics::* to mir::stats::, make them not Action…
pmaciel Dec 5, 2016
5e48e9a
Work on Statistics
pmaciel Dec 6, 2016
a712a43
Work on Statistics
pmaciel Dec 6, 2016
b3859fe
Work on Statistics
pmaciel Dec 6, 2016
8ccc4f4
Work on Statistics
pmaciel Dec 6, 2016
54013f0
Work on Statistics comparisons
pmaciel Dec 6, 2016
d211233
Work on Statistics comparisons
pmaciel Dec 6, 2016
a5e88ae
Work on Statistics comparisons
pmaciel Dec 13, 2016
b05421e
missing includes
pmaciel Jun 5, 2017
0b0419a
Tidy code
b8raoult Jul 15, 2017
87bdf92
Cleanup some includes
pmaciel Jul 23, 2017
da2082f
Cleanup old code
pmaciel Jul 25, 2017
8ae993a
Cleanup
pmaciel Aug 10, 2017
cd2dc63
mir::data::Dimension remove dead code (and angle statistics)
pmaciel Aug 25, 2017
89682b0
ECC-551: implemented resolution on calculation of the spectral varian…
pmaciel Oct 5, 2017
d2da270
mir-statistics
pmaciel Nov 22, 2017
4a6f766
Fix license dates do we dont have to change every year
tlmquintino Feb 18, 2018
944e3a1
Abstract MIRValuesVector from std::vector (no container type change)
pmaciel Jun 18, 2018
e288ba4
MIR-352, MIR-353, MIR-354: field statistics
pmaciel Mar 8, 2019
0d32d28
MIR-352, MIR-353, MIR-354: field statistics
pmaciel Mar 19, 2019
11e2602
MIR-352, MIR-353, MIR-354: field statistics
pmaciel Mar 20, 2019
dddc690
MIR-352, MIR-353, MIR-354: field statistics
pmaciel Mar 20, 2019
049d8df
MIR-352, MIR-353, MIR-354: field statistics
pmaciel Mar 22, 2019
2912c80
MIR-353: cleanup angles comparison
pmaciel Mar 23, 2019
3071049
MIR-352, MIR-353, MIR-354: field statistics
pmaciel Mar 26, 2019
f7f768d
MIR-352, MIR-353, MIR-354: field statistics
pmaciel Mar 26, 2019
fba383a
Cleanup
pmaciel Mar 26, 2019
6d30695
ECKIT-381: substitute eckit::ScopedPtr for std::unique_ptr
pmaciel Mar 26, 2019
9155637
Angle statistics/comparison performace
pmaciel Mar 27, 2019
efa9a1d
Fix mir-compare for reduced_ll
pmaciel Mar 27, 2019
321df0f
mir-statistics
pmaciel Mar 28, 2019
f5a28b4
mir-statistics
pmaciel Mar 29, 2019
846d46a
MIR-352, MIR-353, MIR-354: field statistics
pmaciel Mar 8, 2019
8b6471a
MIR-352, MIR-353, MIR-354: field statistics
pmaciel Mar 26, 2019
e291716
MIR-353: cleanup angles comparison
pmaciel Mar 23, 2019
baa12ef
mir-statistics
pmaciel Mar 23, 2019
cad3759
Angle statistics/comparison performace
pmaciel Mar 27, 2019
57a8e61
Fix mir-compare for reduced_ll
pmaciel Mar 27, 2019
18d0a67
MIR-352, MIR-353, MIR-354: SameParametrisation 'strict' checks keys i…
pmaciel Apr 9, 2019
adcdbc9
Merge branch 'hotfix/1.2.5' into develop
pmaciel Apr 13, 2019
a79e134
MIR-377, PGEN-180 explicitly declare available angle statistics (used…
pmaciel May 28, 2019
94a51a8
Merge branch 'hotfix/1.2.6' into develop
pmaciel Jun 4, 2019
552d327
MIR-5: GridBoxIntegral
pmaciel Sep 24, 2019
335f2de
MIR-5: GridBoxIntegral
pmaciel Sep 24, 2019
a751be6
MIR-5: GridBoxIntegral
pmaciel Sep 24, 2019
6ac0bc6
Fix (some) warnings
pmaciel Sep 25, 2019
65276cd
Merge remote-tracking branch 'origin/release/1.3.0'
pmaciel Oct 1, 2019
ae7cef7
MIR-399: additional options to mir-compare (--ignore-above-latitude= …
pmaciel Oct 3, 2019
6c3025b
Merge branch 'release/1.3.2'
pmaciel Oct 3, 2019
7ca9c60
Merge branch 'release/1.3.2' into develop
pmaciel Oct 3, 2019
111295f
Merge branch 'hotfix/1.3.3'
pmaciel Oct 4, 2019
b37f134
MIR-377, PGEN-180 explicitly declare available angle statistics (used…
pmaciel May 28, 2019
f4733e9
Merge branch 'hotfix/1.2.10'
pmaciel Oct 4, 2019
bb6d1f0
Merge branch 'hotfix/1.2.10' into develop
pmaciel Oct 4, 2019
cacaf2a
Merge branch 'hotfix/1.3.4'
pmaciel Oct 24, 2019
4628a15
cppcheck fixes
pmaciel Oct 25, 2019
d48f9c2
MIR-408: value statistics
pmaciel Oct 30, 2019
242da50
MIR-408: value statistics
pmaciel Oct 30, 2019
218db60
MIR-408: value statistics
pmaciel Nov 1, 2019
b833bca
MIR-407: mir-compare, pgen-compare --compare-missing-values
pmaciel Nov 6, 2019
0d0abd9
MIR-411: cleanup headers
pmaciel Nov 7, 2019
849a809
MIR-411: cleanup headers
pmaciel Nov 7, 2019
a0c8e4e
Merge branch 'hotfix/1.3.6' into develop
pmaciel Nov 21, 2019
8587eaa
Merge branch 'release/1.4.0'
pmaciel Dec 5, 2019
93faf46
MIR-418: mir-compare/pgen-compare simultaneous absolute/relative/pack…
pmaciel Dec 9, 2019
489b7ee
MIR-418: mir-compare/pgen-compare relative compare using factor range…
pmaciel Dec 9, 2019
9714ba9
MIR-423: random input
pmaciel Dec 10, 2019
eb8a1d0
MIR-423: random input
pmaciel Dec 12, 2019
b3eb405
Merge branch 'hotfix/1.4.1' into develop
pmaciel Dec 19, 2019
29d1d60
MIR-443: Update license headers
pmaciel Jan 28, 2020
3548e14
MIR-444: Update code style
pmaciel Jan 28, 2020
60cc03d
Improvements (readability-static-definition-in-anonymous-namespace)
pmaciel Feb 10, 2020
7c19c86
Cleanup
pmaciel Feb 10, 2020
b3cfc54
Improvements (modernize-*, -Wclazy-*)
pmaciel Feb 12, 2020
1ed3c70
Cleanup
pmaciel Mar 9, 2020
9f09ed7
MIR-456: mir-compare/pgen-compare stricter defaults, --absolute-error…
pmaciel Mar 10, 2020
9972ef2
Merge branch 'hotfix/1.5.1'
pmaciel Mar 10, 2020
7122e0c
Merge branch 'hotfix/1.5.1' into develop
pmaciel Mar 10, 2020
2a3ebd7
Merge branch 'hotfix/1.5.2' into develop
pmaciel Mar 13, 2020
f9299c9
Update clang-format (9.0.1)
pmaciel Jun 1, 2020
c5991c1
Reorder logic
pmaciel Nov 21, 2020
7f8e7c2
Add override
b8raoult Dec 2, 2020
4b8ec7a
Add 'override' keyword
b8raoult Dec 3, 2020
21354e2
Merge branch 'develop' of ssh://git.ecmwf.int/mir/mir into develop
b8raoult Dec 3, 2020
639c4e9
Use 'override'
b8raoult Dec 3, 2020
12013df
Add 'override'
b8raoult Dec 3, 2020
d28bb28
Add 'override'
pmaciel Dec 3, 2020
b1bf144
MIR-505, MIR-507 Types.h
pmaciel Jan 25, 2021
010ae33
MIR-505, MIR-508 Refactor (eckit::Log to mir::Log, exceptions to mir:…
pmaciel Jan 29, 2021
1c54b37
MIR-505, MIR-508 Refactor (eckit::ResourceUsage and eckit::Timer to m…
pmaciel Jan 29, 2021
2648331
MIR-505, MIR-508 Refactor (eckit::ResourceUsage and eckit::Timer to m…
pmaciel Jan 30, 2021
ead4cc1
MIR-505, MIR-506 STL threads for factories (lookup(), build(), list())
pmaciel Feb 1, 2021
6b4e029
clang-format (11!)
pmaciel Mar 11, 2021
c6c533c
Merge branch 'feature/MIR-513' into develop
pmaciel Mar 12, 2021
88a3e0e
MIR-517 Voronoi diagram statistics methods
pmaciel Mar 16, 2021
07d191d
MIR-517 Voronoi diagram statistics methods
pmaciel Mar 22, 2021
6406304
MIR-517 Voronoi methods
pmaciel Mar 25, 2021
6b4497b
MIR-517 Voronoi methods
pmaciel Mar 25, 2021
40e976e
MIR-517 Voronoi/grid-box statistics methods
pmaciel Mar 25, 2021
72ac58b
MIR-517 Voronoi/grid-box statistics methods
pmaciel Mar 26, 2021
1f31c85
MIR-517 Voronoi/grid-box statistics methods
pmaciel Mar 27, 2021
0b2af7b
MIR-517 Voronoi/grid-box statistics methods
pmaciel Mar 29, 2021
e36fc54
MIR-517 Field statistics unit tests fix
pmaciel Apr 6, 2021
cb95334
MIR-517 Field statistics median
pmaciel Apr 6, 2021
ba4ca94
MIR-517 Field statistics output
pmaciel Apr 6, 2021
f4a5860
MIR-517 remove ValueStatistics, cray.85 cannot handle this
pmaciel Apr 7, 2021
42e548e
#pragma once
pmaciel Apr 7, 2021
2793384
cray.85
pmaciel Apr 7, 2021
52a2178
ATLAS-301, MIR-454 FiniteElement options update
pmaciel Apr 9, 2021
3648df1
clang macOS fix warnings
pmaciel Apr 13, 2021
6a2709f
MIR-505 Refactor (MIRValuesVector simple definition)
pmaciel Apr 15, 2021
1bfc6b1
MIR-505 Correct headers
pmaciel Apr 15, 2021
6d18c1c
MIR-505, MIR-506 Reverted c++ 11 threading in favour of eckit::Mutex/…
pmaciel Apr 23, 2021
3d13f43
Fix/correct value conversions
pmaciel Apr 29, 2021
fb0e714
Fix/correct class inheritance (virtual and override)
pmaciel Apr 29, 2021
aa48457
MIR-526 MultiDimensionalInput/MultiDimensionalOutput
pmaciel Apr 30, 2021
3eb67bb
Merge branch 'feature/MIR-526' into develop
pmaciel May 5, 2021
b9cfa49
Merge remote-tracking branch 'origin/release/1.8.0' into develop
pmaciel May 12, 2021
8af674d
MIR-537, PGEN-413 Refactor
pmaciel Aug 9, 2021
2f403ee
Merge branch 'hotfix/1.9.6'
pmaciel Aug 23, 2021
ffa12e2
Cleanup
pmaciel Sep 7, 2021
1d76c31
MIR-540 GRIB gridType=space_view: refactor
pmaciel Oct 4, 2021
152d463
Merge branch 'feature/MIR-540' into develop
pmaciel Oct 11, 2021
fe0a36d
PGEN-426 Revert "ATLAS-301, MIR-454 FiniteElement options update"
pmaciel Nov 19, 2021
2aadb50
Merge branch 'hotfix/1.9.7' into develop
pmaciel Nov 19, 2021
4973808
Merge branch 'hotfix/1.9.7'
pmaciel Nov 22, 2021
e7e9adb
Merge branch 'release/1.10.0'
danovaro Dec 3, 2021
53cce12
MIR-559 k-nn interpolation (weighting) methods to match Metview GEO_T…
pmaciel Jan 25, 2022
fc6a3db
Merge branch 'feature/MIR-559' into develop
pmaciel Jan 25, 2022
1da915f
hicpp-use-auto, llvm-include-order, llvm-qualified-auto, modernize-lo…
pmaciel Jan 31, 2022
17ff1c1
Linting
pmaciel Feb 1, 2022
7625c38
Merge branch 'release/1.11.0'
pmaciel Feb 9, 2022
cf15b3e
Fix in interpolation-statistics method "sum", calculating sum instead…
pmaciel Feb 19, 2022
368e32b
Cleanup
pmaciel Feb 21, 2022
279ffb2
Merge branch 'release/1.12.0'
pmaciel Mar 2, 2022
a569ab7
clang-format (14)
pmaciel Nov 12, 2022
6f34418
Mode statistics correct construction (not a problem due to well-initi…
pmaciel Jan 12, 2023
044d6c3
modernize-concat-nested-namespaces, modernize-loop-convert, modernize…
pmaciel Feb 15, 2023
50377f5
MIR-646 earthkit-regrid support with interpolationspec
pmaciel Feb 29, 2024
27c96c6
Merge branch 'feature/earthkit-regrid-interpolationspec' into develop
pmaciel Mar 6, 2024
9bb79e8
Cleanup
pmaciel Nov 12, 2024
a5e007a
eckit::stats
pmaciel Dec 8, 2024
98f5f81
Merge remote-tracking branch 'eckit-stats/feature/eckit-stats' into f…
pmaciel Dec 8, 2024
98e4ccf
eckit::stats
pmaciel Dec 8, 2024
8557010
eckit::stats
pmaciel Dec 9, 2024
1ecd639
eckit::stats
pmaciel Dec 9, 2024
e7c22fa
eckit::stats
pmaciel Dec 9, 2024
055bc20
eckit::stats
pmaciel Dec 9, 2024
74e8b31
eckit::stats
pmaciel Dec 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ ecbuild_add_option( FEATURE GEO_PROJECTION_PROJ_DEFAULT

set( eckit_GEO_CACHE_PATH "/tmp/cache" )


### eckit::stats

ecbuild_add_option( FEATURE ECKIT_STATS
DEFAULT OFF
DESCRIPTION "eckit::stats statistics library" )

### LAPACK

if( eckit_HAVE_MKL )
Expand Down
4 changes: 4 additions & 0 deletions src/eckit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -979,3 +979,7 @@ endif()
if( eckit_HAVE_ECKIT_GEO )
add_subdirectory( geo )
endif()

if( eckit_HAVE_ECKIT_STATS )
add_subdirectory( stats )
endif()
58 changes: 58 additions & 0 deletions src/eckit/stats/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
list( APPEND eckit_stats_srcs
Comparator.cc
Comparator.h
Distribution.cc
Distribution.h
Field.cc
Field.h
FieldStatistics.cc
FieldStatistics.h
Method.cc
Method.h
Statistics.cc
Statistics.h
comparator/ComparatorT.cc
comparator/ComparatorT.h
comparator/MissingValues.cc
comparator/MissingValues.h
comparator/Spectral.cc
comparator/Spectral.h
detail/AngleT.cc
detail/AngleT.h
detail/CentralMomentsT.cc
detail/CentralMomentsT.h
detail/Counter.cc
detail/Counter.h
detail/CounterBinary.cc
detail/CounterBinary.h
detail/ModeT.cc
detail/ModeT.h
detail/PNormsT.h
detail/ScalarT.h
distribution/DistributionT.cc
distribution/DistributionT.h
field/CentralMomentStats.cc
field/CentralMomentStats.h
field/CounterStats.cc
field/CounterStats.h
field/ModeStats.cc
field/ModeStats.h
method/MethodT.cc
method/MethodT.h
statistics/SimplePackingEntropy.cc
statistics/SimplePackingEntropy.h
statistics/Spectral.cc
statistics/Spectral.h
statistics/StatisticsT.cc
statistics/StatisticsT.h
util/mutex.h
)

ecbuild_add_library(
TARGET eckit_stats
TYPE SHARED
INSTALL_HEADERS LISTED
HEADER_DESTINATION ${INSTALL_INCLUDE_DIR}/eckit/stats
SOURCES ${eckit_stats_srcs}
PUBLIC_LIBS eckit )

92 changes: 92 additions & 0 deletions src/eckit/stats/Comparator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* (C) Copyright 1996- ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
*
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation nor
* does it submit to any jurisdiction.
*/


#include "eckit/stats/Comparator.h"

#include <map>
#include <ostream>

#include "eckit/exception/Exceptions.h"
#include "eckit/log/Log.h"
#include "eckit/stats/util/Mutex.h"


namespace eckit::stats {


static util::recursive_mutex* local_mutex = nullptr;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need to be a recursive_mutex?

Can we not use the idiom, used elsewhere, with a static instance() method on the Factory type, and the mutex/map being local members. It's a bit cleaner than using posix once.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a wrapper around the eckit types that have been superceded by stl; There's a define above that controls this, and I've put it in one place only so it's easy to change.

I called it recursive mutex because that's what the stl calls it, and what the eckit::Mutex really is. But this can of course change, at the moment this is only for controlling access to the factories. On that note, the factories could move to a eckit-based implementation -- which I recently changed to behave correctly for concurrent access, but the change is non-trivial. I think this is what should likely happen first? In any case, happy to update

static std::map<std::string, ComparatorFactory*>* m = nullptr;
static util::once_flag once;
static void init() {
local_mutex = new util::recursive_mutex();
m = new std::map<std::string, ComparatorFactory*>();
}


Comparator::Comparator(const Parametrisation& param1, const Parametrisation& param2) :
parametrisation1_(param1), parametrisation2_(param2) {}


Comparator::~Comparator() = default;


ComparatorFactory::ComparatorFactory(const std::string& name) : name_(name) {
util::call_once(once, init);
util::lock_guard<util::recursive_mutex> lock(*local_mutex);

if (m->find(name) != m->end()) {
throw SeriousBug("ComparatorFactory: duplicate '" + name + "'");
}

ASSERT(m->find(name) == m->end());
(*m)[name] = this;
}


ComparatorFactory::~ComparatorFactory() {
util::lock_guard<util::recursive_mutex> lock(*local_mutex);

m->erase(name_);
}


void ComparatorFactory::list(std::ostream& out) {
util::call_once(once, init);
util::lock_guard<util::recursive_mutex> lock(*local_mutex);

const char* sep = "";
for (auto& j : *m) {
out << sep << j.first;
sep = ", ";
}
out << std::endl;
}


Comparator* ComparatorFactory::build(const std::string& name, const Parametrisation& param1,
const Parametrisation& param2) {
util::call_once(once, init);
util::lock_guard<util::recursive_mutex> lock(*local_mutex);

Log::debug() << "ComparatorFactory: looking for '" << name << "'" << std::endl;

auto j = m->find(name);
if (j == m->end()) {
list(Log::error() << "ComparatorFactory: unknown '" << name << "', choices are: ");
throw SeriousBug("ComparatorFactory: unknown '" + name + "'");
}

return j->second->make(param1, param2);
}


} // namespace eckit::stats
97 changes: 97 additions & 0 deletions src/eckit/stats/Comparator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* (C) Copyright 1996- ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
*
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation nor
* does it submit to any jurisdiction.
*/


#pragma once

#include <iosfwd>
#include <string>


namespace eckit {
class Parametrisation;
namespace stats {
class Field;
}
} // namespace eckit


namespace eckit::stats {


class Comparator {
public:
// -- Constructors

Comparator(const Parametrisation&, const Parametrisation&);
Comparator(const Comparator&) = delete;

// -- Destructor

virtual ~Comparator();

// -- Operators

void operator=(const Comparator&) = delete;

// -- Methods

virtual std::string execute(const Field&, const Field&) = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::string is a very non-obvious return type for a Comparator. Unless it is doing something weird.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed this sholuld change -- it is a list of reasons why a comparison failed, and if empty is it a successful comparison. I should bring in more (a lot more) semantics here. Good catch!


protected:
// -- Members

const Parametrisation& parametrisation1_;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a touch dangerous. For general use, can we be confident that the supplied Parametrisation will outlive the Comparator instance?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I'm not happy about this. Currently the Comparator is a class supporting comparing two statistics (say mean of two fields), but its use is quite specific to pgen-compare fields1 fields2 (which is in fact mir-compare). I probably should take the Comparator hierarchy out? I don't see that MultIO would use this.

const Parametrisation& parametrisation2_;

// -- Methods

virtual void print(std::ostream&) const = 0;

private:
// -- Friends

friend std::ostream& operator<<(std::ostream& out, const Comparator& r) {
r.print(out);
return out;
}
};


class ComparatorFactory {
std::string name_;
virtual Comparator* make(const Parametrisation&, const Parametrisation&) = 0;

ComparatorFactory(const ComparatorFactory&) = delete;
ComparatorFactory& operator=(const ComparatorFactory&) = delete;

protected:
ComparatorFactory(const std::string&);
virtual ~ComparatorFactory();

public:
static void list(std::ostream&);
static Comparator* build(const std::string&, const Parametrisation&, const Parametrisation&);
};


template <class T>
class ComparatorBuilder : public ComparatorFactory {
Comparator* make(const Parametrisation& param1, const Parametrisation& param2) override {
return new T(param1, param2);
}

public:
ComparatorBuilder(const std::string& name) : ComparatorFactory(name) {}
};


} // namespace eckit::stats
97 changes: 97 additions & 0 deletions src/eckit/stats/Distribution.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* (C) Copyright 1996- ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
*
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation nor
* does it submit to any jurisdiction.
*/


#include "eckit/stats/Distribution.h"

#include <map>
#include <sstream>

#include "eckit/config/YAMLConfiguration.h"
#include "eckit/exception/Exceptions.h"
#include "eckit/log/Log.h"
#include "eckit/stats/util/Mutex.h"


namespace eckit::stats {


static util::once_flag once;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment RE instance() method rather than static global variables.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed -- maybe factories refactoring come first though.

static util::recursive_mutex* local_mutex = nullptr;
static std::map<std::string, DistributionFactory*>* m = nullptr;
static void init() {
local_mutex = new util::recursive_mutex();
m = new std::map<std::string, DistributionFactory*>();
}


Distribution::Distribution() = default;


Distribution::~Distribution() = default;


DistributionFactory::DistributionFactory(const std::string& name) : name_(name) {
util::call_once(once, init);
util::lock_guard<util::recursive_mutex> lock(*local_mutex);

if (m->find(name) != m->end()) {
std::ostringstream oss;
oss << "DistributionFactory: duplicate '" << name << "'";
throw SeriousBug(oss.str());
}

(*m)[name] = this;
}


DistributionFactory::~DistributionFactory() {
util::lock_guard<util::recursive_mutex> lock(*local_mutex);

m->erase(name_);
}


Distribution* DistributionFactory::build(const std::string& name) {
util::call_once(once, init);
util::lock_guard<util::recursive_mutex> lock(*local_mutex);

// parse <name>[{<yaml-arguments>}] (arguments are optional)
auto braces = name.find('{');
ASSERT(braces == std::string::npos || name.back() == '}');

auto key = name.substr(0, braces);
auto yaml = name.substr(braces);

Log::debug() << "DistributionFactory: looking for '" << key << "'" << std::endl;

if (auto j = m->find(key); j != m->end()) {
return j->second->make(YAMLConfiguration{yaml});
}

list(Log::error() << "DistributionFactory: unknown '" << key << "', choices are: ");
Log::warning() << std::endl;
}


void DistributionFactory::list(std::ostream& out) {
util::call_once(once, init);
util::lock_guard<util::recursive_mutex> lock(*local_mutex);

const char* sep = "";
for (const auto& j : *m) {
out << sep << j.first;
sep = ", ";
}
}


} // namespace eckit::stats
Loading
Loading