Spinning Topp Logo BlackTopp Studios
inc
uidgenerator.cpp
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 
41 #ifndef _uidgenerator_cpp
42 #define _uidgenerator_cpp
43 
44 #include "uidgenerator.h"
45 
46 #include <limits>
47 
48 namespace Mezzanine
49 {
51  {
52  // Create the full range of valid ID's
53  this->FreeIDs.push_back( IntervalType(1,std::numeric_limits<IDType>::max()) );
54  }
55 
57  { }
58 
59  ///////////////////////////////////////////////////////////////////////////////
60  // Utility
61 
63  {
64  IDType RetID = 0;
65  if( !this->FreeIDs.empty() ) {
66  IntervalType& FirstInterval = this->FreeIDs.front();
67  RetID = FirstInterval.GetLowerBound();
68 
69  if( FirstInterval.GetIntervalSize() > 0 ) {
70  FirstInterval.LowerBound++;
71  }else{
72  this->FreeIDs.erase(this->FreeIDs.begin());
73  }
74  }
75  return RetID;
76  }
77 
79  {
80  for( ConstIDIterator IDRangeIt = this->FreeIDs.begin() ; IDRangeIt != this->FreeIDs.end() ; ++IDRangeIt )
81  {
82  if( (*IDRangeIt).IsWithinBounds(ID) ) {
83  return false;
84  }
85  }
86  return true;
87  }
88 
90  {
91  for( IDIterator IDRangeIt = this->FreeIDs.begin() ; IDRangeIt != this->FreeIDs.end() ; ++IDRangeIt )
92  {
93  // Find the range we're in
94  if( (*IDRangeIt).IsWithinBounds(ID) ) {
95  // Rule out being on the edges
96  if( (*IDRangeIt).GetLowerBound() == ID ) {
97  // Just increment the bounds
98  (*IDRangeIt).LowerBound++;
99  return true;
100  }
101  if( (*IDRangeIt).GetUpperBound() == ID ) {
102  // Just decrement the bounds
103  (*IDRangeIt).UpperBound--;
104  return true;
105  }
106 
107  // Gonna have to make a split
108  IDContainer Temp;
109 
110  // Get all the ranges before the split into our new container
111  if( IDRangeIt != this->FreeIDs.begin() ) {
112  Temp.insert(Temp.end(),this->FreeIDs.begin(),IDRangeIt);
113  }
114 
115  // Perform the split
116  Temp.push_back( IntervalType((*IDRangeIt).GetLowerBound(),ID - 1) ); // Previous
117  Temp.push_back( IntervalType(ID + 1,(*IDRangeIt).GetUpperBound()) ); // Next
118 
119  // Get all the ranges after the split into our container
120  IDIterator AfterIterator = IDRangeIt + 1;
121  if( AfterIterator != this->FreeIDs.end() ) {
122  Temp.insert(Temp.end(),AfterIterator,this->FreeIDs.end());
123  }
124 
125  // Cleanup
126  this->FreeIDs.swap(Temp);
127  return true;
128  }
129  }
130  return false;
131  }
132 
134  {
135  // Only proceed if we are working with a valid ID
136  if( ID >= 1 ) {
137  // Check the simple case
138  IDIterator IDBegin = this->FreeIDs.begin();
139  if( (*IDBegin).GetLowerBound() > ID ) {
140  if( (*IDBegin).GetLowerBound() - 1 == ID ) {
141  (*IDBegin).LowerBound--;
142  }else{
143  this->FreeIDs.insert(IDBegin,IntervalType(ID,ID));
144  }
145  return true;
146  }else{
147  // Ok, the ID being released isn't before the first free ID interval
148  for( IDIterator IDRangeIt = IDBegin ; IDRangeIt != this->FreeIDs.end() ; ++IDRangeIt )
149  {
150  // Check if we have an invalid/already issued ID
151  if( (*IDRangeIt).IsWithinBounds(ID) ) {
152  return false;
153  }
154  // Check the lowest value of the next range
155  IDIterator NextRange = IDRangeIt + 1;
156  if( NextRange == this->FreeIDs.end() ) {
157  MEZZ_EXCEPTION(ExceptionBase::INVALID_STATE_EXCEPTION,"Internal state error or all possible IDs have been generated/issued.");
158  }
159  // Check if we're in the right spot or increment and try again
160  if( (*NextRange).GetLowerBound() > ID ) {
161  Boole BorderPrev = (*IDRangeIt).GetUpperBound() + 1 == ID;
162  Boole BorderNext = (*NextRange).GetLowerBound() - 1 == ID;
163  if( BorderPrev && BorderNext ) {
164  // This is the last ID separating two intervals, gotta merge
165  IDContainer Temp;
166  Temp.insert(Temp.end(),this->FreeIDs.begin(),IDRangeIt);
167  Temp.push_back( IntervalType((*IDRangeIt).GetLowerBound(),(*NextRange).GetUpperBound()) );
168  if( NextRange + 1 != this->FreeIDs.end() ) {
169  Temp.insert(Temp.end(),NextRange + 1,this->FreeIDs.end());
170  }
171  this->FreeIDs.swap(Temp);
172  }else if( BorderPrev ) {
173  (*IDRangeIt).UpperBound++;
174  }else if( BorderNext ) {
175  (*NextRange).LowerBound--;
176  }else{
177  this->FreeIDs.insert(IDBegin,IntervalType(ID,ID));
178  }
179  return true;
180  }
181  }
182  }
183  }
184  return false;
185  }
186 
188  { std::sort(this->FreeIDs.begin(),this->FreeIDs.end()); }
189 }//Mezzanine
190 
191 #endif
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
NumType LowerBound
The lower numeric boundry of the interval.
Definition: interval.h:65
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
NumType & GetLowerBound()
Gets the lower boundry of this Interval.
Definition: interval.h:114
Interval< IDType > IntervalType
Convenience type for Intervals used by this class.
Definition: uidgenerator.h:59
IDType GenerateID()
Generates a new ID unique to the pool made by this generator.
IDContainer::const_iterator ConstIDIterator
Const Iterator type for IDs generated and stored by this class.
Definition: uidgenerator.h:65
std::vector< IntervalType > IDContainer
Basic container type for IDs generated and stored by this class.
Definition: uidgenerator.h:61
IDContainer FreeIDs
The container storing all IDs generated and in use by this generator.
Definition: uidgenerator.h:69
NumType GetIntervalSize() const
Gets the numeric distance between the lower and upper bounds of this interval.
Definition: interval.h:131
Boole ReserveID(const IDType ID)
Adds a specific ID to the pool of used IDs.
UIDGenerator()
Class constructor.
This class will generate keep track of a pool of unique 32-bit ID's that can be used for distinct obj...
Definition: interval.h:55
Thrown when the available information should have worked but failed for unknown reasons.
Definition: exception.h:113
IDContainer::iterator IDIterator
Iterator type for IDs generated and stored by this class.
Definition: uidgenerator.h:63
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
~UIDGenerator()
Class destructor.
Boole ReleaseID(const IDType ID)
Frees up an ID so that it can be reused.
void Sort()
Sorts the free IDs stored in this generator.
UInt32 IDType
Convenience type for the ID to be used. Should be some flavor of int.
Definition: uidgenerator.h:57
Boole IsIDUsed(const IDType ID) const
Checks to see if an ID is in use.