<commit_before>#include "gridmanager.h"
#include "user.h"
#include "machine.h"
#include "job.h"
#include "log.h"
#include "loader.h"
#include "menu.h"

#include <chrono>
#include <iostream>
#include <map>
#include <algorithm>
#include <numeric>

uint GridManager::_lastUserId = 0;
uint GridManager::_lastMachineId = 0;
Menu* GridManager::_menu = Loader<Menu>("gridManagerMenu.txt").Load();

GridManager::~GridManager()
{
    for (auto user : _users)
        delete user;

    for (auto machine : _machines)
        delete machine;
}

bool GridManager::Save(ByteBuffer& bb) const
{
    bb.WriteUInt32(_lastUserId);
    bb.WriteUInt32(_lastMachineId);
    bb.WriteUInt32(Machine::GetLastJobId());

    bb.WriteUInt32(_users.size());
    for (auto user : _users)
        user->Save(bb);

    bb.WriteUInt32(_machines.size());
    for (auto machine : _machines)
        machine->Save(bb);

    bb.WriteUInt32(_priorityMachines.size());
    for (auto pMachine : _priorityMachines)
        pMachine->Save(bb);

    _idleUsers.Save(bb);

    return true;
}

GridManager* GridManager::Load(ByteBuffer& bb)
{
    GridManager* gm = new GridManager();

    _lastUserId = bb.ReadUInt32();
    _lastMachineId = bb.ReadUInt32();
    Machine::SetLastJobId(bb.ReadUInt32());

    uint32 usersCount = bb.ReadUInt32();
    for (uint32 i = 0; i < usersCount; ++i)
        gm->AddUser(User::Load(bb));

    uint32 machinesCount = bb.ReadUInt32();
    for (uint32 i = 0; i < machinesCount; ++i)
        gm->AddMachine(Machine::Load(bb));

    uint32 priorityMachinesCount = bb.ReadUInt32();
    for (uint32 i = 0; i < priorityMachinesCount; ++i)
        gm->AddPriorityMachine(PriorityMachine::Load(bb));

    gm->_idleUsers = *IdleUserContainer::Load(bb);

    return gm;
}

bool GridManager::RemoveUser(const User* user)
{
    if (!user) return NULL;
    auto it = std::find_if(_users.begin(), _users.end(), [user] (User* usr) { return usr == user; });
    if (it == _users.end())
    {
        sLog(Console)->Log("GridManager::RemoveUser(User*): Could not find user %s in GridManager", user->GetName().c_str());
        return false;
    }

    _idleUsers.InsertUser(IdleUser::FromUser(*it));

    delete *it;
    _users.erase(it);
<commit_msg>Implement save of waiting jobs queue.<commit_after>#include "gridmanager.h"
#include "user.h"
#include "machine.h"
#include "job.h"
#include "log.h"
#include "loader.h"
#include "menu.h"

#include <chrono>
#include <iostream>
#include <map>
#include <algorithm>
#include <numeric>

uint GridManager::_lastUserId = 0;
uint GridManager::_lastMachineId = 0;
Menu* GridManager::_menu = Loader<Menu>("gridManagerMenu.txt").Load();

GridManager::~GridManager()
{
    for (auto user : _users)
        delete user;

    for (auto machine : _machines)
        delete machine;
}

bool GridManager::Save(ByteBuffer& bb) const
{
    bb.WriteUInt32(_lastUserId);
    bb.WriteUInt32(_lastMachineId);
    bb.WriteUInt32(Machine::GetLastJobId());

    bb.WriteUInt32(_users.size());
    for (auto user : _users)
        user->Save(bb);

    bb.WriteUInt32(_machines.size());
    for (auto machine : _machines)
        machine->Save(bb);

    bb.WriteUInt32(_priorityMachines.size());
    for (auto pMachine : _priorityMachines)
        pMachine->Save(bb);

    bb.WriteUInt32(_waitingJobs.size());
    for (std::queue<Job*> temp(_waitingJobs); !temp.empty(); temp.pop())
        temp.front()->Save(bb);

    _idleUsers.Save(bb);

    return true;
}

GridManager* GridManager::Load(ByteBuffer& bb)
{
    GridManager* gm = new GridManager();

    _lastUserId = bb.ReadUInt32();
    _lastMachineId = bb.ReadUInt32();
    Machine::SetLastJobId(bb.ReadUInt32());

    uint32 usersCount = bb.ReadUInt32();
    for (uint32 i = 0; i < usersCount; ++i)
        gm->AddUser(User::Load(bb));

    uint32 machinesCount = bb.ReadUInt32();
    for (uint32 i = 0; i < machinesCount; ++i)
        gm->AddMachine(Machine::Load(bb));

    uint32 priorityMachinesCount = bb.ReadUInt32();
    for (uint32 i = 0; i < priorityMachinesCount; ++i)
        gm->AddPriorityMachine(PriorityMachine::Load(bb));

    uint32 waitingJobsCount = bb.ReadUInt32();
    for (uint32 i = 0; i < waitingJobsCount; ++i)
        gm->AddJob(Job::Load(bb));

    gm->_idleUsers = *IdleUserContainer::Load(bb);

    return gm;
}

bool GridManager::RemoveUser(const User* user)
{
    if (!user) return NULL;
    auto it = std::find_if(_users.begin(), _users.end(), [user] (User* usr) { return usr == user; });
    if (it == _users.end())
    {
        sLog(Console)->Log("GridManager::RemoveUser(User*): Could not find user %s in GridManager", user->GetName().c_str());
        return false;
    }

    _idleUsers.InsertUser(IdleUser::FromUser(*it));

    delete *it;
    _users.erase(it);

{'fraction_non_alphanumeric': 0.1576955424726661, 'fraction_numerical': 0.019133725820016823, 'mean_word_length': 3.74750499001996, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7419235', 'n_tokens_mistral': 1715, 'n_tokens_neox': 1607, 'n_words': 341}