0032308: Configuration - make Xlib dependency optional
[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 #include <Standard_Dump.hxx>
22
23 // =======================================================================
24 // function : Aspect_GenId
25 // purpose  :
26 // =======================================================================
27 Aspect_GenId::Aspect_GenId()
28 : myFreeCount  (INT_MAX / 2 + 1),
29   myLength     (INT_MAX / 2 + 1),
30   myLowerBound (0),
31   myUpperBound (INT_MAX / 2)
32 {
33   //
34 }
35
36 // =======================================================================
37 // function : Aspect_GenId
38 // purpose  :
39 // =======================================================================
40 Aspect_GenId::Aspect_GenId (const Standard_Integer theLow,
41                             const Standard_Integer theUpper)
42 : myFreeCount  (theUpper - theLow + 1),
43   myLength     (theUpper - theLow + 1),
44   myLowerBound (theLow),
45   myUpperBound (theUpper)
46 {
47   if (theLow > theUpper)
48   {
49     throw Aspect_IdentDefinitionError("GenId Create Error: wrong interval");
50   }
51 }
52
53 // =======================================================================
54 // function : Free
55 // purpose  :
56 // =======================================================================
57 void Aspect_GenId::Free()
58 {
59   myFreeCount = myLength;
60   myFreeIds.Clear();
61 }
62
63 // =======================================================================
64 // function : Free
65 // purpose  :
66 // =======================================================================
67 void Aspect_GenId::Free (const Standard_Integer theId)
68 {
69   if (theId >= myLowerBound
70    && theId <= myUpperBound)
71   {
72     if (myFreeCount + myFreeIds.Extent() + 1 == myLength)
73     {
74       myFreeCount = myLength;
75       myFreeIds.Clear();
76     }
77     else
78     {
79       myFreeIds.Prepend (theId);
80     }
81   }
82 }
83
84 // =======================================================================
85 // function : Next
86 // purpose  :
87 // =======================================================================
88 Standard_Integer Aspect_GenId::Next()
89 {
90   Standard_Integer aNewId = 0;
91   if (!Next (aNewId))
92   {
93     throw Aspect_IdentDefinitionError("Aspect_GenId::Next(), Error: Available == 0");
94   }
95   return aNewId;
96 }
97
98 // =======================================================================
99 // function : Next
100 // purpose  :
101 // =======================================================================
102 Standard_Boolean Aspect_GenId::Next (Standard_Integer& theId)
103 {
104   if (!myFreeIds.IsEmpty())
105   {
106     theId = myFreeIds.First();
107     myFreeIds.RemoveFirst();
108     return Standard_True;
109   }
110   else if (myFreeCount < 1)
111   {
112     return Standard_False;
113   }
114
115   --myFreeCount;
116   theId = myLowerBound + myLength - myFreeCount - 1;
117   return Standard_True;
118 }
119
120 // =======================================================================
121 // function : DumpJson
122 // purpose  :
123 // =======================================================================
124 void Aspect_GenId::DumpJson (Standard_OStream& theOStream, Standard_Integer) const
125 {
126   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myFreeCount)
127   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myLength)
128   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myLowerBound)
129   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myUpperBound)
130
131   for (TColStd_ListOfInteger::Iterator anIter (myFreeIds); anIter.More(); anIter.Next())
132   {
133     Standard_Integer aFreeId = anIter.Value();
134     OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, aFreeId)
135   }
136 }