Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FIFO type of container - Which STL Container is most suitable and why?

I was recently tasked on implementing a buffer which will be used as a temporary storage by a logging class. The logging class itself is a singleton and the observer listener pattern is used. You can expect thousands of messages to be logged with this class.

Now the problem lies here :

We have a trace logging option which is used for deguging purposes. When this option is on, the count of messages/second is increased exponentially. In the release code trace logging is disabled, however a buffer which can store a fixed number of messages e.g. 10000 is dumped into the log IF an error occurs, so that the developers can identify the root of the problem.

If the buffer is full the oldest message is removed to free space for the newest message.

void Log::storeToBuffer(const LogType_E & type_in, const LogDomain_E & domain_in,const int & id_in, const char * msg_in)
{
    if(this->myEnableTraceBuffer)
    {
        if(static_cast<std::list<Message> * >(this->myRingBuffer)->size() < this->myRingBufferMaxSize)
        {
            static_cast<std::list<Message> * >(this->myRingBuffer)->push_back(Message(type_in, domain_in, id_in, msg_in));
        }
        else
        {
            //buffer full so remove oldest element and add new
            if(static_cast<std::list<Message> * >(this->myRingBuffer)->size() > 0) static_cast<std::list<Message> * >(this->myRingBuffer)->pop_front();
            static_cast<std::list<Message> * >(this->myRingBuffer)->push_back(Message(type_in, domain_in, id_in, msg_in));
        }
    }
}

I implemented this with std::list , very simply using push_back/pop_front to exploit the constant delete/insert execution time. (don't ask for the void casting, not my decision).

But since the buffer size is fixed, and not likely to be changed during the lifetime of an object, maybe vector with explicit index manipulation is more suitable? For example there can be two indeces , start/current starting both at position 0. When the vector is full and we add something start moves to position 1 and and current to position 0, thus when printing the result we have the right order.

Maybe another STL container is more suitable for this kind of thing?

Thank your for your patience to read this long wall of text. I am here to answer any questions.

like image 652
FailedDev Avatar asked Nov 04 '11 20:11

FailedDev


People also ask

Which of the following are STL containers types?

The three types of containers found in the STL are sequential, associative and unordered.

What kind of container is the STL vector?

1) std::vector is a sequence container that encapsulates dynamic size arrays.


2 Answers

The std::deque can efficiently insert or delete at either the beginning or the end, making it an excellent structure for a buffer. It can be used as the template type for std::queue which would be perfect for your application - just check the size of the queue each time you do a push, and pop if the size is over the maximum.

like image 59
Mark Ransom Avatar answered Nov 03 '22 00:11

Mark Ransom


Ever heard of std::deque? :)
It allows amortized constant time random access and constant time push/pop operations on both sides. As such, it can be easily used as a FIFO queue.

That said, since it can be used as such so easily, there is a container adaptor std::queue, though it doesn't offer random access and an iterator interface.

like image 27
Xeo Avatar answered Nov 02 '22 23:11

Xeo