Remove dynamic exceptions
[anna.git] / source / core / util / MultiRangeExpression.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 // Local
10 #include <anna/core/util/MultiRangeExpression.hpp>
11
12 #include <anna/core/util/Tokenizer.hpp>
13 #include <anna/core/functions.hpp>
14
15 // Standard
16 #include <limits.h>
17
18
19 //using namespace anna;
20
21
22 // private
23
24 //------------------------------------------------------------------------------
25 //---------------------------------------------- MultiRangeExpression::refresh()
26 //------------------------------------------------------------------------------
27 void anna::MultiRangeExpression::refresh(void) {
28   anna::Tokenizer ranges, borders;
29   anna::Tokenizer::const_iterator ranges_it, borders_it;
30   std::string range;
31   unsigned int min, max;
32   a_data.clear();
33
34   if(a_literal == "") return;
35
36   ranges.apply(a_literal, ",");
37
38   for(ranges_it = ranges.begin(); ranges_it != ranges.end(); ranges_it ++) {
39     range = anna::Tokenizer::data(ranges_it);
40     borders.apply(range, "-");
41     borders_it = borders.begin();
42
43     if(borders_it != borders.end()) {
44       min = atoi(anna::Tokenizer::data(borders_it));
45       max = min;
46       borders_it++;
47
48       if(borders_it != borders.end()) {
49         max = atoi(anna::Tokenizer::data(borders_it));
50       }
51
52       // Update a_data:
53       for(unsigned int k = min; k <= max; k++) {
54         a_data[k] = 0;
55
56         if(k == UINT_MAX/* overflow */) break;
57       }
58     }
59   }
60 }
61
62
63 //------------------------------------------------------------------------------
64 //----------------------------------- MultiRangeExpression::getExpandedLiteral()
65 //------------------------------------------------------------------------------
66 std::string anna::MultiRangeExpression::getExpandedLiteral(void) const {
67   std::string result;
68   std::map < unsigned int, int/*dummy*/ >::const_iterator it;
69   std::map < unsigned int, int/*dummy*/ >::const_iterator it_min(a_data.begin());
70   std::map < unsigned int, int/*dummy*/ >::const_iterator it_max(a_data.end());
71
72   for(it = it_min; it != it_max; it++) {
73     result += anna::functions::asString((*it).first);
74     result += ",";
75   }
76
77   int pos = result.size();
78
79   if(pos) result.erase(pos - 1);
80
81   return (result);
82 }
83
84
85 //------------------------------------------------------------------------------
86 //-------------------------------------- MultiRangeExpression::simplifyLiteral()
87 //------------------------------------------------------------------------------
88 const char * anna::MultiRangeExpression::simplifyLiteral(void) {
89   if(a_data.size() == 0) return NULL;
90
91   std::map < unsigned int, int/*dummy*/ >::const_iterator it;
92   std::map < unsigned int, int/*dummy*/ >::const_iterator it_min(a_data.begin());
93   std::map < unsigned int, int/*dummy*/ >::const_iterator it_max(a_data.end());
94   unsigned int min = UINT_MAX;
95   unsigned int max = 0;
96   unsigned int value;
97   unsigned int prevValue = a_data.begin()->first;
98   a_literal = "";
99
100   for(it = it_min; it != it_max; it++) {
101     value = (*it).first;
102
103     if(value < min) min = value;
104
105     if(value - prevValue > 1) {
106       a_literal += anna::functions::asString(min);
107       a_literal += "-";
108       a_literal += anna::functions::asString(max);
109       a_literal += ",";
110       min = value;
111     }
112
113     if(value > max) max = value;
114
115     prevValue = value;
116   }
117
118   a_literal += anna::functions::asString(min);
119
120   if(max != min) {
121     a_literal += "-";
122     a_literal += anna::functions::asString(max);
123   }
124
125   return a_literal.c_str();
126 }
127