Skip to content

Commit 74e9d88

Browse files
author
Martin D. Weinberg
committed
Added a removeUnits() member function for completeness
1 parent 02675c7 commit 74e9d88

4 files changed

Lines changed: 51 additions & 6 deletions

File tree

expui/Coefficients.H

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ namespace CoefClasses
239239
void setUnits
240240
(const std::vector<std::tuple<std::string, std::string, float>>& units);
241241

242+
//! Remove a unit by name
243+
void removeUnits(const std::string name);
244+
242245
//! Write units to H5
243246
void WriteH5Units(HighFive::File& file);
244247

expui/Coefficients.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ namespace CoefClasses
7171
p->times = times;
7272
}
7373

74+
void Coefs::removeUnits(const std::string name)
75+
{
76+
// Lambda test for matching name
77+
auto test = [&name](Unit& elem) -> bool{return elem.name==name; };
78+
79+
// Explanation: std::remove_if shifts elements to be removed to
80+
// the end of the range and returns an iterator to the new logical
81+
// end. units.erase() then removes the elements from that point to
82+
// the end.
83+
units.erase(std::remove_if(units.begin(), units.end(), test), units.end());
84+
}
85+
86+
7487
void Coefs::setUnits
7588
(const std::string Name, const std::string Unit, const float Value)
7689
{

expui/UnitValidator.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ UnitValidator::createAllowedUnitTypes()
5454
allowed["L"] = "length";
5555
allowed["m"] = "mass";
5656
allowed["M"] = "mass";
57+
allowed["t"] = "time";
58+
allowed["T"] = "time";
5759
allowed["vel"] = "velocity";
5860
allowed["Vel"] = "velocity";
5961
allowed["v"] = "velocity";

pyEXP/CoefWrappers.cc

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,19 @@ void CoefficientClasses(py::module &m) {
202202
void zerodata() override {
203203
PYBIND11_OVERRIDE_PURE(void, Coefs, zerodata,);
204204
}
205+
206+
void setUnits(const std::string name, const std::string unit, const float value) {
207+
PYBIND11_OVERRIDE(void, Coefs, setUnits, name, unit, value);
208+
}
209+
210+
void setUnits(const std::vector<std::tuple<std::string, std::string, float>>& units) {
211+
PYBIND11_OVERRIDE(void, Coefs, setUnits, units);
212+
}
213+
214+
void removeUnits(const std::string name) {
215+
PYBIND11_OVERRIDE(void, Coefs, removeUnits, name);
216+
}
217+
205218
};
206219

207220
class PySphCoefs : public SphCoefs
@@ -1167,13 +1180,27 @@ void CoefficientClasses(py::module &m) {
11671180
.def("Times",
11681181
&CoefClasses::Coefs::Times,
11691182
R"(
1170-
Return a list of times for coefficient sets currently in the container
1183+
Return a list of times for coefficient sets currently in the container
11711184
1172-
Returns
1173-
-------
1174-
list(float,...)
1175-
list of times
1176-
)")
1185+
Returns
1186+
-------
1187+
list(float,...)
1188+
list of times
1189+
)")
1190+
.def("removeUnits",
1191+
&CoefClasses::Coefs::removeUnits,
1192+
R"(
1193+
Remove a unit from the coefficient structure.
1194+
1195+
Parameters
1196+
----------
1197+
name : str
1198+
the name of physical quantity (G, Length, Mass, Time, etc)
1199+
1200+
Returns
1201+
-------
1202+
None
1203+
)")
11771204
.def("setUnits",
11781205
py::overload_cast<const std::string, const std::string, const float>(&CoefClasses::Coefs::setUnits),
11791206
R"(

0 commit comments

Comments
 (0)