Changeset 1298 for cpp


Ignore:
Timestamp:
03/29/24 23:30:34 (4 weeks ago)
Author:
Maciej Komosinski
Message:

Introduced overloads for rndUint() with size_t and int arguments to avoid numerous type casts in sources

Location:
cpp
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/nonstd_math.cpp

    r1280 r1298  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2023  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2024  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    88#include <sstream>
    99#include <algorithm> // std::min()
     10#include <common/log.h>
     11
     12
     13unsigned int rndUint(int limit_exclusive)
     14{
     15        if (limit_exclusive < 0)
     16        {
     17                logPrintf("", "rndUint", LOG_ERROR, "rndUint(negative: %d)", limit_exclusive);
     18                return 0;
     19        }
     20        else return rndUint((unsigned int)limit_exclusive);
     21}
     22
    1023
    1124RandomGenerator &rndGetInstance()
     
    125138#include <fenv.h>
    126139
     140#ifdef __CYGWIN__ //since my cygwin update in 2024 (g++ (GCC) 11.4.0), these two are no longer found:
     141 #define feenableexcept(x)
     142 #define fedisableexcept(x)
     143#endif
     144
    127145namespace fpExcept
    128146{
     
    203221
    204222#endif
     223
  • cpp/common/nonstd_math.h

    r1275 r1298  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2024  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    99#ifdef _MSC_VER
    1010 #define _USE_MATH_DEFINES //after this is defined, the next #include <math.h> or <cmath> will define M_PI etc.
    11  #include <math.h> //w vc2008 dzia�a�o tu <cmath>, ale w vc2010 juz nie bo "co�" (jaki� inny .h stl'a?) includuje wcze�niej <cmath> bez _USE_MATH_DEFINES, a <cmath> includuje <math.h> (ale tylko raz bo ma "include guards" jak kazdy .h)
     11 #include <math.h> //in vc2008, <cmath> worked here, but no longer in vc2010 because "something" (some other .h from stl?) earlier includes <cmath> without _USE_MATH_DEFINES, and <cmath> includes <math.h> (just once because it has "include guards" like any other .h)
    1212 #include <float.h>
    1313 //#define isnan(x) _isnan(x) //since 2014 we use std::isnan()
    1414 #define finite(x) _finite(x)
    15 #else //m.in. __BORLANDC__
     15#else //e.g. __BORLANDC__
    1616 #include <math.h>
    1717#endif
     
    2525inline double rndDouble(double limit_exclusive) { return rndGetInstance().getDouble() * limit_exclusive; }
    2626inline unsigned int rndUint(unsigned int limit_exclusive) { return (unsigned int)(rndGetInstance().getDouble() * limit_exclusive); } //returns random from 0..limit_exclusive-1
     27inline unsigned int rndUint(size_t limit_exclusive) {return rndUint((unsigned int)limit_exclusive);} //just an overload with size_t argument
     28unsigned int rndUint(int limit_exclusive); //just an overload with int argument
    2729inline void rndSetSeed(unsigned int seed) { rndGetInstance().setSeed(seed); }
    2830inline unsigned int rndRandomizeSeed() { return rndGetInstance().randomize(); }
  • cpp/frams/genetics/f4/f4_oper.cpp

    r1274 r1298  
    253253                                return GENOPER_OPFAIL;
    254254
    255                         node_mutated = candidate_nodes[rndUint((unsigned int)candidate_nodes.size())];
     255                        node_mutated = candidate_nodes[rndUint(candidate_nodes.size())];
    256256                        f4_Node *node_mutated_parent = node_mutated->parent;
    257257
  • cpp/frams/genetics/fB/fB_oper.cpp

    r1273 r1298  
    252252        {
    253253                std::list<SString> tokenized = tokenizeSequence(line);
    254                 int rndid = rndUint((int)tokenized.size()); // select random letter from genotype
     254                int rndid = rndUint(tokenized.size()); // select random letter from genotype
    255255                // increment/decrement character - when overflow happens, this method
    256256                // uses the "reflect" approach
     
    291291                std::list<SString> tokenized = tokenizeSequence(line);
    292292                std::list<SString>::iterator it = tokenized.begin();
    293                 int rndid = rndUint((int)tokenized.size()); // select random insertion point
     293                int rndid = rndUint(tokenized.size()); // select random insertion point
    294294                std::advance(it, rndid);
    295295                NeuroClass *cls = getRandomNeuroClass(Model::SHAPETYPE_BALL_AND_STICK);
     
    312312                chg = 1.0 / line.length();
    313313                std::list<SString> tokenized = tokenizeSequence(line);
    314                 int rndid = rndUint((int)tokenized.size()); // select random insertion point
     314                int rndid = rndUint(tokenized.size()); // select random insertion point
    315315                std::list<SString>::iterator it = tokenized.begin();
    316316                std::advance(it, rndid);
     
    326326                std::list<SString> tokenized = tokenizeSequence(line);
    327327                std::list<SString>::iterator it = tokenized.begin();
    328                 int rndid = rndUint((int)tokenized.size()); // select random deletion point
     328                int rndid = rndUint(tokenized.size()); // select random deletion point
    329329                std::advance(it, rndid);
    330330                tokenized.erase(it);
     
    348348                for (int i = 0; i < 4; i++)
    349349                {
    350                         cuts[i] = rndUint((int)tokenized.size());
     350                        cuts[i] = rndUint(tokenized.size());
    351351                }
    352352                std::sort(cuts.begin(), cuts.end());
  • cpp/frams/genetics/fF/fF_oper.cpp

    r1280 r1298  
    4949        par.load(gene);
    5050        static const int propsToMutate[] = fF_PROPS_TO_MUTATE;
    51         int which = rndUint((unsigned int)std::size(propsToMutate));
     51        int which = rndUint(std::size(propsToMutate));
    5252        bool mutated_ok = GenoOperators::mutatePropertyNaive(par.param, propsToMutate[which]);
    5353        if (mutated_ok)
  • cpp/frams/genetics/fL/fL_oper.cpp

    r1273 r1298  
    181181        else
    182182        {
    183                 int rid = rndUint((unsigned int)creature->rules.size());
     183                int rid = rndUint(creature->rules.size());
    184184                list = &creature->rules[rid]->objsucc;
    185185                numparams = creature->rules[rid]->objpred->npar;
     
    313313                if (creature->rules.size() > 0)
    314314                {
    315                         int ruleid = rndUint((unsigned int)creature->rules.size());
     315                        int ruleid = rndUint(creature->rules.size());
    316316                        if (!creature->rules[ruleid]->condeval)
    317317                        {
     
    350350                if (wordswithnorules.size() > 0)
    351351                {
    352                         int predid = rndUint((unsigned int)wordswithnorules.size());
     352                        int predid = rndUint(wordswithnorules.size());
    353353                        fL_Rule *newrule = new fL_Rule(0, 0);
    354354                        fL_Word *pred = new fL_Word();
     
    362362                else if (creature->rules.size() > 0)
    363363                {
    364                         int ruleid = rndUint((unsigned int)creature->rules.size());
     364                        int ruleid = rndUint(creature->rules.size());
    365365                        fL_Rule *newrule = new fL_Rule(0, 0);
    366366                        fL_Word *pred = new fL_Word();
     
    465465                else
    466466                {
    467                         int rndid = rndUint((unsigned int)list->size());
     467                        int rndid = rndUint(list->size());
    468468                        std::list<fL_Word *>::iterator it = list->begin();
    469469                        std::advance(it, rndid);
     
    492492                int tmp = 0;
    493493                std::list<fL_Word *> *list = selectRandomSequence(creature, numpars, tmp);
    494                 int rndid = rndUint((unsigned int)list->size());
     494                int rndid = rndUint(list->size());
    495495                std::list<fL_Word *>::iterator it = list->begin();
    496496                std::advance(it, rndid);
     
    541541                int tmp = 0;
    542542                std::list<fL_Word *> *list = selectRandomSequence(creature, numpars, tmp);
    543                 int rndid = rndUint((unsigned int)list->size());
     543                int rndid = rndUint(list->size());
    544544                std::list<fL_Word *>::iterator selectedword = list->begin();
    545545                std::advance(selectedword, rndid);
     
    555555                        int numpars = 0;
    556556                        std::list<fL_Word *> *list = selectRandomSequence(creature, numpars, tmp);
    557                         int rndid = rndUint((unsigned int)list->size());
     557                        int rndid = rndUint(list->size());
    558558                        std::list<fL_Word *>::iterator it = list->begin();
    559559                        std::advance(it, rndid);
     
    587587                                if (available.size() > 0)
    588588                                {
    589                                         int newnameid = rndUint((unsigned int)available.size());
     589                                        int newnameid = rndUint(available.size());
    590590                                        (*selectedword)->name = available[newnameid]->name;
    591591                                }
     
    762762                for (int i = 0; i < numselrules; i++)
    763763                {
    764                         int rulid = rndUint((unsigned int)from->rules.size());
     764                        int rulid = rndUint(from->rules.size());
    765765                        fL_Rule *rul = from->rules[rulid];
    766766                        fL_Rule *newrule = new fL_Rule(0, 0);
Note: See TracChangeset for help on using the changeset viewer.