summaryrefslogtreecommitdiff
path: root/inc/Dico_Dictionary.gxx
blob: 3a0afa8215eb237c3d20a7dc89b3b5d683d7e23b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//#include <Dico_Dictionary.ixx>  pas en gxx
#include <Standard_NoSuchObject.hxx>
#include <string.h>



// thecars[0] : caractere de consigne, caracterisant la cellule
// thecars[1] : indicateur de presence : 0 rien, ==thecar[0] item present
// thecars[2] : caractere du Sub  ou 0
// thecars[3] : caractere du Next ou 0

//  REMARQUE : Fonctions d acces dupliquees : TCollection_AsciiString / CString
//  C est a chaque fois le MEME CODE, mais Length et Value ne sont pas obtenues
//  de la  meme maniere ...
//  SearchCell pas duplique : la version String appelle la version CString
//  Les autres fonctions doivent d abord evaluer la longueur de <name>
//  Sur String, methode Length, mais sur CString, il faut evaluer par  strlen
//  String serait donc meilleur (plus systematique) MAIS suppose, sur appel
//  CString, de constituer une TCollection_AsciiString ce qui peut couter ...
//  CONCLUSION : Fonctions d acces par Nom dupliquees, pour optimisation


    Dico_Dictionary::Dico_Dictionary ()
{
  thecars[0] = thecars[1] = thecars[2] = thecars[3] = '\0';
  thesub.Nullify(); thenext.Nullify();
}

    void Dico_Dictionary::SetChar (const char car)  {  thecars[0] = car;  }


//  ....                        HasItem                                ....

    Standard_Boolean Dico_Dictionary::HasItem
  (const Standard_CString name, const Standard_Boolean exact) const
{
  Handle(Dico_Dictionary) acell;
  Standard_Integer reslev,stat;
  SearchCell (name,strlen(name),name[0],1, acell,reslev,stat);
  if (stat != 0 || reslev != 0) return Standard_False;
  if (acell->HasIt()) return Standard_True;
  if (!exact)    {  if (!acell->Complete(acell)) return Standard_False;  }
  return (acell->HasIt());
}

    Standard_Boolean Dico_Dictionary::HasItem
  (const TCollection_AsciiString& name, const Standard_Boolean exact) const
{
  Handle(Dico_Dictionary) acell;
  Standard_Integer reslev,stat;
  SearchCell (name.ToCString(),name.Length(),name.Value(1),1, acell,reslev,stat);
  if (stat != 0 || reslev != 0) return Standard_False;
  if (acell->HasIt()) return Standard_True;
  if (!exact)    {  if (!acell->Complete(acell)) return Standard_False;  }
  return (acell->HasIt());
}

//  ....                        Item                                ....

    const TheItem& Dico_Dictionary::Item
  (const Standard_CString name, const Standard_Boolean exact) const
{
  Handle(Dico_Dictionary) acell;
  Standard_Integer reslev,stat;
  SearchCell (name,strlen(name),name[0],1, acell,reslev,stat);
  if (stat != 0 || reslev != 0)
    Standard_NoSuchObject::Raise("Dictionary : Item");
  if (acell->HasIt()) return acell->It();
  if (!exact)    {  if (!acell->Complete(acell)) return acell->It();  }
  if (!acell->HasIt()) Standard_NoSuchObject::Raise("Dictionary : Item");
  return (acell->It());
}

    const TheItem& Dico_Dictionary::Item
  (const TCollection_AsciiString& name, const Standard_Boolean exact) const
{
  Handle(Dico_Dictionary) acell;
  Standard_Integer reslev,stat;
  SearchCell (name.ToCString(),name.Length(),name.Value(1),1, acell,reslev,stat);
  if (stat != 0 || reslev != 0)
    Standard_NoSuchObject::Raise("Dictionary : Item");
  if (acell->HasIt()) return acell->It();
  if (!exact)    {  if (!acell->Complete(acell)) return acell->It();  }
  if (!acell->HasIt()) Standard_NoSuchObject::Raise("Dictionary : Item");
  return (acell->It());
}

