Skip to content

Commit

Permalink
Use std::vector and std::deque instead of most std::lists
Browse files Browse the repository at this point in the history
Use `std::vector` in cases where elements are only inserted at the end of the collection.

Use `std::deque` in cases where elements are only inserted/deleted at the beginning/end of the collection.

Use `std::list` in the remaining single case where elements are being removed from arbitrary positions and added at either the beginning or the end of the collection.

Adjust variables names. Don't use separate prefix for `std::deque`s and `std::list`s, as they are only used very rarely. Closes ddnet#6779.
  • Loading branch information
Robyt3 committed Jul 1, 2023
1 parent ae685e8 commit bc73ea3
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 61 deletions.
14 changes: 7 additions & 7 deletions src/engine/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2879,18 +2879,18 @@ void CClient::Update()

if(State() == IClient::STATE_ONLINE)
{
if(!m_lpEditJobs.empty())
if(!m_EditJobs.empty())
{
std::shared_ptr<CDemoEdit> e = m_lpEditJobs.front();
if(e->Status() == IJob::STATE_DONE)
std::shared_ptr<CDemoEdit> pJob = m_EditJobs.front();
if(pJob->Status() == IJob::STATE_DONE)
{
char aBuf[256];
str_format(aBuf, sizeof(aBuf), "Successfully saved the replay to %s!", e->Destination());
char aBuf[IO_MAX_PATH_LENGTH + 64];
str_format(aBuf, sizeof(aBuf), "Successfully saved the replay to %s!", pJob->Destination());
m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "replay", aBuf);

GameClient()->Echo(Localize("Successfully saved the replay!"));

m_lpEditJobs.pop_front();
m_EditJobs.pop_front();
}
}
}
Expand Down Expand Up @@ -3818,7 +3818,7 @@ void CClient::SaveReplay(const int Length, const char *pFilename)
// Create a job to do this slicing in background because it can be a bit long depending on the file size
std::shared_ptr<CDemoEdit> pDemoEditTask = std::make_shared<CDemoEdit>(GameClient()->NetVersion(), &m_SnapshotDelta, m_pStorage, pSrc, aFilename, StartTick, EndTick);
Engine()->AddJob(pDemoEditTask);
m_lpEditJobs.push_back(pDemoEditTask);
m_EditJobs.push_back(pDemoEditTask);

// And we restart the recorder
DemoRecorder_StartReplayRecorder();
Expand Down
4 changes: 2 additions & 2 deletions src/engine/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#ifndef ENGINE_CLIENT_CLIENT_H
#define ENGINE_CLIENT_CLIENT_H

#include <list>
#include <deque>
#include <memory>

#include <base/hash.h>
Expand Down Expand Up @@ -246,7 +246,7 @@ class CClient : public IClient, public CDemoPlayer::IListener

CSnapshotDelta m_SnapshotDelta;

std::list<std::shared_ptr<CDemoEdit>> m_lpEditJobs;
std::deque<std::shared_ptr<CDemoEdit>> m_EditJobs;

//
bool m_CanReceiveServerCapabilities;
Expand Down
12 changes: 6 additions & 6 deletions src/engine/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1858,7 +1858,7 @@ static inline int GetCacheIndex(int Type, bool SendClient)

CServer::CCache::CCache()
{
m_Cache.clear();
m_vCache.clear();
}

CServer::CCache::~CCache()
Expand All @@ -1873,12 +1873,12 @@ CServer::CCache::CCacheChunk::CCacheChunk(const void *pData, int Size)

void CServer::CCache::AddChunk(const void *pData, int Size)
{
m_Cache.emplace_back(pData, Size);
m_vCache.emplace_back(pData, Size);
}

void CServer::CCache::Clear()
{
m_Cache.clear();
m_vCache.clear();
}

