]> Creatis software - creaContours.git/blob - lib/kernel_ManagerContour_NDimensions/PrefixMaxKeyGenerator.cxx
83fc7f8785553551a29497e813fb11b52b9c80e5
[creaContours.git] / lib / kernel_ManagerContour_NDimensions / PrefixMaxKeyGenerator.cxx
1
2 //----------------------------------------------------------------------------------------------------------------
3 // Class definition include
4 //----------------------------------------------------------------------------------------------------------------
5 #include "PrefixMaxKeyGenerator.h"
6
7 //----------------------------------------------------------------------------------------------------------------
8 // Class implementation
9 //----------------------------------------------------------------------------------------------------------------
10 /** @file PrefixMaxKeyGenerator.cxx */
11
12 //------------------------------------------------------------------------------------------------------------
13 // Constructors & Destructors
14 //------------------------------------------------------------------------------------------------------------
15
16         /*
17         * Creates the prefix+max key generator
18         */
19         PrefixMaxKeyGenerator :: PrefixMaxKeyGenerator()
20         {
21                 numberOfKeyThings = 0;          
22         }
23
24         /*
25         * Destroys the outline manager
26         */
27         PrefixMaxKeyGenerator :: ~PrefixMaxKeyGenerator()
28         {
29                 clear();
30         }
31
32 //------------------------------------------------------------------------------------------------------------
33 // Public Methods
34 //------------------------------------------------------------------------------------------------------------
35
36         
37         /*
38         * Adds a key thing to the keyThings building the respective KeyThing (new keyThing). 
39         * @param theName Is the name of the new keyThing to include. If the name is not unique, returns false.
40         * @param thePrefix Is the prefix of the new keyThing for the key generation correponding to the new keyThing
41         * @param theMax Is the maximum value for the key generation correponding to the new keyThing
42         * @return Returns true if the keyThing could be added of not.
43         */
44         bool PrefixMaxKeyGenerator :: addKeyThing( std::string theName, std::string thePrefix, int theMax )
45         {
46                 KeyThing * kThing = new KeyThing(thePrefix, theMax);
47                 int before = keyThings.size();
48                 keyThings.insert(std::pair < std::string, KeyThing >( theName, *kThing ));
49                 int after = keyThings.size();                   
50                 return after > before;
51         }
52
53         /*
54         * Remove a key thing  
55         * @param theName Is the name of the keyThing to remove. 
56         */
57         void PrefixMaxKeyGenerator :: removeKeyThing( std::string theName )
58         {
59                 keyThings.erase(theName);               
60         }
61
62
63         /*
64         * Indicates if a key thing existis in the generator
65         * @param theName Is the name of the keyThing to search. 
66         * @return Returns true if the keyThing exists in the keyThings.
67         */
68         bool PrefixMaxKeyGenerator :: existsKeyThing( std::string theName )
69         {
70                 return keyThings.find(theName) != keyThings.end();              
71         }
72
73         /*
74         * Updates the maximum value of a key thing if necesary (posibleMax>theMaxOfKeyThing). If the key thing doesn't exist nothing is done.
75         * @param theName Is the name of the keyThing to update.         
76         * @param posibleMax Is the number that corresponds to a posible max value of the keyThing to update. 
77         */
78         void PrefixMaxKeyGenerator :: updateMaxTo( std::string theName, int posibleMax )
79         {
80                 std::map<std::string, KeyThing >::iterator iterP = keyThings.find(theName);
81                 if ( iterP != keyThings.end() )
82                 {
83                         int max = (iterP->second).getValue();
84                         if ( max < posibleMax )
85                                 (iterP->second).setValue(posibleMax);
86                 }
87         }
88
89         /*
90         * Generates a (std::string) key for a given keyThing. If the key thing doesn't exist nothing is done and returns false.
91         * @param theName Is the name of the keyThing to search. 
92         * @param theInputString Is string to load the generated key formed like <prefixOfTheKeyThing> <maxOfTheKeyThing>
93         * @return theKey Returns true if the key was generated successfully. (If theName is an existent keyThing)
94         */
95         bool PrefixMaxKeyGenerator :: generateKeyOf( std::string theName, std::string &theInputString )
96         {
97                 theInputString = "";
98                 std::map<std::string, KeyThing >::iterator iterP = keyThings.find(theName);
99                 if ( iterP != keyThings.end() )
100                 {                       
101                         KeyThing kthing = (iterP->second);
102                         int max = kthing.getValue();                    
103                         std::ostringstream genKey;
104                         genKey<<kthing.getPrefix() << " " << max;
105                         theInputString = genKey.str();
106                         return true;
107                 }
108                 return false;
109         }
110
111         /*
112         * Generates a (std::string) key for a given keyThing and updates the max value of it if necesary. If the key thing doesn't exist nothing is done. 
113         * @param theName Is the name of the keyThing to search. 
114         * @param posibleMax Is the number that corresponds to a posible max value of the keyThing to update. 
115         * @param theInputString Is string to load the generated key formed like <prefixOfTheKeyThing> <maxOfTheKeyThing>
116         * @return Returns true if the key was generated successfully. (If theName is an existent keyThing)
117         */
118         bool PrefixMaxKeyGenerator :: generateKeyOf( std::string theName,  int posibleMax, std::string &theInputString  )
119         {
120                 theInputString = "";
121                 std::map<std::string, KeyThing >::iterator iterP = keyThings.find(theName);
122                 if ( iterP != keyThings.end() )
123                 {       
124                         KeyThing kthing = (iterP->second);
125                         int max = kthing.getValue();
126
127                         if ( max < posibleMax )
128                         {
129                                 kthing.setValue(posibleMax);
130                         }
131
132
133                         std::ostringstream genKey;
134                         genKey<<kthing.getPrefix() << " " << kthing.getValue();
135                         theInputString = genKey.str();
136                         return true;
137                 }
138                 return false;
139         }
140
141         /*
142         * Gets the prefix of a specific keyThing identified with the name in the parameter, if the key thing doesn't exists return false.
143         * @param theName Is the name of the keyThing to search the prefix. 
144         * @param theInputString Is string to load the prefix of the searched keyThing.
145         * @return isStringOutputReal Returns if the loaded string in theInputString is real (i.e if the -theName- keyThing exists)
146         */
147         bool PrefixMaxKeyGenerator :: getPrefixOf( std::string theName, std::string &theInputString )           
148         {
149                 theInputString = "";
150                 std::map<std::string, KeyThing >::iterator iterP = keyThings.find(theName);
151                 if ( iterP != keyThings.end() )
152                 {       
153                         KeyThing kthing = (iterP->second);
154                         theInputString = kthing.getPrefix();
155                         return true;
156                 }
157                 return false;
158         }
159
160         /*
161         * Gets the max value of a specific keyThing identified with the name in the parameter. If the key thing doesn't exists returns -1.
162         * @param theName Is the name of the keyThing to search the maximum.     
163         * @return theMax Returns the maxumum value for key generation at the keyThing. Returns -1 if not finded keyThing.
164         */
165         int PrefixMaxKeyGenerator :: getCurrentMaxOf( std::string theName )
166         {
167                 std::map<std::string, KeyThing >::iterator iterP = keyThings.find(theName);
168                 if ( iterP != keyThings.end() )
169                 {       
170                         KeyThing kthing = (iterP->second);
171                         return kthing.getValue();                       
172                 }
173                 return -1;
174         }
175
176         /*
177         * Gets the total of keyThings managed
178         * @return Retuns the total of keyThing managed
179         */
180         int PrefixMaxKeyGenerator :: getTotalThingsNumber()
181         {
182                 return keyThings.size();
183         }
184
185         /*
186         * Clears the generator deleating the existring keyThings
187         */
188         void PrefixMaxKeyGenerator :: clear()
189         {               
190                 std::map<std::string, KeyThing >::iterator iter = keyThings.begin();
191                 while( iter != keyThings.end() )
192                 {
193                         keyThings.erase(iter);                  
194                 }
195                 keyThings.clear();              
196                 numberOfKeyThings = 0;
197         }