/* Copyright 2015, Michele Santullo * This file is part of DoorKeeper. * * DoorKeeper is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * DoorKeeper is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with DoorKeeper. If not, see . */ #ifndef idBC27D8C2304944D0856C2C2B012AE57B #define idBC27D8C2304944D0856C2C2B012AE57B #include "doorkeeper/primitivetypes.hpp" #include "doorkeeper/implem/saltedid.hpp" #include #include #include #include namespace dk { namespace implem { template class IDInterval { public: IDInterval ( I parFrom, I parTo ) : m_interval(parFrom, parTo) { } bool operator< ( const IDInterval& parOther ) const; I lower ( void ) const { return m_interval.lower(); } I upper ( void ) const { return m_interval.upper(); } private: boost::numeric::interval m_interval; }; } //namespace implem template class IDManager { public: typedef SaltedID value_type; typedef typename value_type::index_type index_type; enum { bit_count = sizeof(value_type) * CHAR_BIT }; IDManager ( void ); value_type allocate ( void ); void free ( value_type parID ); void reset ( void ); private: typedef typename value_type::salt_type salt_type; using id_interval = implem::IDInterval; using interval_set = std::set; interval_set m_free_intervals; std::vector m_salts; }; namespace implem { template bool IDInterval::operator< ( const IDInterval& parOther) const { return (m_interval.lower() < parOther.m_interval.lower()) and (m_interval.upper() < parOther.m_interval.upper()); } } //namespace implem template IDManager::IDManager() { m_free_intervals.insert(id_interval(1, value_type::max_index)); } template auto IDManager::allocate() -> value_type { DK_ASSERT(not m_free_intervals.empty()); auto first = *m_free_intervals.begin(); auto free_id = first.lower(); m_free_intervals.erase(m_free_intervals.begin()); if (first.lower() + 1 <= first.upper()) { m_free_intervals.insert(typename interval_set::value_type(first.lower() + 1, first.upper())); } if (static_cast(m_salts.size()) < free_id) { const auto old_size = m_salts.size(); m_salts.resize(free_id); std::fill(m_salts.begin() + old_size, m_salts.end(), 0); } DK_ASSERT(free_id > 0); const auto salt_index = free_id - 1; if (m_salts[salt_index] < value_type::max_salt) { return value_type(free_id, m_salts[salt_index]++); } else { return value_type(free_id, m_salts[salt_index]); } } template void IDManager::free (value_type parID) { if (parID.salt() == value_type::max_salt) return; const auto id = parID.index(); DK_ASSERT(id > 0); if (m_salts.size() < id or 0 == id) return; DK_ASSERT(parID.salt() == m_salts[id - 1] - 1); if (parID.salt() != m_salts[id - 1] - 1) return; auto it = m_free_intervals.find(id_interval(id, id)); if (it != m_free_intervals.end() and it->lower() <= id and it->upper() > id) { return; } it = m_free_intervals.upper_bound(id_interval(id, id)); if (m_free_intervals.end() == it) { return; } else { auto free_interval = *it; if (id + 1 != free_interval.lower()) { m_free_intervals.insert(id_interval(id, id)); } else { if (m_free_intervals.begin() != it) { auto it_2 = it; --it_2; if (it_2->lower() + 1 == id) { auto free_interval_2 = *it_2; m_free_intervals.erase(it); m_free_intervals.erase(it_2); m_free_intervals.insert(id_interval(free_interval_2.lower(), free_interval.upper())); } else { m_free_intervals.erase(it); m_free_intervals.insert(id_interval(id, free_interval.lower())); } } else { m_free_intervals.erase(it); m_free_intervals.insert(id_interval(id, free_interval.upper())); } } } } template void IDManager::reset() { m_free_intervals.clear(); m_free_intervals.insert(id_interval(1, value_type::max_index)); m_salts.clear(); } } //namespace dk #endif