C ++: A queue with an efficient set / set of elements?

So, I feel that C ++ should have a good built-in solution for this, but I'm not sure what it is.

I need a queue (ideally thread safe, but I can wrap it myself during synchronization), which efficiently processes groups of bytes - allows me to read / write different sizes.

therefore, the interface looks, for example,

//removes the first bytesToRead elements from the front of the queue and places them in array; returns the actual number of bytes dequeued
int dequeue(unsigned char *array, int bytesToRead) 
//Adds bytesToWrite elements from array to the end of the queue; does nothing and returns 0 if this would exceed the queue max size
int enqueue(unsigned char *array, int bytesToWrite)

I can write one myself without much difficulty, but it looks like it should be something that is easy to do from the shelf.

The best thing about STL is as if it could be a stringbuf - I would have to bind the sgetc / pubseekoff calls manually, but it looks like it would work.

I want to do this as a replacement for the current queue implementation, which is related to a performance issue; the read in this implementation is O (N) of the amount of data in the queue. (This is a very naive implementation - each dequeue copies an array of the remaining data in the queue.)

Additional requirements (I can implement them in the shell, if necessary): - I need to specify the maximum buffer size -Risk operations should retrieve all available data, if less data is available than was requested -Write operations should not do anything if the requested record exceeds the maximum size and returns a failure indicator

, : 1) stringbuf? / O (1) , ? (, O (n) .)

2) - , , ?

!

+5
4

... :

class queue { 
      std::deque<unsigned char> data;
public:
    int enqueue(unsigned char *array, int bytesToWrite) { 
        data.insert(data.end(), array, array+bytesToWrite);
    }

    int dequeue(unsigned char *array, int bytesToRead) { 
        std::copy(data.begin(), data.begin()+bytesToRead, array);
        // or, for C++11: std::copy_n(data.begin(), bytesToRead, array);

        data.erase(data.begin(), data.begin()+bytesToRead);
    }
};

, , , , . , , , (, , ).

/ .

+7

, , , ( , ), memcpy, - .

, .

+1

std::stringstream, write read?

0

, std::stringstream, , .

std::deque, ( / , , O (N) , ), , std::deque ( ( )), / , , :

std::deque<unsigned char> buf;

int dequeue(unsigned char *array, int bytesToRead) {
  int result = std::min(bytesToRead, buf.size());
  std::copy(buf.begin(), buf.begin() + result, array);
  buf.erase(buf.begin(), buf.begin() + result);
  return result;
}; 

int enqueue(unsigned char *array, int bytesToWrite) {
  buf.insert(buf.end(), array, array + bytesToWrite);
  return bytesToWrite;
};

, , .

0

All Articles