0024002: Overall code and build procedure refactoring -- automatic
[occt.git] / src / IFSelect / IFSelect_SignatureList.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14
15 #include <Dico_DictionaryOfInteger.hxx>
16 #include <Dico_DictionaryOfTransient.hxx>
17 #include <Dico_IteratorOfDictionaryOfInteger.hxx>
18 #include <Dico_IteratorOfDictionaryOfTransient.hxx>
19 #include <IFSelect_SignatureList.hxx>
20 #include <Interface_InterfaceModel.hxx>
21 #include <Interface_Macros.hxx>
22 #include <Interface_MSG.hxx>
23 #include <Message.hxx>
24 #include <Message_Messenger.hxx>
25 #include <Standard_Transient.hxx>
26 #include <Standard_Type.hxx>
27 #include <TCollection_HAsciiString.hxx>
28 #include <TColStd_HSequenceOfTransient.hxx>
29
30 IFSelect_SignatureList::IFSelect_SignatureList
31   (const Standard_Boolean withlist)
32 {
33   thesignonly = Standard_False;
34   thelistat = withlist;
35   thenbnuls = 0;
36   thedicount = new Dico_DictionaryOfInteger;
37   thediclist = new Dico_DictionaryOfTransient;
38   SetName("...");
39 }
40
41     void IFSelect_SignatureList::SetList
42   (const Standard_Boolean withlist)
43       {  thelistat = withlist;  }
44
45     Standard_Boolean&  IFSelect_SignatureList::ModeSignOnly ()
46       {  return thesignonly;  }
47
48     void IFSelect_SignatureList::Clear ()
49 {
50   thelastval.Clear();
51   thenbnuls = 0;
52   thedicount = new Dico_DictionaryOfInteger;
53   thediclist = new Dico_DictionaryOfTransient;
54 }
55
56     void IFSelect_SignatureList::Add
57   (const Handle(Standard_Transient)& ent, const Standard_CString sign)
58 {
59   if (thesignonly) {
60     thelastval.Clear();
61     thelastval.AssignCat (sign);
62     return;
63   }
64
65   if (sign[0] == '\0') {  thenbnuls ++;  return;  }
66
67   Standard_Boolean deja;
68   Standard_Integer& nb = thedicount->NewItem(sign,deja);
69   if (!deja) nb = 0;
70   nb ++;
71
72   if (thelistat) {
73     Handle(Standard_Transient)& anitem = thediclist->NewItem(sign,deja);
74     DeclareAndCast(TColStd_HSequenceOfTransient,alist,anitem);
75     if (!deja) { alist = new TColStd_HSequenceOfTransient(); anitem = alist;  }
76     alist->Append(ent);
77   }
78 }
79
80     Standard_CString  IFSelect_SignatureList::LastValue () const
81       {  return thelastval.ToCString();  }
82
83     void IFSelect_SignatureList::Init
84   (const Standard_CString name,
85    const Handle(Dico_DictionaryOfInteger)&   theCount,
86    const Handle(Dico_DictionaryOfTransient)& list,
87    const Standard_Integer nbnuls)
88 {
89   thelastval.Clear();
90   thename    = new TCollection_HAsciiString (name);
91   thedicount = theCount;
92   thediclist = list;
93   thenbnuls  = nbnuls;
94   if (thediclist.IsNull()) thelistat = Standard_False;
95 }
96
97
98     Handle(TColStd_HSequenceOfHAsciiString)  IFSelect_SignatureList::List
99   (const Standard_CString root) const
100 {
101   Handle(TColStd_HSequenceOfHAsciiString) list =
102     new TColStd_HSequenceOfHAsciiString();
103   Dico_IteratorOfDictionaryOfInteger iter(thedicount,root);
104   for (; iter.More(); iter.Next()) {
105     Handle(TCollection_HAsciiString) sign =
106       new TCollection_HAsciiString (iter.Name());
107     list->Append(sign);
108   }
109   return list;
110 }
111
112
113     Standard_Boolean  IFSelect_SignatureList::HasEntities () const
114       {  return thelistat;  }
115
116     Standard_Integer  IFSelect_SignatureList::NbNulls () const
117       {  return thenbnuls;  }
118
119     Standard_Integer  IFSelect_SignatureList::NbTimes
120   (const Standard_CString sign) const
121 {
122   Standard_Integer nb;
123   if (thedicount->GetItem(sign,nb)) return nb;
124   else return 0;
125 }
126
127     Handle(TColStd_HSequenceOfTransient)  IFSelect_SignatureList::Entities
128   (const Standard_CString sign) const
129 {
130   Handle(TColStd_HSequenceOfTransient) list;
131   if (!thelistat) return list;
132   if (thediclist->GetItem(sign,list)) return list;
133   list = new TColStd_HSequenceOfTransient();
134   return list;
135 }
136
137
138     void IFSelect_SignatureList::SetName (const Standard_CString name)
139       {  thename    = new TCollection_HAsciiString (name);  }
140
141     Standard_CString  IFSelect_SignatureList::Name () const
142       {  return thename->ToCString();  }
143
144
145     void  IFSelect_SignatureList::PrintCount (const Handle(Message_Messenger)& S) const
146 {
147   Standard_Integer nbtot = 0, nbsign = 0;
148   Dico_IteratorOfDictionaryOfInteger iter(thedicount,"");
149   S << " Count  "<<thename->ToCString()<<"\n -----      -----------"<<endl;
150   for (; iter.More(); iter.Next()) {
151     Standard_Integer val = iter.Value();
152     S << Interface_MSG::Blanks(val,6) << val <<"        "<<iter.Name()<<endl;
153     nbtot += val;
154     nbsign ++;
155   }
156   if (thenbnuls > 0) S << thename->ToCString()<< " Nul : " << thenbnuls<<endl;
157   S<<"    Nb Total:"<<nbtot<<"  for "<<nbsign<<" items"<<endl;
158 }
159
160     void  IFSelect_SignatureList::PrintList
161   (const Handle(Message_Messenger)& S, const Handle(Interface_InterfaceModel)& model,
162    const IFSelect_PrintCount mod) const
163 {
164   if (mod == IFSelect_ItemsByEntity) return;
165   if (mod == IFSelect_CountByItem)   {  PrintCount (S);  return;  }
166   if (mod == IFSelect_CountSummary)  {  PrintSum   (S);  return;  }
167   if (!HasEntities()) {
168     S <<" SignatureList "<<Name()<<" : PrintList, list not available"<<endl;
169     PrintCount(S);
170     return;
171   }
172   Standard_Integer nbtot = 0, nbsign = 0;
173   Dico_IteratorOfDictionaryOfTransient iter(thediclist,"");
174   for (; iter.More(); iter.Next()) {
175     DeclareAndCast(TColStd_HSequenceOfTransient,list,iter.Value());
176     S<<Name()<<" : "<<iter.Name()<<endl;
177     if (list.IsNull())  {  S<<"  - (empty list)"<<endl; continue;  }
178     Standard_Integer nb = list->Length();
179     S<<"  - Nb: "<<nb<<" : ";
180     Standard_Integer nc = nb;  if (nb > 5 && mod == IFSelect_ShortByItem) nc = 5;
181     for (Standard_Integer i = 1; i <= nc; i ++) {
182       if (list->Value(i).IsNull()) {
183         S<<"  0";  if (mod == IFSelect_EntitiesByItem) S<<":(Global)";
184         continue;
185       }
186       Standard_Integer num = model->Number(list->Value(i));
187       if (num == IFSelect_ShortByItem)  {  S<<"  ??";  continue;  }
188       S<<"  "<<num;
189       if (mod == IFSelect_EntitiesByItem)
190         {  S<<":";  model->PrintLabel(list->Value(i),S);  }
191     }
192     if (nc < nb) S<<"  .. etc";
193     S<<endl;
194     nbtot += nb;
195     nbsign ++;
196   }
197   S<<" Nb Total:"<<nbtot<<"  for "<<nbsign<<" items"<<endl;
198 }
199
200
201     void  IFSelect_SignatureList::PrintSum (const Handle(Message_Messenger)& S) const
202 {
203   Dico_IteratorOfDictionaryOfInteger iter(thedicount,"");
204   S << " Summary "<<thename->ToCString()<<"\n -----     -----------"<<endl;
205   Standard_Integer nbtot = 0, nbsign = 0, maxent = 0, nbval = 0, nbve = 0, minval = 0, maxval = 0, totval = 0;
206   for (; iter.More(); iter.Next()) {
207     Standard_Integer nbent = iter.Value();
208     nbtot += nbent;
209     nbsign ++;
210     if (nbent > maxent) maxent = nbent;
211     TCollection_AsciiString name = iter.Name();
212 //    if (!name.IsIntegerValue()) continue;  pas bien fiable
213     Standard_Integer ic, nc = name.Length();
214     Standard_Boolean iaint = Standard_True;
215     for (ic = 1; ic <= nc; ic ++) {
216       char unc = name.Value(ic);
217       if (ic == 1 && (unc == ' ' || unc == '+' || unc == '-')) continue;
218       if (unc >= '0' && unc <= '9') continue;
219       iaint = Standard_False;    break;
220     }
221     if (!iaint) continue;
222     Standard_Integer val = name.IntegerValue();
223     if (nbval == 0) { minval = maxval = val; }
224     if (minval > val) minval = val;
225     if (maxval < val) maxval = val;
226     nbval ++;
227     nbve += nbent;
228     totval += (val*nbent);
229   }
230   S << "    Nb Total:"<<nbtot<<"  for "<<nbsign<<" items"<<endl;
231   S << "    Highest count of entities : "<<maxent<<" on one item"<<endl;
232   if (nbval > 0) {
233     S<<"    Summary on Integer Values"<<endl;
234     S<<"    Nb Integer Items : "<<nbval<<endl;
235     S<<"    For Nb Entities  : "<<nbve<<endl;
236     S<<"    Cumulated Values : "<<totval<<endl;
237     S<<"    Maximum Value    : "<<maxval<<endl;
238     Standard_Integer avg1, avg2;
239     avg1 = totval/nbve;
240     avg2 = ((totval - (avg1*nbve)) * 10) / nbve;
241     S<<"    Average Value    : "<<avg1<<" "<<avg2<<"/10"<<endl;
242     S<<"    Minimum Value    : "<<minval<<endl;
243   }
244 }