//  ....                        GetItem                                ....

    Standard_Boolean Dico_Dictionary::GetItem
  (const Standard_CString name,
   TheItem& anitem, const Standard_Boolean exact) const
{
  Handle(Dico_Dictionary) acell;
  Standard_Integer reslev,stat;
  SearchCell (name,strlen(name),name[0],1, acell,reslev,stat);
  if (stat != 0 || reslev != 0) return Standard_False;
  if (acell->HasIt())    {  anitem = acell->It();  return Standard_True;  }
  if (!exact)      {  if (!acell->Complete(acell)) return Standard_False;  }
  anitem = acell->It();
  return (acell->HasIt());
}

    Standard_Boolean Dico_Dictionary::GetItem
  (const TCollection_AsciiString& name,
   TheItem& anitem, const Standard_Boolean exact) const
{
  Handle(Dico_Dictionary) acell;
  Standard_Integer reslev,stat;
  SearchCell (name.ToCString(),name.Length(),name.Value(1),1, acell,reslev,stat);
  if (stat != 0 || reslev != 0) return Standard_False;
  if (acell->HasIt())    {  anitem = acell->It();  return Standard_True;  }
  if (!exact)      {  if (!acell->Complete(acell)) return Standard_False;  }
  anitem = acell->It();
  return (acell->HasIt());
}

//  ....                        SetItem                                ....

    void Dico_Dictionary::SetItem
  (const Standard_CString name,
   const TheItem& anitem, const Standard_Boolean exact)
{
  Handle(Dico_Dictionary) acell;
  Standard_Integer reslev,stat;
  Standard_Integer namlen = strlen(name);
  SearchCell (name,namlen,name[0],1, acell,reslev,stat);
  if (!exact && !acell->HasIt())
    {  if (acell->Complete(acell)) { acell->SetIt(anitem); return; }  }
  if (stat < 0) {  cout<<"Dictionary walk back not performed"<<endl; return;  }
  NewCell (name,namlen, acell,reslev,stat);
  acell->SetIt(anitem);
}


    void Dico_Dictionary::SetItem
  (const TCollection_AsciiString& name,
   const TheItem& anitem, const Standard_Boolean exact)
{
  Handle(Dico_Dictionary) acell;
  Standard_Integer reslev,stat;
  Standard_CString namval = name.ToCString();
  Standard_Integer namlen = name.Length();
  SearchCell (namval,namlen,name.Value(1),1, acell,reslev,stat);
  if (!exact && !acell->HasIt())
    {  if (acell->Complete(acell)) { acell->SetIt(anitem); return; }  }
  if (stat < 0) {  cout<<"Dictionary walk back not performed"<<endl; return;  }
  NewCell (namval,namlen, acell,reslev,stat);
  acell->SetIt(anitem);
}


//  ....                        NewItem                                ....

    TheItem& Dico_Dictionary::NewItem
  (const Standard_CString name,
   Standard_Boolean& isvalued, const Standard_Boolean exact)
{
  Handle(Dico_Dictionary) acell;
  Standard_Integer reslev,stat;
  Standard_Integer namlen = strlen(name);
  SearchCell (name,namlen,name[0],1, acell,reslev,stat);
  if (stat == 0 && reslev == 0) {
    isvalued = acell->HasIt();    // ancien statut
    acell->DeclIt();              // nouveau statut = value d office
    return acell->ItAdr();
  }
  if (!exact) {
    if (acell->Complete(acell))
      {  isvalued = acell->HasIt(); acell->DeclIt();  return acell->ItAdr();  }
  }
  if (stat < 0) {   Standard_NoSuchObject::Raise("Dictionary : NewItem");  }
  NewCell (name,namlen, acell,reslev,stat);
  isvalued = acell->HasIt();    // ancien statut
  acell->DeclIt();              // nouveau statut = value d office
  return acell->ItAdr();
}


    TheItem& Dico_Dictionary::NewItem
  (const TCollection_AsciiString& name,
   Standard_Boolean& isvalued, const Standard_Boolean exact)
{
  Handle(Dico_Dictionary) acell;
  Standard_Integer reslev,stat;
  Standard_Integer namlen = name.Length();
  Standard_CString namval = name.ToCString();
  SearchCell (namval,namlen,name.Value(1),1, acell,reslev,stat);
  if (stat == 0 && reslev == 0) {
    isvalued = acell->HasIt();    // ancien statut
    acell->DeclIt();              // nouveau statut = value d office
    return acell->ItAdr();
  }
  if (!exact) {
    if (acell->Complete(acell))
      {  isvalued = acell->HasIt(); acell->DeclIt();  return acell->ItAdr();  }
  }
  if (stat < 0) {   Standard_NoSuchObject::Raise("Dictionary : NewItem");  }
  NewCell (namval,namlen, acell,reslev,stat);
  isvalued = acell->HasIt();    // ancien statut
  acell->DeclIt();              // nouveau statut = value d office
  return acell->ItAdr();
}

