efficient circular queue reorder
I am using a circular queue as a data buffer. I am using a char array to hold the data and my problem is when I need to view the data in the buffer in its original order. the reordering of the buffer is taking up most of the processing time with 64%. I need this to be as optimized as possible because I use this buffer to search for signatures in gigabytes of data. Any optimization suggestions would be greatly appreciated and/or any other suggestions on how to store this buffer and easy convert it to a char array for processing. thanks in advance
edit: I apologize what I mean by reorder is if the data is "abcdef" then there is a good chance the buffer will look like "defabc" so I need to reorder the buffer to "abcdef"
queue structu开发者_如何转开发res:
typedef struct item{
unsigned char* data;
int length;
} item;
typedef struct queue{
unsigned char *data;
int front;
int back;
int length;
int size;
} queue;
reorder function:
int toStr(queue *ptr, item *ret){
int length;
int i;
int j;
int back = ptr->back;
int size = ptr->size;
char* tmp;
char* tmp_data;
if(ptr->length == 0){
return 0;
}
tmp = ret->data;
ret->length = ptr->length;
tmp_data = ptr->data;
i = ptr->front;
j = 0;
while(i != back){
//ret->data[j] = tmp_data[i];
tmp[j] = tmp_data[i];
++i;
++j;
if(i > size){
i = 0;
}
}
return 1;
}
Right now you're copying all the data just to see it in order. I think I'd provide a function to convert a subscript into a pointer to the correct place in the buffer:
unsigned char *sub(queue const *q, int subscript) {
return q->data + (front + subscript) % q->size);
}
Then using the data in order is something like:
for (i=0; i<q->size; i++)
printf("%c\n", *sub(q, i));
精彩评论