NCBI C++ Toolkit Cross Reference

C++/src/util/distribution.cpp


  1 /*  $Id: distribution.cpp 159154 2009-05-01 20:26:11Z kazimird $
  2  * ===========================================================================
  3  *
  4  *                            PUBLIC DOMAIN NOTICE
  5  *               National Center for Biotechnology Information
  6  *
  7  *  This software/database is a "United States Government Work" under the
  8  *  terms of the United States Copyright Act.  It was written as part of
  9  *  the author's official duties as a United States Government employee and
 10  *  thus cannot be copyrighted.  This software/database is freely available
 11  *  to the public for use. The National Library of Medicine and the U.S.
 12  *  Government have not placed any restriction on its use or reproduction.
 13  *
 14  *  Although all reasonable efforts have been taken to ensure the accuracy
 15  *  and reliability of the software and data, the NLM and the U.S.
 16  *  Government do not and cannot warrant the performance or results that
 17  *  may be obtained by using this software or data. The NLM and the U.S.
 18  *  Government disclaim all warranties, express or implied, including
 19  *  warranties of performance, merchantability or fitness for any particular
 20  *  purpose.
 21  *
 22  *  Please cite the author in any work or product based on this material.
 23  *
 24  * ===========================================================================
 25  *
 26  * Authors:
 27  *   Dmitry Kazimirov
 28  *
 29  * File Description:
 30  *   Implementations of the CDiscreteDistribution class.
 31  */
 32 
 33 #include <ncbi_pch.hpp>
 34 
 35 #include <util/distribution.hpp>
 36 
 37 BEGIN_NCBI_SCOPE
 38 
 39 const char* CInvalidParamException::GetErrCodeString(void) const
 40 {
 41     switch (GetErrCode()) {
 42     case eUndefined:
 43         return "eUndefined";
 44 
 45     case eNotANumber:
 46         return "eNotANumber";
 47 
 48     case eInvalidCharacter:
 49         return "eInvalidCharacter";
 50 
 51     default:
 52         return CException::GetErrCodeString();
 53     }
 54 }
 55 
 56 void CDiscreteDistribution::InitFromParameter(const char* parameter_name,
 57     const char* parameter_value, CRandom* random_gen)
 58 {
 59     m_RandomGen = random_gen;
 60 
 61     if (*parameter_value == '\0') {
 62         NCBI_THROW(CInvalidParamException, eUndefined,
 63             string("Configuration parameter '") + parameter_name +
 64                 "' was not defined.");
 65     }
 66 
 67     m_RangeVector.clear();
 68 
 69     const char* pos = parameter_value;
 70 
 71     TRange new_range;
 72 
 73     unsigned* current_bound_ptr = &new_range.first;
 74     new_range.second = 0;
 75 
 76     for (;;) {
 77         pos = SkipSpaces(pos);
 78 
 79         unsigned bound = (unsigned) (*pos - '0');
 80 
 81         if (bound > 9) {
 82             NCBI_THROW(CInvalidParamException, eNotANumber,
 83                 string("In configuration parameter '") + parameter_name +
 84                     "': not a number at position " +
 85                         NStr::UIntToString(pos - parameter_value + 1) + ".");
 86         }
 87 
 88         unsigned digit;
 89 
 90         while ((digit = (unsigned) (*++pos - '0')) <= 9)
 91             bound = bound * 10 + digit;
 92 
 93         *current_bound_ptr = bound;
 94 
 95         pos = SkipSpaces(pos);
 96 
 97         switch (*pos) {
 98         case '\0':
 99             m_RangeVector.push_back(new_range);
100             return;
101 
102         case ',':
103             m_RangeVector.push_back(new_range);
104             ++pos;
105             current_bound_ptr = &new_range.first;
106             new_range.second = 0;
107             break;
108 
109         case '-':
110             ++pos;
111             current_bound_ptr = &new_range.second;
112             break;
113 
114         default:
115             NCBI_THROW(CInvalidParamException, eInvalidCharacter,
116                 string("In configuration parameter '") + parameter_name +
117                     "': invalid character at position " +
118                         NStr::UIntToString(pos - parameter_value + 1) + ".");
119         }
120     }
121 }
122 
123 unsigned CDiscreteDistribution::GetNextValue() const
124 {
125     CRandom::TValue random_number = m_RandomGen->GetRand();
126 
127     TRangeVector::const_iterator random_range =
128         m_RangeVector.begin() + (random_number % m_RangeVector.size());
129 
130     return random_range->second == 0 ? random_range->first :
131         random_range->first +
132             (random_number % (random_range->second - random_range->first + 1));
133 }
134 
135 const char* CDiscreteDistribution::SkipSpaces(const char* input_string)
136 {
137     while (*input_string == ' ' || *input_string == '\t')
138         ++input_string;
139 
140     return input_string;
141 }
142 
143 END_NCBI_SCOPE
144 

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.