0031668: Visualization - WebGL sample doesn't work on Emscripten 1.39
[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 <IFSelect_SignatureList.hxx>
16 #include <Interface_InterfaceModel.hxx>
17 #include <Interface_Macros.hxx>
18 #include <Interface_MSG.hxx>
19 #include <Standard_Transient.hxx>
20 #include <Standard_Type.hxx>
21 #include <TCollection_HAsciiString.hxx>
22 #include <TColStd_HSequenceOfTransient.hxx>
23
24 IMPLEMENT_STANDARD_RTTIEXT(IFSelect_SignatureList,Standard_Transient)
25
26 IFSelect_SignatureList::IFSelect_SignatureList
27   (const Standard_Boolean withlist)
28 {
29   thesignonly = Standard_False;
30   thelistat = withlist;
31   thenbnuls = 0;
32   SetName("...");
33 }
34
35     void IFSelect_SignatureList::SetList
36   (const Standard_Boolean withlist)
37       {  thelistat = withlist;  }
38
39     Standard_Boolean&  IFSelect_SignatureList::ModeSignOnly ()
40       {  return thesignonly;  }
41
42     void IFSelect_SignatureList::Clear ()
43 {
44   thelastval.Clear();
45   thenbnuls = 0;
46   thedicount.Clear();
47   thediclist.Clear();
48 }
49
50     void IFSelect_SignatureList::Add
51   (const Handle(Standard_Transient)& ent, const Standard_CString sign)
52 {
53   if (thesignonly) {
54     thelastval.Clear();
55     thelastval.AssignCat (sign);
56     return;
57   }
58
59   if (sign[0] == '\0') {  thenbnuls ++;  return;  }
60
61   if (thedicount.Contains(sign))
62     thedicount.ChangeFromKey(sign)++;
63   else
64     thedicount.Add(sign, 1);
65
66   if (thelistat) {
67     Handle(TColStd_HSequenceOfTransient) alist;
68     if (thediclist.Contains(sign))
69       alist = Handle(TColStd_HSequenceOfTransient)::DownCast(thediclist.FindFromKey(sign));
70     else {
71       alist = new TColStd_HSequenceOfTransient();
72       thediclist.Add(sign, alist);
73     }
74     alist->Append(ent);
75   }
76 }
77
78     Standard_CString  IFSelect_SignatureList::LastValue () const
79       {  return thelastval.ToCString();  }
80
81     void IFSelect_SignatureList::Init
82   (const Standard_CString name,
83    const NCollection_IndexedDataMap<TCollection_AsciiString, Standard_Integer>&   theCount,
84    const NCollection_IndexedDataMap<TCollection_AsciiString, Handle(Standard_Transient)>& list,
85    const Standard_Integer nbnuls)
86 {
87   thelastval.Clear();
88   thename    = new TCollection_HAsciiString (name);
89   thedicount = theCount;
90   thediclist = list;
91   thenbnuls  = nbnuls;
92   if (thediclist.IsEmpty()) thelistat = Standard_False;
93 }
94
95
96     Handle(TColStd_HSequenceOfHAsciiString)  IFSelect_SignatureList::List
97   (const Standard_CString root) const
98 {
99   Handle(TColStd_HSequenceOfHAsciiString) list =
100     new TColStd_HSequenceOfHAsciiString();
101   NCollection_IndexedDataMap<TCollection_AsciiString, Standard_Integer>::Iterator iter(thedicount);
102   for (; iter.More(); iter.Next()) {
103     if (!iter.Key().StartsWith(root)) continue;
104
105     Handle(TCollection_HAsciiString) sign =
106       new TCollection_HAsciiString (iter.Key());
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 = 0;
123   thedicount.FindFromKey(sign, nb);
124   return nb;
125 }
126
127     Handle(TColStd_HSequenceOfTransient)  IFSelect_SignatureList::Entities
128   (const Standard_CString sign) const
129 {
130   Handle(TColStd_HSequenceOfTransient) list;
131   Handle(Standard_Transient) aTList;
132   if (!thelistat) return list;
133   if (thediclist.FindFromKey(sign, aTList))
134     list = Handle(TColStd_HSequenceOfTransient)::DownCast(aTList);
135   else
136     list = new TColStd_HSequenceOfTransient();
137   return list;
138 }
139
140
141     void IFSelect_SignatureList::SetName (const Standard_CString name)
142       {  thename    = new TCollection_HAsciiString (name);  }
143
144     Standard_CString  IFSelect_SignatureList::Name () const
145       {  return thename->ToCString();  }
146
147
148     void  IFSelect_SignatureList::PrintCount (Standard_OStream& S) const
149 {
150   Standard_Integer nbtot = 0, nbsign = 0;
151   NCollection_IndexedDataMap<TCollection_AsciiString, Standard_Integer>::Iterator iter(thedicount);
152   S << " Count  "<<thename->ToCString()<<"\n -----      -----------"<<std::endl;
153   for (; iter.More(); iter.Next()) {
154     Standard_Integer val = iter.Value();
155     S << Interface_MSG::Blanks(val,6) << val <<"        "<<iter.Key()<<std::endl;
156     nbtot += val;
157     nbsign ++;
158   }
159   if (thenbnuls > 0) S << thename->ToCString()<< " Nul : " << thenbnuls <<std::endl;
160   S << "    Nb Total:"<<nbtot<<"  for "<<nbsign<<" items"<<std::endl;
161 }
162
163     void  IFSelect_SignatureList::PrintList
164   (Standard_OStream& S, const Handle(Interface_InterfaceModel)& model,
165    const IFSelect_PrintCount mod) const
166 {
167   if (mod == IFSelect_ItemsByEntity) return;
168   if (mod == IFSelect_CountByItem)   {  PrintCount (S);  return;  }
169   if (mod == IFSelect_CountSummary)  {  PrintSum   (S);  return;  }
170   if (!HasEntities()) {
171     S <<" SignatureList "<<Name()<<" : PrintList, list not available"<<std::endl;
172     PrintCount(S);
173     return;
174   }
175   Standard_Integer nbtot = 0, nbsign = 0;
176   NCollection_IndexedDataMap<TCollection_AsciiString, Handle(Standard_Transient)>::Iterator iter(thediclist);
177   for (; iter.More(); iter.Next()) {
178     DeclareAndCast(TColStd_HSequenceOfTransient,list,iter.Value());
179     S<<Name()<<" : "<<iter.Key()<<std::endl;
180     if (list.IsNull())  {  S<<"  - (empty list)"<<std::endl; continue;  }
181     Standard_Integer nb = list->Length();
182     S<<"  - Nb: "<<nb<<" : ";
183     Standard_Integer nc = nb;  if (nb > 5 && mod == IFSelect_ShortByItem) nc = 5;
184     for (Standard_Integer i = 1; i <= nc; i ++) {
185       if (list->Value(i).IsNull()) {
186         S << "  0";  
187         if (mod == IFSelect_EntitiesByItem) S<<":(Global)";
188         continue;
189       }
190       Standard_Integer num = model->Number(list->Value(i));
191       if (num == IFSelect_ShortByItem)  {  S<<"  ??";  continue;  }
192       S<<"  "<<num;
193       if (mod == IFSelect_EntitiesByItem)
194         {  S<<":";  model->PrintLabel(list->Value(i), S);  }
195     }
196     if (nc < nb) S<<"  .. etc";
197     S<<std::endl;
198     nbtot += nb;
199     nbsign ++;
200   }
201   S <<" Nb Total:"<<nbtot<<"  for "<<nbsign<<" items"<<std::endl;
202 }
203
204
205     void  IFSelect_SignatureList::PrintSum (Standard_OStream& S) const
206 {
207   NCollection_IndexedDataMap<TCollection_AsciiString, Standard_Integer>::Iterator iter(thedicount);
208   S << " Summary "<<thename->ToCString()<<"\n -----     -----------"<<std::endl;
209   Standard_Integer nbtot = 0, nbsign = 0, maxent = 0, nbval = 0, nbve = 0, minval = 0, maxval = 0, totval = 0;
210   for (; iter.More(); iter.Next()) {
211     Standard_Integer nbent = iter.Value();
212     nbtot += nbent;
213     nbsign ++;
214     if (nbent > maxent) maxent = nbent;
215     TCollection_AsciiString name = iter.Key();
216 //    if (!name.IsIntegerValue()) continue;  pas bien fiable
217     Standard_Integer ic, nc = name.Length();
218     Standard_Boolean iaint = Standard_True;
219     for (ic = 1; ic <= nc; ic ++) {
220       char unc = name.Value(ic);
221       if (ic == 1 && (unc == ' ' || unc == '+' || unc == '-')) continue;
222       if (unc >= '0' && unc <= '9') continue;
223       iaint = Standard_False;    break;
224     }
225     if (!iaint) continue;
226     Standard_Integer val = name.IntegerValue();
227     if (nbval == 0) { minval = maxval = val; }
228     if (minval > val) minval = val;
229     if (maxval < val) maxval = val;
230     nbval ++;
231     nbve += nbent;
232     totval += (val*nbent);
233   }
234   S << "    Nb Total:"<<nbtot<<"  for "<<nbsign<<" items"<<std::endl;
235   S << "    Highest count of entities : "<<maxent<<" on one item"<<std::endl;
236   if (nbval > 0) {
237     S<<"    Summary on Integer Values"<<std::endl;
238     S<<"    Nb Integer Items : "<<nbval<<std::endl;
239     S<<"    For Nb Entities  : "<<nbve<<std::endl;
240     S<<"    Cumulated Values : "<<totval<<std::endl;
241     S <<"    Maximum Value    : "<<maxval<<std::endl;
242     Standard_Integer avg1, avg2;
243     avg1 = totval/nbve;
244     avg2 = ((totval - (avg1*nbve)) * 10) / nbve;
245     S <<"    Average Value    : "<<avg1<<" "<<avg2<<"/10"<<std::endl;
246     S <<"    Minimum Value    : "<<minval<<std::endl;
247   }
248 }