Use placement new to construct the queue

Change-Id: I455f9954165bd4524f2883db7ea24a6fed3015f9
This commit is contained in:
satok 2012-01-13 15:41:17 +09:00
parent f67f001e40
commit b960477952
3 changed files with 31 additions and 12 deletions

View file

@ -205,6 +205,7 @@ static void dumpWord(const unsigned short* word, const int length) {
// Word limit for sub queues used in WordsPriorityQueuePool. Sub queues are temporary queues used // Word limit for sub queues used in WordsPriorityQueuePool. Sub queues are temporary queues used
// for better performance. // for better performance.
#define SUB_QUEUE_MAX_WORDS 5 #define SUB_QUEUE_MAX_WORDS 5
#define SUB_QUEUE_MAX_COUNT 10
#define MAX_DEPTH_MULTIPLIER 3 #define MAX_DEPTH_MULTIPLIER 3

View file

@ -48,6 +48,7 @@ class WordsPriorityQueue {
mSuggestedWords[i].mUsed = false; mSuggestedWords[i].mUsed = false;
} }
} }
~WordsPriorityQueue() { ~WordsPriorityQueue() {
delete[] mSuggestedWords; delete[] mSuggestedWords;
} }

View file

@ -17,6 +17,8 @@
#ifndef LATINIME_WORDS_PRIORITY_QUEUE_POOL_H #ifndef LATINIME_WORDS_PRIORITY_QUEUE_POOL_H
#define LATINIME_WORDS_PRIORITY_QUEUE_POOL_H #define LATINIME_WORDS_PRIORITY_QUEUE_POOL_H
#include <assert.h>
#include <new>
#include "words_priority_queue.h" #include "words_priority_queue.h"
namespace latinime { namespace latinime {
@ -24,30 +26,45 @@ namespace latinime {
class WordsPriorityQueuePool { class WordsPriorityQueuePool {
public: public:
WordsPriorityQueuePool(int mainQueueMaxWords, int subQueueMaxWords, int maxWordLength) { WordsPriorityQueuePool(int mainQueueMaxWords, int subQueueMaxWords, int maxWordLength) {
mMasterQueue = new WordsPriorityQueue(mainQueueMaxWords, maxWordLength); mMasterQueue = new(mMasterQueueBuf) WordsPriorityQueue(mainQueueMaxWords, maxWordLength);
mSubQueue1 = new WordsPriorityQueue(subQueueMaxWords, maxWordLength); for (int i = 0, subQueueBufOffset = 0; i < SUB_QUEUE_MAX_COUNT;
mSubQueue2 = new WordsPriorityQueue(subQueueMaxWords, maxWordLength); ++i, subQueueBufOffset += sizeof(WordsPriorityQueue)) {
mSubQueues1[i] = new(mSubQueueBuf1 + subQueueBufOffset)
WordsPriorityQueue(subQueueMaxWords, maxWordLength);
mSubQueues2[i] = new(mSubQueueBuf2 + subQueueBufOffset)
WordsPriorityQueue(subQueueMaxWords, maxWordLength);
}
} }
~WordsPriorityQueuePool() { virtual ~WordsPriorityQueuePool() {
delete mMasterQueue;
} }
WordsPriorityQueue* getMasterQueue() { WordsPriorityQueue* getMasterQueue() {
return mMasterQueue; return mMasterQueue;
} }
// TODO: Come up with more generic pool // TODO: Come up with more generic pool
WordsPriorityQueue* getSubQueue1() { WordsPriorityQueue* getSubQueue1(const int id) {
return mSubQueue1; if (DEBUG_WORDS_PRIORITY_QUEUE) {
assert(id >= 0 && id < SUB_QUEUE_MAX_COUNT);
}
return mSubQueues1[id];
} }
WordsPriorityQueue* getSubQueue2() {
return mSubQueue2; WordsPriorityQueue* getSubQueue2(const int id) {
if (DEBUG_WORDS_PRIORITY_QUEUE) {
assert(id >= 0 && id < SUB_QUEUE_MAX_COUNT);
}
return mSubQueues2[id];
} }
private: private:
WordsPriorityQueue *mMasterQueue; WordsPriorityQueue* mMasterQueue;
WordsPriorityQueue *mSubQueue1; WordsPriorityQueue* mSubQueues1[SUB_QUEUE_MAX_COUNT];
WordsPriorityQueue *mSubQueue2; WordsPriorityQueue* mSubQueues2[SUB_QUEUE_MAX_COUNT];
char mMasterQueueBuf[sizeof(WordsPriorityQueue)];
char mSubQueueBuf1[SUB_QUEUE_MAX_COUNT * sizeof(WordsPriorityQueue)];
char mSubQueueBuf2[SUB_QUEUE_MAX_COUNT * sizeof(WordsPriorityQueue)];
}; };
} }