cca7eca31d05f1f1fb6cfa18af3c65d19a9ecf61
[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 : HasFree
53 // purpose  :
54 // =======================================================================
55 Standard_Boolean Aspect_GenId::HasFree() const
56 {
57   return myFreeCount > 0
58       || myFreeIds.Extent() > 0;
59 }
60
61 // =======================================================================
62 // function : Available
63 // purpose  :
64 // =======================================================================
65 Standard_Integer Aspect_GenId::Available() const
66 {
67   return myFreeCount + myFreeIds.Extent();
68 }
69
70 // =======================================================================
71 // function : Free
72 // purpose  :
73 // =======================================================================
74 void Aspect_GenId::Free()
75 {
76   myFreeCount = myLength;
77   myFreeIds.Clear();
78 }
79
80 // =======================================================================
81 // function : Free
82 // purpose  :
83 // =======================================================================
84 void Aspect_GenId::Free (const Standard_Integer theId)
85 {
86   if (theId >= myLowerBound
87    && theId <= myUpperBound)
88   {
89     if (myFreeCount + myFreeIds.Extent() + 1 == myLength)
90     {
91       myFreeCount = myLength;
92       myFreeIds.Clear();
93     }
94     else
95     {
96       myFreeIds.Prepend (theId);
97     }
98   }
99 }
100
101 // =======================================================================
102 // function : Lower
103 // purpose  :
104 // =======================================================================
105 Standard_Integer Aspect_GenId::Lower() const
106 {
107   return myLowerBound;
108 }
109
110 // =======================================================================
111 // function : Next
112 // purpose  :
113 // =======================================================================
114 Standard_Integer Aspect_GenId::Next()
115 {
116   if (!myFreeIds.IsEmpty())
117   {
118     const Standard_Integer anId = myFreeIds.First();
119     myFreeIds.RemoveFirst();
120     return anId;
121   }
122   else if (myFreeCount < 1)
123   {
124     throw Aspect_IdentDefinitionError("GenId Next Error: Available == 0");
125   }
126
127   --myFreeCount;
128   const Standard_Integer anId = myLowerBound + myLength - myFreeCount - 1;
129   return anId;
130 }
131
132 // =======================================================================
133 // function : Upper
134 // purpose  :
135 // =======================================================================
136 Standard_Integer Aspect_GenId::Upper() const
137 {
138   return myUpperBound;
139 }