//  ....                        RemoveItem                                ....

    Standard_Boolean Dico_Dictionary::RemoveItem
  (const Standard_CString name,
   const Standard_Boolean cln, const Standard_Boolean exact)
{
  Handle(Dico_Dictionary) acell;
  Standard_Integer reslev,stat;
  SearchCell (name,strlen(name),name[0],1, acell,reslev,stat);
//  if (stat != 0) { cout<<"Dictionary : RemoveItem not found"<<endl; return; }
  if (stat != 0) return Standard_False;
  if (!exact && !acell->HasIt())
    {  if (!acell->Complete(acell)) return Standard_False;  }
  acell->RemoveIt();
  if (cln) Clean();
  return Standard_True;
}

    Standard_Boolean Dico_Dictionary::RemoveItem
  (const TCollection_AsciiString& name,
   const Standard_Boolean cln, const Standard_Boolean exact)
{
  Handle(Dico_Dictionary) acell;
  Standard_Integer reslev,stat;
  SearchCell (name.ToCString(),name.Length(),name.Value(1),1, acell,reslev,stat);
//  if (stat != 0) { cout<<"Dictionary : RemoveItem not found"<<endl; return; }
  if (stat != 0) return Standard_False;
  if (!exact && !acell->HasIt())
    {  if (!acell->Complete(acell)) return Standard_False;  }
  acell->RemoveIt();
  if (cln) Clean();
  return Standard_True;
}

//  ....                Methodes d Interet General                        ....

    void Dico_Dictionary::Clean ()
{
  if (HasSub()) {
    thesub->Clean();
    if (!thesub->HasIt() && !thesub->HasSub()) {    // alors prendre son Next
      if (thesub->HasNext()) {
	thesub = thesub->Next();
	thecars[2] = thesub->CellChar();
      } else {
	thesub.Nullify();
	thecars[2] = '\0';
      }
    }
  }
  if (HasNext()) {
    thenext->Clean();
    if (!thenext->HasIt() && !thenext->HasSub()) {    // alors prendre son Next
      if (thenext->HasNext()) {
	thenext = thenext->Next();
	thecars[3] = thenext->CellChar();
      } else {
	thenext.Nullify();
	thecars[3] = '\0';
      }
    }
  }
}

    Standard_Boolean Dico_Dictionary::IsEmpty () const
{
  if (thecars[1] != '\0') return Standard_False;
  if (!thesub.IsNull())
    {  if (!thesub->IsEmpty()) return Standard_False;  }
  if (!thenext.IsNull())
    {  if (!thenext->IsEmpty()) return Standard_False;  }
  return Standard_True;
}

    void Dico_Dictionary::Clear ()
{
  thecars[0] = thecars[1] = thecars[2] = thecars[3] = '\0';
  thesub.Nullify();  thenext.Nullify();
}


    Handle(Dico_Dictionary) Dico_Dictionary::Copy () const
{
  Handle(Dico_Dictionary) newdic = new Dico_Dictionary;
//    Handle(Dico_Dictionary)::DownCast(ShallowCopy());
  newdic->GetCopied(this);    // qui doit gerer completement thecars et item
  return newdic;
}

//  ########################################################################

// ....                        Routines internes                        ....

    Standard_Boolean Dico_Dictionary::HasSub () const
      {  return (thecars[2] != '\0');  }

    Handle(Dico_Dictionary) Dico_Dictionary::Sub () const   { return thesub;  }

    Standard_Boolean Dico_Dictionary::HasNext () const
      {  return (thecars[3] != '\0');  }

    Handle(Dico_Dictionary) Dico_Dictionary::Next () const  { return thenext; }

    void Dico_Dictionary::SetSub (const Handle(Dico_Dictionary)& acell)
{
  thesub = acell;      thecars[2] = '\0';
  if (!acell.IsNull()) thecars[2] = acell->CellChar();
}

    void Dico_Dictionary::SetNext (const Handle(Dico_Dictionary)& acell)
{
  thenext = acell;     thecars[3] = '\0';
  if (!acell.IsNull()) thecars[3] = acell->CellChar();
}

