diff --git a/src/creflector.cpp b/src/creflector.cpp index 9acf714..a5cb278 100644 --- a/src/creflector.cpp +++ b/src/creflector.cpp @@ -647,9 +647,9 @@ void CReflector::WriteXmlFile(std::ofstream &xmlFile) // lock CUsers *users = GetUsers(); // iterate on users - for ( int i = 0; i < users->GetSize(); i++ ) + for ( auto it=users->begin(); it!=users->end(); it++ ) { - users->GetUser(i)->WriteXml(xmlFile); + (*it).WriteXml(xmlFile); } // unlock ReleaseUsers(); @@ -724,10 +724,10 @@ void CReflector::SendJsonStationsObject(CUdpSocket &Socket, CIp &Ip) // lock CUsers *users = GetUsers(); // iterate on users - for ( int i = 0; i < users->GetSize(); i++ ) + for ( auto it=users->begin(); it!=users->end(); ) { - users->GetUser(i)->GetJsonObject(Buffer); - if ( i < users->GetSize()-1 ) + (*it++).GetJsonObject(Buffer); + if ( it != users->end() ) { ::strcat(Buffer, ","); } diff --git a/src/cusers.cpp b/src/cusers.cpp index c4dddd7..fbb7271 100644 --- a/src/cusers.cpp +++ b/src/cusers.cpp @@ -4,6 +4,7 @@ // // Created by Jean-Luc Deltombe (LX3JL) on 13/11/2015. // Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved. +// Copyright © 2020 Thomas A. Early, N7TAE // // ---------------------------------------------------------------------------- // This file is part of xlxd. @@ -29,10 +30,7 @@ //////////////////////////////////////////////////////////////////////////////////////// // constructor -CUsers::CUsers() -{ - m_Users.reserve(LASTHEARD_USERS_MAX_SIZE); -} +CUsers::CUsers() {} //////////////////////////////////////////////////////////////////////////////////////// // users management @@ -40,11 +38,8 @@ CUsers::CUsers() void CUsers::AddUser(const CUser &user) { // add - m_Users.push_back(user); - - // sort list by descending time (fisrt is youngest) - std::sort(m_Users.begin(), m_Users.end()); - + m_Users.push_front(user); + // if list size too big, remove oldest if ( m_Users.size() >= (LASTHEARD_USERS_MAX_SIZE-1) ) { @@ -66,27 +61,16 @@ void CUsers::Hearing(const CCallsign &my, const CCallsign &rpt1, const CCallsign void CUsers::Hearing(const CCallsign &my, const CCallsign &rpt1, const CCallsign &rpt2, const CCallsign &xlx) { CUser heard(my, rpt1, rpt2, xlx); - - // first check if we have this user listed yet - bool found = false; - for ( int i = 0; (i < m_Users.size()) && !found; i++ ) + + // first check if this user is already listed. If so, erase him. + for ( auto it=begin(); it!=end(); it++ ) { - found = (m_Users[i] == heard); - if ( found ) + if (*it == heard) { - m_Users[i].HeardNow(); + m_Users.erase(it); + break; } } - - // if not found, add user to list - // otherwise just re-sort the list - if ( !found ) - { - AddUser(heard); - } - else - { - std::sort(m_Users.begin(), m_Users.end()); - } -} + AddUser(heard); +} diff --git a/src/cusers.h b/src/cusers.h index 460bde9..0227f32 100644 --- a/src/cusers.h +++ b/src/cusers.h @@ -4,6 +4,7 @@ // // Created by Jean-Luc Deltombe (LX3JL) on 13/11/2015. // Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved. +// Copyright © 2020 Thomas A. Early, N7TAE // // ---------------------------------------------------------------------------- // This file is part of xlxd. @@ -34,18 +35,21 @@ class CUsers public: // constructor CUsers(); - + // destructor virtual ~CUsers() {} // locks void Lock(void) { m_Mutex.lock(); } void Unlock(void) { m_Mutex.unlock(); } - + // management int GetSize(void) const { return (int)m_Users.size(); } void AddUser(const CUser &); - CUser *GetUser(int i) { return &m_Users[i]; } + + // pass-thru + std::list::iterator begin() { return m_Users.begin(); } + std::list::iterator end() { return m_Users.end(); } // operation void Hearing(const CCallsign &, const CCallsign &, const CCallsign &); @@ -53,8 +57,8 @@ public: protected: // data - std::mutex m_Mutex; - std::vector m_Users; + std::mutex m_Mutex; + std::list m_Users; }; ////////////////////////////////////////////////////////////////////////////////////////