void CServer::CacheServerInfo(CCache *pCache, int Type, bool SendClients)
Expand Down Expand Up @@ -2178,12 +2178,12 @@ void CServer::SendServerInfo(const NETADDR *pAddr, int Token, int Type, bool Sen
Packet.m_Address = *pAddr;
Packet.m_Flags = NETSENDFLAG_CONNLESS;

for(const auto &Chunk : pCache->m_Cache)
for(const auto &Chunk : pCache->m_vCache)
{
p.Reset();
if(Type == SERVERINFO_EXTENDED)
{
if(&Chunk == &pCache->m_Cache.front())
if(&Chunk == &pCache->m_vCache.front())
p.AddRaw(SERVERBROWSE_INFO_EXTENDED, sizeof(SERVERBROWSE_INFO_EXTENDED));
else
p.AddRaw(SERVERBROWSE_INFO_EXTENDED_MORE, sizeof(SERVERBROWSE_INFO_EXTENDED_MORE));
Expand Down Expand Up @@ -2222,7 +2222,7 @@ void CServer::GetServerInfoSixup(CPacker *pPacker, int Token, bool SendClients)

SendClients = SendClients && Token != -1;

CCache::CCacheChunk &FirstChunk = m_aSixupServerInfoCache[SendClients].m_Cache.front();
CCache::CCacheChunk &FirstChunk = m_aSixupServerInfoCache[SendClients].m_vCache.front();
pPacker->AddRaw(FirstChunk.m_vData.data(), FirstChunk.m_vData.size());
}

Expand Down
3 changes: 2 additions & 1 deletion src/engine/server/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,12 @@ class CServer : public IServer
public:
CCacheChunk(const void *pData, int Size);
CCacheChunk(const CCacheChunk &) = delete;
CCacheChunk(CCacheChunk &&) = default;

std::vector<uint8_t> m_vData;
};

std::list<CCacheChunk> m_Cache;
std::vector<CCacheChunk> m_vCache;

CCache();
~CCache();
Expand Down
6 changes: 3 additions & 3 deletions src/game/client/prediction/entities/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1029,9 +1029,9 @@ void CCharacter::DDRacePostCoreTick()
HandleSkippableTiles(CurrentIndex);

// handle Anti-Skip tiles
std::list<int> Indices = Collision()->GetMapIndices(m_PrevPos, m_Pos);
if(!Indices.empty())
for(int Index : Indices)
std::vector<int> vIndices = Collision()->GetMapIndices(m_PrevPos, m_Pos);
if(!vIndices.empty())
for(int Index : vIndices)
HandleTiles(Index);
else
{
Expand Down
9 changes: 4 additions & 5 deletions src/game/client/prediction/gameworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,9 @@ CCharacter *CGameWorld::IntersectCharacter(vec2 Pos0, vec2 Pos1, float Radius, v
return pClosest;
}

std::list<class CCharacter *> CGameWorld::IntersectedCharacters(vec2 Pos0, vec2 Pos1, float Radius, const CEntity *pNotThis)
std::vector<CCharacter *> CGameWorld::IntersectedCharacters(vec2 Pos0, vec2 Pos1, float Radius, const CEntity *pNotThis)
{
std::list<CCharacter *> listOfChars;

std::vector<CCharacter *> vpCharacters;
CCharacter *pChr = (CCharacter *)FindFirst(CGameWorld::ENTTYPE_CHARACTER);
for(; pChr; pChr = (CCharacter *)pChr->TypeNext())
{
Expand All @@ -303,11 +302,11 @@ std::list<class CCharacter *> CGameWorld::IntersectedCharacters(vec2 Pos0, vec2
float Len = distance(pChr->m_Pos, IntersectPos);
if(Len < pChr->m_ProximityRadius + Radius)
{
listOfChars.push_back(pChr);
vpCharacters.push_back(pChr);
}
}
}
return listOfChars;
return vpCharacters;
}

void CGameWorld::ReleaseHooked(int ClientID)
Expand Down
3 changes: 2 additions & 1 deletion src/game/client/prediction/gameworld.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <game/teamscore.h>

#include <list>
#include <vector>

class CCollision;
class CCharacter;
Expand Down Expand Up @@ -49,7 +50,7 @@ class CGameWorld

// DDRace
void ReleaseHooked(int ClientID);
std::list<CCharacter *> IntersectedCharacters(vec2 Pos0, vec2 Pos1, float Radius, const CEntity *pNotThis = nullptr);
std::vector<CCharacter *> IntersectedCharacters(vec2 Pos0, vec2 Pos1, float Radius, const CEntity *pNotThis = nullptr);

int m_GameTick;
int m_GameTickSpeed;
Expand Down
6 changes: 3 additions & 3 deletions src/game/client/race.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ bool CRaceHelper::IsStart(CGameClient *pClient, vec2 Prev, vec2 Pos)
}
else
{
std::list<int> Indices = pCollision->GetMapIndices(Prev, Pos);
if(!Indices.empty())
for(int &Indice : Indices)
std::vector<int> vIndices = pCollision->GetMapIndices(Prev, Pos);
if(!vIndices.empty())
for(int &Indice : vIndices)
{
if(pCollision->GetTileIndex(Indice) == TILE_START)
return true;
Expand Down
18 changes: 9 additions & 9 deletions src/game/collision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -880,9 +880,9 @@ int CCollision::GetMapIndex(vec2 Pos) const
return -1;
}

std::list<int> CCollision::GetMapIndices(vec2 PrevPos, vec2 Pos, unsigned MaxIndices) const
std::vector<int> CCollision::GetMapIndices(vec2 PrevPos, vec2 Pos, unsigned MaxIndices) const
{
std::list<int> Indices;
std::vector<int> vIndices;
float d = distance(PrevPos, Pos);
int End(d + 1);
if(!d)
Expand All @@ -893,11 +893,11 @@ std::list<int> CCollision::GetMapIndices(vec2 PrevPos, vec2 Pos, unsigned MaxInd

if(TileExists(Index))
{
Indices.push_back(Index);
return Indices;
vIndices.push_back(Index);
return vIndices;
}
else
return Indices;
return vIndices;
}
else
{
Expand All @@ -911,14 +911,14 @@ std::list<int> CCollision::GetMapIndices(vec2 PrevPos, vec2 Pos, unsigned MaxInd
int Index = Ny * m_Width + Nx;
if(TileExists(Index) && LastIndex != Index)
{
if(MaxIndices && Indices.size() > MaxIndices)
return Indices;
Indices.push_back(Index);
if(MaxIndices && vIndices.size() > MaxIndices)
return vIndices;
vIndices.push_back(Index);
LastIndex = Index;
}
}

return Indices;
return vIndices;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/game/collision.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <base/vmath.h>
#include <engine/shared/protocol.h>

#include <list>
#include <vector>

enum
{
Expand Down Expand Up @@ -73,7 +73,7 @@ class CCollision
int Entity(int x, int y, int Layer) const;
int GetPureMapIndex(float x, float y) const;
int GetPureMapIndex(vec2 Pos) const { return GetPureMapIndex(Pos.x, Pos.y); }
std::list<int> GetMapIndices(vec2 PrevPos, vec2 Pos, unsigned MaxIndices = 0) const;
std::vector<int> GetMapIndices(vec2 PrevPos, vec2 Pos, unsigned MaxIndices = 0) const;
int GetMapIndex(vec2 Pos) const;
bool TileExists(int Index) const;
bool TileExistsNext(int Index) const;
Expand Down
8 changes: 4 additions & 4 deletions src/game/editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6459,7 +6459,7 @@ void CEditor::RenderPressedKeys(CUIRect View)

void CEditor::RenderSavingIndicator(CUIRect View)
{
if(m_lpWriterFinishJobs.empty())
if(m_WriterFinishJobs.empty())
return;

UI()->MapScreen();
Expand Down Expand Up @@ -7065,10 +7065,10 @@ bool CEditor::PerformAutosave()

void CEditor::HandleWriterFinishJobs()
{
if(m_lpWriterFinishJobs.empty())
if(m_WriterFinishJobs.empty())
return;

std::shared_ptr<CDataFileWriterFinishJob> pJob = m_lpWriterFinishJobs.front();
std::shared_ptr<CDataFileWriterFinishJob> pJob = m_WriterFinishJobs.front();
if(pJob->Status() != IJob::STATE_DONE)
return;

Expand All @@ -7095,7 +7095,7 @@ void CEditor::HandleWriterFinishJobs()
}
}

m_lpWriterFinishJobs.pop_front();
m_WriterFinishJobs.pop_front();
}

void CEditor::OnUpdate()
Expand Down
4 changes: 2 additions & 2 deletions src/game/editor/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "auto_map.h"

#include <chrono>
#include <list>
#include <deque>
#include <string>
#include <vector>

Expand Down Expand Up @@ -1154,7 +1154,7 @@ class CEditor : public IEditor
static const void *ms_pUiGotContext;

CEditorMap m_Map;
std::list<std::shared_ptr<CDataFileWriterFinishJob>> m_lpWriterFinishJobs;
std::deque<std::shared_ptr<CDataFileWriterFinishJob>> m_WriterFinishJobs;

int m_ShiftBy;

Expand Down
4 changes: 2 additions & 2 deletions src/game/editor/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct CSoundSource_DEPRECATED
bool CEditor::Save(const char *pFilename)
{
// Check if file with this name is already being saved at the moment
if(std::any_of(std::begin(m_lpWriterFinishJobs), std::end(m_lpWriterFinishJobs), [pFilename](const std::shared_ptr<CDataFileWriterFinishJob> &Job) { return str_comp(pFilename, Job->GetFileName()) == 0; }))
if(std::any_of(std::begin(m_WriterFinishJobs), std::end(m_WriterFinishJobs), [pFilename](const std::shared_ptr<CDataFileWriterFinishJob> &Job) { return str_comp(pFilename, Job->GetFileName()) == 0; }))
return false;

return m_Map.Save(pFilename);
Expand Down Expand Up @@ -378,7 +378,7 @@ bool CEditorMap::Save(const char *pFileName)
// finish the data file
std::shared_ptr<CDataFileWriterFinishJob> pWriterFinishJob = std::make_shared<CDataFileWriterFinishJob>(pFileName, std::move(df));
m_pEditor->Engine()->AddJob(pWriterFinishJob);
m_pEditor->m_lpWriterFinishJobs.push_back(pWriterFinishJob);
m_pEditor->m_WriterFinishJobs.push_back(pWriterFinishJob);

return true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/game/server/entities/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2094,10 +2094,10 @@ void CCharacter::DDRacePostCoreTick()
return;

// handle Anti-Skip tiles
std::list<int> Indices = Collision()->GetMapIndices(m_PrevPos, m_Pos);
if(!Indices.empty())
std::vector<int> vIndices = Collision()->GetMapIndices(m_PrevPos, m_Pos);
if(!vIndices.empty())
{
for(int &Index : Indices)
for(int &Index : vIndices)
{
HandleTiles(Index);
if(!m_Alive)
Expand Down
7 changes: 3 additions & 4 deletions src/game/server/entities/light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ CLight::CLight(CGameWorld *pGameWorld, vec2 Pos, float Rotation, int Length,

bool CLight::HitCharacter()
{
std::list<CCharacter *> HitCharacters =
GameServer()->m_World.IntersectedCharacters(m_Pos, m_To, 0.0f, 0);
if(HitCharacters.empty())
std::vector<CCharacter *> vpHitCharacters = GameServer()->m_World.IntersectedCharacters(m_Pos, m_To, 0.0f, 0);
if(vpHitCharacters.empty())
return false;
for(auto *pChar : HitCharacters)
for(auto *pChar : vpHitCharacters)
{
if(m_Layer == LAYER_SWITCH && m_Number > 0 && !Switchers()[m_Number].m_aStatus[pChar->Team()])
continue;
Expand Down
9 changes: 4 additions & 5 deletions src/game/server/gameworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,9 @@ CCharacter *CGameWorld::ClosestCharacter(vec2 Pos, float Radius, const CEntity *
return pClosest;
}

std::list<class CCharacter *> CGameWorld::IntersectedCharacters(vec2 Pos0, vec2 Pos1, float Radius, const CEntity *pNotThis)
std::vector<CCharacter *> CGameWorld::IntersectedCharacters(vec2 Pos0, vec2 Pos1, float Radius, const CEntity *pNotThis)
{
std::list<CCharacter *> listOfChars;

std::vector<CCharacter *> vpCharacters;
CCharacter *pChr = (CCharacter *)FindFirst(CGameWorld::ENTTYPE_CHARACTER);
for(; pChr; pChr = (CCharacter *)pChr->TypeNext())
{
Expand All @@ -416,11 +415,11 @@ std::list<class CCharacter *> CGameWorld::IntersectedCharacters(vec2 Pos0, vec2
if(Len < pChr->m_ProximityRadius + Radius)
{
pChr->m_Intersection = IntersectPos;
listOfChars.push_back(pChr);
vpCharacters.push_back(pChr);
}
}
}
return listOfChars;
return vpCharacters;
}

void CGameWorld::ReleaseHooked(int ClientID)
Expand Down
4 changes: 2 additions & 2 deletions src/game/server/gameworld.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <game/gamecore.h>

#include <list>
#include <vector>

class CEntity;
class CCharacter;
Expand Down Expand Up @@ -165,7 +165,7 @@ class CGameWorld
Returns:
Returns list with all Characters on line.
*/
std::list<CCharacter *> IntersectedCharacters(vec2 Pos0, vec2 Pos1, float Radius, const CEntity *pNotThis = nullptr);
std::vector<CCharacter *> IntersectedCharacters(vec2 Pos0, vec2 Pos1, float Radius, const CEntity *pNotThis = nullptr);
};

#endif

0 comments on commit bc73ea3

Please sign in to comment.