//  ....                            SearchCell                            ....

    void Dico_Dictionary::SearchCell
      (const Standard_CString name,  const Standard_Integer lmax,
       const Standard_Character car, const Standard_Integer level,
       Handle(Dico_Dictionary)& acell,
       Standard_Integer& reslev,     Standard_Integer& stat) const
{
  reslev = lmax - level;
  if      (car > thecars[0]) {
    if (thecars[3] == '\0')     {  acell = this;  stat   =  1;    return;  }
    else {                                      // ici, HasNext
      if (thecars[3] > car)     {  acell = this;  stat   =  1;    return;  }
      Standard_Integer lev2,stat2;
      thenext->SearchCell (name,lmax,car,level,acell,lev2,stat2);
      if (stat2 < 0)            {  acell = this;  stat   =  1;    return;  }
      else                      {  stat = stat2;  reslev = lev2;  return;  }
    }
  }
  else if (car == thecars[0]) {
    if (reslev == 0 || thecars[2] == '\0')      // c-a-d !HasSub
                                {  acell = this;  stat   =  0;    return;  }
    else {
      Standard_Character carsub = name[level];  // caractere no (level+1);
      if (thecars[2] > carsub)  {  acell = this;  stat   =  0;    return;  }
      Standard_Integer lev2,stat2;
      thesub->SearchCell (name,lmax,carsub,level+1,acell,lev2,stat2);
      if (stat2 < 0)            {  acell = this;  stat   =  0;    return;  }
      else                      {  stat = stat2;  reslev = lev2;  return;  }
    }
  }
  else if (car < thecars[0])    {  acell = this;  stat   = -1;    return;  }
}


//  ....                            NewCell                            ....

    void Dico_Dictionary::NewCell
  (const Standard_CString name, const Standard_Integer namlen,
   Handle(Dico_Dictionary)& acell,
   const Standard_Integer reslev, const Standard_Integer stat)
{
  Standard_Integer level = namlen - reslev;
  if (stat > 0) {    // Creer une cellule next
    Handle(Dico_Dictionary) newcell = new Dico_Dictionary;
    newcell->SetChar( name[level-1] );      // caractere no level
    if (acell->HasNext()) newcell->SetNext(acell->Next());
    acell->SetNext(newcell);
    acell = newcell;
  }
//  A present, voir les sous-niveaux
  for (Standard_Integer i = level+1; i <= namlen; i ++) {
    Handle(Dico_Dictionary) newcell = new Dico_Dictionary;
    newcell->SetChar(name[i-1]);
    if (acell->HasSub()) newcell->SetNext(acell->Sub());
    acell->SetSub(newcell);
    acell = newcell;
  }
}


    Standard_Boolean Dico_Dictionary::Complete
  (Handle(Dico_Dictionary)& newcell) const
{
  if (!HasSub())            {  newcell = this; return HasIt();  }
  if (HasIt())              {  newcell = this; return Standard_False;  }
  if (thesub->HasNext())    {  newcell = this; return Standard_False;  }
  return thesub->Complete (newcell);
}


//  ##    ##    ##    ##    ##    ##    ##    ##    ##    ##    ##    ##    ##
//  ....                    Actions Internes Unitaires                    ....

    Standard_Boolean Dico_Dictionary::HasIt () const
      {  return (thecars[1] != '\0');  }

    const TheItem& Dico_Dictionary::It () const           {  return theitem;  }

    TheItem& Dico_Dictionary::ItAdr ()                    {  return theitem;  }

    void Dico_Dictionary::SetIt (const TheItem& anitem)
      {  theitem = anitem;  thecars[1] = thecars[0];  }

    void Dico_Dictionary::DeclIt ()              {  thecars[1] = thecars[0];  }

    void Dico_Dictionary::RemoveIt ()                  {  thecars[1] = '\0';  }

    Standard_Character Dico_Dictionary::CellChar () const
      {  return thecars[0];  }

    void Dico_Dictionary::GetCopied (const Handle(Dico_Dictionary)& fromcell)
{
  thecars[0] = fromcell->CellChar();
//  On reprend l item s ilyena un
  if (fromcell->HasIt()) {
    thecars[1] = thecars[0];
    theitem = fromcell->It();
  }
  else thecars[1] = '\0';

//  On saute les noeuds vides
  if (fromcell->HasSub()) {
    thesub   = fromcell->Sub()->Copy();
    while (!thesub->HasIt() && !thesub->HasSub()) {
      thesub = thesub->Next();
      if (thesub.IsNull()) { thecars[2] = '\0'; break; }
      else                   thecars[2] = thesub->CellChar();
    }
  }

  if (fromcell->HasNext()) {
    thenext  = fromcell->Next()->Copy();
    while (!thenext->HasIt() && !thenext->HasSub()) {
      thenext = thenext->Next();
      if (thenext.IsNull()) { thecars[3] = '\0'; break; }
      else                    thecars[3] = thenext->CellChar();
    }
  }
}