#ifndef TILE_BASE_INTERNAL_INDEX_ALLOC_H
#define TILE_BASE_INTERNAL_INDEX_ALLOC_H

#pragma once

#include "tile/base/never_destroyed.h"
#include "tile/base/thread/mutex.h"

namespace tile {
namespace internal {

class IndexAlloc {
public:
    template<typename Tag>
    static IndexAlloc *For()
    {
        static NeverDestroyedSingleton<IndexAlloc> ia;
        return ia.Get();
    }

    std::size_t Next();
    void Free(std::size_t index);

private:
    friend class NeverDestroyedSingleton<IndexAlloc>;
    IndexAlloc()  = default;
    ~IndexAlloc() = default;

private:
    Mutex lock_;
    std::size_t current_{};
    std::vector<std::size_t> recycled_;
};

}// namespace internal
}// namespace tile

#endif// TILE_BASE_INTERNAL_INDEX_ALLOC_H
