Skip to content

Commit

Permalink
talipot-core/TlpTools: Rename random number functions to randomNumber
Browse files Browse the repository at this point in the history
C++ functions can have the same name if their parameter and return types
differ so better exploiting it to get shorter function names.

Add specialization for unsigned long.

Add typedef ulong for unsigned long.
  • Loading branch information
anlambert committed Nov 16, 2023
1 parent 32d8a3f commit db6c600
Show file tree
Hide file tree
Showing 48 changed files with 204 additions and 194 deletions.
4 changes: 2 additions & 2 deletions library/talipot-core/include/talipot/TLPParser.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright (C) 2019-2022 The Talipot developers
* Copyright (C) 2019-2023 The Talipot developers
*
* Talipot is a fork of Tulip, created by David Auber
* and the Tulip development Team from LaBRI, University of Bordeaux
Expand Down Expand Up @@ -243,7 +243,7 @@ struct TLPTokenParser {
return ERRORINFILE;
}

unsigned long strlength = val.str.length();
ulong strlength = val.str.length();

if (endPtr == (cstr + strlength)) {
val.integer = resultl;
Expand Down
13 changes: 9 additions & 4 deletions library/talipot-core/include/talipot/TlpTools.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright (C) 2019-2022 The Talipot developers
* Copyright (C) 2019-2023 The Talipot developers
*
* Talipot is a fork of Tulip, created by David Auber
* and the Tulip development Team from LaBRI, University of Bordeaux
Expand Down Expand Up @@ -135,17 +135,22 @@ TLP_SCOPE std::mt19937 &getRandomNumberGenerator();
* @brief Returns a random integer in the range [0, max] if max is positive or in the range [max, 0]
* if max is negative
*/
TLP_SCOPE int randomInteger(int max);
TLP_SCOPE int randomNumber(int max);

/**
* @brief Returns a random unsigned integer in the range [0, max]
*/
TLP_SCOPE uint randomUnsignedInteger(uint max);
TLP_SCOPE uint randomNumber(uint max);

/**
* @brief Returns a random unsigned long in the range [0, max]
*/
TLP_SCOPE ulong randomNumber(ulong max);

/**
* @brief Returns a random double in the range [0, max]
*/
TLP_SCOPE double randomDouble(double max = 1.0);
TLP_SCOPE double randomNumber(double max = 1.0);

/**
* @brief Cross-platform function to stat a path on a filesystem.
Expand Down
4 changes: 2 additions & 2 deletions library/talipot-core/include/talipot/cxx/Circle.cxx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright (C) 2019-2020 The Talipot developers
* Copyright (C) 2019-2023 The Talipot developers
*
* Talipot is a fork of Tulip, created by David Auber
* and the Tulip development Team from LaBRI, University of Bordeaux
Expand Down Expand Up @@ -320,7 +320,7 @@ tlp::Circle<Obj, OTYPE> tlp::enclosingCircle(const std::vector<tlp::Circle<Obj,
}

for (unsigned i = circlesSet.size(); i > 0;) {
unsigned idx = tlp::randomUnsignedInteger(i - 1);
unsigned idx = tlp::randomNumber(i - 1);
--i;
std::swap(enclosedCircles[idx], enclosedCircles[i]);
}
Expand Down
4 changes: 2 additions & 2 deletions library/talipot-core/src/GraphAbstract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ node GraphAbstract::getRandomNode() const {
const std::vector<node> &vNodes = nodes();

if (!vNodes.empty()) {
return vNodes[randomUnsignedInteger(vNodes.size() - 1)];
return vNodes[randomNumber(vNodes.size() - 1)];
}

return node();
Expand All @@ -295,7 +295,7 @@ edge GraphAbstract::getRandomEdge() const {
const std::vector<edge> &vEdges = edges();

if (!vEdges.empty()) {
return vEdges[randomUnsignedInteger(vEdges.size() - 1)];
return vEdges[randomNumber(vEdges.size() - 1)];
}

return edge();
Expand Down
8 changes: 4 additions & 4 deletions library/talipot-core/src/PluginLibraryLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,18 @@ int __talipot_select_libs(struct dirent *ent) {
#endif
#if !defined(__APPLE__)
const char *suffix = ".so";
const unsigned long suffix_len = 3;
const ulong suffix_len = 3;
#else
const char *suffix = ".dylib";
const unsigned long suffix_len = 6;
const ulong suffix_len = 6;
#endif
int idx = strlen(ent->d_name) - suffix_len;

if (idx < 0) {
return 0;
}

for (unsigned long i = 0; i < suffix_len; ++i) {
for (ulong i = 0; i < suffix_len; ++i) {
if ((ent->d_name[idx + i]) != suffix[i]) {
return 0;
}
Expand Down Expand Up @@ -263,7 +263,7 @@ bool PluginLibraryLoader::initPluginDir(PluginLoader *loader, bool recursive,

if (loader != nullptr) {
// count files loop
unsigned long nbFiles = 0;
ulong nbFiles = 0;

if (hFind != INVALID_HANDLE_VALUE) {
nbFiles = 1;
Expand Down
15 changes: 12 additions & 3 deletions library/talipot-core/src/TlpTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ TLP_SCOPE std::mt19937 &tlp::getRandomNumberGenerator() {
return mt;
}

int tlp::randomInteger(int max) {
int tlp::randomNumber(int max) {
if (max == 0) {
return 0;
} else if (max > 0) {
Expand All @@ -345,7 +345,7 @@ int tlp::randomInteger(int max) {
}
}

uint tlp::randomUnsignedInteger(uint max) {
uint tlp::randomNumber(uint max) {
if (max == 0) {
return 0;
} else {
Expand All @@ -354,7 +354,16 @@ uint tlp::randomUnsignedInteger(uint max) {
}
}

double tlp::randomDouble(double max) {
ulong tlp::randomNumber(ulong max) {
if (max == 0) {
return 0;
} else {
std::uniform_int_distribution<ulong> dist(0, max);
return dist(mt);
}
}

double tlp::randomNumber(double max) {
std::uniform_real_distribution<double> dist(0, std::nextafter(max, DBL_MAX));
return dist(mt);
}
Expand Down
4 changes: 2 additions & 2 deletions library/talipot-gui/src/CSVParser.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright (C) 2019-2022 The Talipot developers
* Copyright (C) 2019-2023 The Talipot developers
*
* Talipot is a fork of Tulip, created by David Auber
* and the Tulip development Team from LaBRI, University of Bordeaux
Expand Down Expand Up @@ -57,7 +57,7 @@ bool CSVSimpleParser::parse(CSVContentHandler *handler, PluginProgress *progress

csvFile->seekg(0, std::ios_base::end);
// get position = file size
unsigned long fileSize = csvFile->tellg(), readSize = 0;
ulong fileSize = csvFile->tellg(), readSize = 0;
// reset position
csvFile->seekg(0, std::ios_base::beg);
string line;
Expand Down
10 changes: 4 additions & 6 deletions library/talipot-ogl/include/talipot/GlQuantitativeAxis.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright (C) 2019-2021 The Talipot developers
* Copyright (C) 2019-2023 The Talipot developers
*
* Talipot is a fork of Tulip, created by David Auber
* and the Tulip development Team from LaBRI, University of Bordeaux
Expand Down Expand Up @@ -66,17 +66,15 @@ class TLP_GL_SCOPE GlQuantitativeAxis : public GlAxis {
const LabelPosition &axisGradsLabelsPosition = LEFT_OR_BELOW,
const bool drawFirstLabel = true);

void setAxisParameters(const long long min, const long long max,
const unsigned long long incrementStep,
void setAxisParameters(const long long min, const long long max, const ulong incrementStep,
const LabelPosition &axisGradsLabelsPosition = LEFT_OR_BELOW,
const bool drawFirstLabel = true);

void setAxisParameters(const int min, const int max, const uint incrementStep,
const LabelPosition &axisGradsLabelsPosition = LEFT_OR_BELOW,
const bool drawFirstLabel = true) {
setAxisParameters(static_cast<long long>(min), static_cast<long long>(max),
static_cast<unsigned long long>(incrementStep), axisGradsLabelsPosition,
drawFirstLabel);
static_cast<ulong>(incrementStep), axisGradsLabelsPosition, drawFirstLabel);
}

void setNbGraduations(const uint nbGraduations) {
Expand Down Expand Up @@ -151,7 +149,7 @@ class TLP_GL_SCOPE GlQuantitativeAxis : public GlAxis {
bool logScale;
uint logBase;
bool integerScale;
unsigned long long incrementStep;
ulong incrementStep;
bool minMaxSet;
};
}
Expand Down
4 changes: 2 additions & 2 deletions library/talipot-ogl/src/GlQuantitativeAxis.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright (C) 2019-2021 The Talipot developers
* Copyright (C) 2019-2023 The Talipot developers
*
* Talipot is a fork of Tulip, created by David Auber
* and the Tulip development Team from LaBRI, University of Bordeaux
Expand Down Expand Up @@ -62,7 +62,7 @@ void GlQuantitativeAxis::setAxisParameters(const double minV, const double maxV,
}

void GlQuantitativeAxis::setAxisParameters(const long long minV, const long long maxV,
const unsigned long long incrementStepV,
const ulong incrementStepV,
const LabelPosition &axisGradsLabelsPos,
const bool firstLabel) {
integerScale = true;
Expand Down
6 changes: 3 additions & 3 deletions library/talipot-python/bindings/stl/std_list.sip
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,10 @@ return sipGetState(sipTransferObj);
};

// ****************************************************
// Specialization for std::list<unsigned long>
// Specialization for std::list<ulong>
// ****************************************************

%MappedType std::list<unsigned long> /TypeHint="List[int]"/ {
%MappedType std::list<ulong> /TypeHint="List[int]"/ {
%TypeHeaderCode
#include <list>
%End
Expand Down Expand Up @@ -514,7 +514,7 @@ if (sipIsErr == NULL) {
}

// Convert Python list of integers to a std::vector<int>
std::list<unsigned long> *v = new std::list<unsigned long>();
std::list<ulong> *v = new std::list<ulong>();
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(sipPy); ++i) {
v->push_back(PyLong_AsUnsignedLong(PyList_GET_ITEM(sipPy, i)));
}
Expand Down
8 changes: 4 additions & 4 deletions library/talipot-python/bindings/stl/std_set.sip
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ return sipGetState(sipTransferObj);

};

%MappedType std::set<unsigned long> /TypeHint="Set[int]"/ {
%MappedType std::set<ulong> /TypeHint="Set[int]"/ {
%TypeHeaderCode
#include <set>
%End
Expand All @@ -532,7 +532,7 @@ if ((l = PySet_New(NULL)) == NULL) {

// Go through each element in the C++ instance and convert it to a
// wrapper object.
for (unsigned long n : *sipCpp) {
for (ulong n : *sipCpp) {
// Add the wrapper to the list.
PySet_Add(l, PyLong_FromUnsignedLong(n));
}
Expand Down Expand Up @@ -561,8 +561,8 @@ if (sipIsErr == NULL) {
return 1;
}

// Convert Python set of integers to a std::set<unsigned long>
std::set<unsigned long> *s = new std::set<unsigned long>();
// Convert Python set of integers to a std::set<ulong>
std::set<ulong> *s = new std::set<ulong>();

PyObject *iterator = PyObject_GetIter(sipPy);
PyObject *item = NULL;
Expand Down
6 changes: 3 additions & 3 deletions library/talipot-python/bindings/stl/std_vector.sip
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,10 @@ return sipGetState(sipTransferObj);
};

// ****************************************************
// Specialization for std::vector<unsigned long>
// Specialization for std::vector<ulong>
// ****************************************************

%MappedType std::vector<unsigned long> /TypeHint="List[int]"/ {
%MappedType std::vector<ulong> /TypeHint="List[int]"/ {
%TypeHeaderCode
#include <vector>
%End
Expand Down Expand Up @@ -523,7 +523,7 @@ if (sipIsErr == NULL) {
}

// Convert Python list of integers to a std::vector<int>
std::vector<unsigned long> *v = new std::vector<unsigned long>();
std::vector<ulong> *v = new std::vector<ulong>();
v->reserve(PyList_GET_SIZE(sipPy));
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(sipPy); ++i) {
v->push_back(PyLong_AsUnsignedLong(PyList_GET_ITEM(sipPy, i)));
Expand Down
18 changes: 9 additions & 9 deletions library/talipot-python/include/talipot/PythonCppTypesConverter.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright (C) 2019-2021 The Talipot developers
* Copyright (C) 2019-2023 The Talipot developers
*
* Talipot is a fork of Tulip, created by David Auber
* and the Tulip development Team from LaBRI, University of Bordeaux
Expand Down Expand Up @@ -46,8 +46,8 @@ TLP_PYTHON_SCOPE PyObject *convertDoubleToPyObject(double cppObject);
TLP_PYTHON_SCOPE bool convertPyObjectToLong(PyObject *pyObject, long &cppObject);
TLP_PYTHON_SCOPE PyObject *convertLongToPyObject(long cppObject);

TLP_PYTHON_SCOPE bool convertPyObjectToUnsignedLong(PyObject *pyObject, unsigned long &cppObject);
TLP_PYTHON_SCOPE PyObject *convertUnsignedLongToPyObject(unsigned long cppObject);
TLP_PYTHON_SCOPE bool convertPyObjectToUnsignedLong(PyObject *pyObject, ulong &cppObject);
TLP_PYTHON_SCOPE PyObject *convertUnsignedLongToPyObject(ulong cppObject);

class TLP_PYTHON_SCOPE ValueSetter {

Expand Down Expand Up @@ -176,9 +176,9 @@ class PyObjectToCppObjectConverter<int> {
};

template <>
class PyObjectToCppObjectConverter<unsigned long> {
class PyObjectToCppObjectConverter<ulong> {
public:
bool convert(PyObject *pyObject, unsigned long &cppObject) {
bool convert(PyObject *pyObject, ulong &cppObject) {
return convertPyObjectToUnsignedLong(pyObject, cppObject);
}
};
Expand All @@ -187,8 +187,8 @@ template <>
class PyObjectToCppObjectConverter<uint> {
public:
bool convert(PyObject *pyObject, uint &cppObject) {
unsigned long val = 0;
PyObjectToCppObjectConverter<unsigned long> converter;
ulong val = 0;
PyObjectToCppObjectConverter<ulong> converter;
bool ok = converter.convert(pyObject, val);
cppObject = val;
return ok;
Expand Down Expand Up @@ -280,9 +280,9 @@ class CppObjectToPyObjectConverter<uint> {
};

template <>
class CppObjectToPyObjectConverter<unsigned long> {
class CppObjectToPyObjectConverter<ulong> {
public:
bool convert(const unsigned long &cppObject, PyObject *&pyObject) {
bool convert(const ulong &cppObject, PyObject *&pyObject) {
pyObject = convertUnsignedLongToPyObject(cppObject);
return true;
}
Expand Down
Loading

0 comments on commit db6c600

Please sign in to comment.