]> Creatis software - creaContours.git/blob - lib/kernel_ManagerContour_NDimensions/PrefixMaxKeyGenerator.cxx
Feature #1772 Add licence terms for all files.
[creaContours.git] / lib / kernel_ManagerContour_NDimensions / PrefixMaxKeyGenerator.cxx
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
8 #
9 #  This software is governed by the CeCILL-B license under French law and
10 #  abiding by the rules of distribution of free software. You can  use,
11 #  modify and/ or redistribute the software under the terms of the CeCILL-B
12 #  license as circulated by CEA, CNRS and INRIA at the following URL
13 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 #  or in the file LICENSE.txt.
15 #
16 #  As a counterpart to the access to the source code and  rights to copy,
17 #  modify and redistribute granted by the license, users are provided only
18 #  with a limited warranty  and the software's author,  the holder of the
19 #  economic rights,  and the successive licensors  have only  limited
20 #  liability.
21 #
22 #  The fact that you are presently reading this means that you have had
23 #  knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
25
26
27 //----------------------------------------------------------------------------------------------------------------
28 // Class definition include
29 //----------------------------------------------------------------------------------------------------------------
30 #include "PrefixMaxKeyGenerator.h"
31
32 //----------------------------------------------------------------------------------------------------------------
33 // Class implementation
34 //----------------------------------------------------------------------------------------------------------------
35 /** @file PrefixMaxKeyGenerator.cxx */
36
37 //------------------------------------------------------------------------------------------------------------
38 // Constructors & Destructors
39 //------------------------------------------------------------------------------------------------------------
40
41         /*
42         * Creates the prefix+max key generator
43         */
44         PrefixMaxKeyGenerator :: PrefixMaxKeyGenerator()
45         {
46                 numberOfKeyThings = 0;          
47         }
48
49         /*
50         * Destroys the outline manager
51         */
52         PrefixMaxKeyGenerator :: ~PrefixMaxKeyGenerator()
53         {
54                 clear();
55         }
56
57 //------------------------------------------------------------------------------------------------------------
58 // Public Methods
59 //------------------------------------------------------------------------------------------------------------
60
61         
62         /*
63         * Adds a key thing to the keyThings building the respective KeyThing (new keyThing). 
64         * @param theName Is the name of the new keyThing to include. If the name is not unique, returns false.
65         * @param thePrefix Is the prefix of the new keyThing for the key generation correponding to the new keyThing
66         * @param theMax Is the maximum value for the key generation correponding to the new keyThing
67         * @return Returns true if the keyThing could be added of not.
68         */
69         bool PrefixMaxKeyGenerator :: addKeyThing( std::string theName, std::string thePrefix, int theMax )
70         {
71                 KeyThing * kThing = new KeyThing(thePrefix, theMax);
72                 int before = keyThings.size();
73                 keyThings.insert(std::pair < std::string, KeyThing >( theName, *kThing ));
74                 int after = keyThings.size();                   
75                 return after > before;
76         }
77
78         /*
79         * Remove a key thing  
80         * @param theName Is the name of the keyThing to remove. 
81         */
82         void PrefixMaxKeyGenerator :: removeKeyThing( std::string theName )
83         {
84                 keyThings.erase(theName);               
85         }
86
87
88         /*
89         * Indicates if a key thing existis in the generator
90         * @param theName Is the name of the keyThing to search. 
91         * @return Returns true if the keyThing exists in the keyThings.
92         */
93         bool PrefixMaxKeyGenerator :: existsKeyThing( std::string theName )
94         {
95                 return keyThings.find(theName) != keyThings.end();              
96         }
97
98         /*
99         * Updates the maximum value of a key thing if necesary (posibleMax>theMaxOfKeyThing). If the key thing doesn't exist nothing is done.
100         * @param theName Is the name of the keyThing to update.         
101         * @param posibleMax Is the number that corresponds to a posible max value of the keyThing to update. 
102         */
103         void PrefixMaxKeyGenerator :: updateMaxTo( std::string theName, int posibleMax )
104         {
105                 std::map<std::string, KeyThing >::iterator iterP = keyThings.find(theName);
106                 if ( iterP != keyThings.end() )
107                 {
108                         int max = (iterP->second).getValue();
109                         if ( max < posibleMax )
110                                 (iterP->second).setValue(posibleMax);
111                 }
112         }
113
114         /*
115         * Generates a (std::string) key for a given keyThing. If the key thing doesn't exist nothing is done and returns false.
116         * @param theName Is the name of the keyThing to search. 
117         * @param theInputString Is string to load the generated key formed like <prefixOfTheKeyThing> <maxOfTheKeyThing>
118         * @return theKey Returns true if the key was generated successfully. (If theName is an existent keyThing)
119         */
120         bool PrefixMaxKeyGenerator :: generateKeyOf( std::string theName, std::string &theInputString )
121         {
122                 theInputString = "";
123                 std::map<std::string, KeyThing >::iterator iterP = keyThings.find(theName);
124                 if ( iterP != keyThings.end() )
125                 {                       
126                         KeyThing kthing = (iterP->second);
127                         int max = kthing.getValue();                    
128                         std::ostringstream genKey;
129                         genKey<<kthing.getPrefix() << " " << max;
130                         theInputString = genKey.str();
131                         return true;
132                 }
133                 return false;
134         }
135
136         /*
137         * 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. 
138         * @param theName Is the name of the keyThing to search. 
139         * @param posibleMax Is the number that corresponds to a posible max value of the keyThing to update. 
140         * @param theInputString Is string to load the generated key formed like <prefixOfTheKeyThing> <maxOfTheKeyThing>
141         * @return Returns true if the key was generated successfully. (If theName is an existent keyThing)
142         */
143         bool PrefixMaxKeyGenerator :: generateKeyOf( std::string theName,  int posibleMax, std::string &theInputString  )
144         {
145                 theInputString = "";
146                 std::map<std::string, KeyThing >::iterator iterP = keyThings.find(theName);
147                 if ( iterP != keyThings.end() )
148                 {       
149                         KeyThing kthing = (iterP->second);
150                         int max = kthing.getValue();
151
152                         if ( max < posibleMax )
153                         {
154                                 kthing.setValue(posibleMax);
155                         }
156
157
158                         std::ostringstream genKey;
159                         genKey<<kthing.getPrefix() << " " << kthing.getValue();
160                         theInputString = genKey.str();
161                         return true;
162                 }
163                 return false;
164         }
165
166         /*
167         * Gets the prefix of a specific keyThing identified with the name in the parameter, if the key thing doesn't exists return false.
168         * @param theName Is the name of the keyThing to search the prefix. 
169         * @param theInputString Is string to load the prefix of the searched keyThing.
170         * @return isStringOutputReal Returns if the loaded string in theInputString is real (i.e if the -theName- keyThing exists)
171         */
172         bool PrefixMaxKeyGenerator :: getPrefixOf( std::string theName, std::string &theInputString )           
173         {
174                 theInputString = "";
175                 std::map<std::string, KeyThing >::iterator iterP = keyThings.find(theName);
176                 if ( iterP != keyThings.end() )
177                 {       
178                         KeyThing kthing = (iterP->second);
179                         theInputString = kthing.getPrefix();
180                         return true;
181                 }
182                 return false;
183         }
184
185         /*
186         * Gets the max value of a specific keyThing identified with the name in the parameter. If the key thing doesn't exists returns -1.
187         * @param theName Is the name of the keyThing to search the maximum.     
188         * @return theMax Returns the maxumum value for key generation at the keyThing. Returns -1 if not finded keyThing.
189         */
190         int PrefixMaxKeyGenerator :: getCurrentMaxOf( std::string theName )
191         {
192                 std::map<std::string, KeyThing >::iterator iterP = keyThings.find(theName);
193                 if ( iterP != keyThings.end() )
194                 {       
195                         KeyThing kthing = (iterP->second);
196                         return kthing.getValue();                       
197                 }
198                 return -1;
199         }
200
201         /*
202         * Gets the total of keyThings managed
203         * @return Retuns the total of keyThing managed
204         */
205         int PrefixMaxKeyGenerator :: getTotalThingsNumber()
206         {
207                 return keyThings.size();
208         }
209
210         /*
211         * Clears the generator deleating the existring keyThings
212         */
213         void PrefixMaxKeyGenerator :: clear()
214         {               
215                 std::map<std::string, KeyThing >::iterator iter = keyThings.begin();
216                 while( iter != keyThings.end() )
217                 {
218                         keyThings.erase(iter);                  
219                 }
220                 keyThings.clear();              
221                 numberOfKeyThings = 0;
222         }