0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / Aspect / Aspect_GenId.cxx
1 // Created on: 1992-05-14
2 // Created by: NW,JPB,CAL
3 // Copyright (c) 1992-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <Aspect_GenId.hxx>
19 #include <Aspect_IdentDefinitionError.hxx>
20
21 // =======================================================================
22 // function : Aspect_GenId
23 // purpose  :
24 // =======================================================================
25 Aspect_GenId::Aspect_GenId()
26 : myFreeCount  (INT_MAX / 2 + 1),
27   myLength     (INT_MAX / 2 + 1),
28   myLowerBound (0),
29   myUpperBound (INT_MAX / 2)
30 {
31   //
32 }
33
34 // =======================================================================
35 // function : Aspect_GenId
36 // purpose  :
37 // =======================================================================
38 Aspect_GenId::Aspect_GenId (const Standard_Integer theLow,
39                             const Standard_Integer theUpper)
40 : myFreeCount  (theUpper - theLow + 1),
41   myLength     (theUpper - theLow + 1),
42   myLowerBound (theLow),
43   myUpperBound (theUpper)
44 {
45   if (theLow > theUpper)
46   {
47     throw Aspect_IdentDefinitionError("GenId Create Error: wrong interval");
48   }
49 }
50
51 // =======================================================================
52 // function : Free
53 // purpose  :
54 // =======================================================================
55 void Aspect_GenId::Free()
56 {
57   myFreeCount = myLength;
58   myFreeIds.Clear();
59 }
60
61 // =======================================================================
62 // function : Free
63 // purpose  :
64 // =======================================================================
65 void Aspect_GenId::Free (const Standard_Integer theId)
66 {
67   if (theId >= myLowerBound
68    && theId <= myUpperBound)
69   {
70     if (myFreeCount + myFreeIds.Extent() + 1 == myLength)
71     {
72       myFreeCount = myLength;
73       myFreeIds.Clear();
74     }
75     else
76     {
77       myFreeIds.Prepend (theId);
78     }
79   }
80 }
81
82 // =======================================================================
83 // function : Next
84 // purpose  :
85 // =======================================================================
86 Standard_Integer Aspect_GenId::Next()
87 {
88   Standard_Integer aNewId = 0;
89   if (!Next (aNewId))
90   {
91     throw Aspect_IdentDefinitionError("Aspect_GenId::Next(), Error: Available == 0");
92   }
93   return aNewId;
94 }
95
96 // =======================================================================
97 // function : Next
98 // purpose  :
99 // =======================================================================
100 Standard_Boolean Aspect_GenId::Next (Standard_Integer& theId)
101 {
102   if (!myFreeIds.IsEmpty())
103   {
104     theId = myFreeIds.First();
105     myFreeIds.RemoveFirst();
106     return Standard_True;
107   }
108   else if (myFreeCount < 1)
109   {
110     return Standard_False;
111   }
112
113   --myFreeCount;
114   theId = myLowerBound + myLength - myFreeCount - 1;
115   return Standard_True;
116 }