00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <iostream>
00023 #include <fstream>
00024 #include <stdexcept>
00025 #include <cassert>
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <cstring>
00029 #include <sstream>
00030 #include <string>
00031 #include "math.h"
00032 #include "mempool.h"
00033 #include "htable.h"
00034 #include "lmtable.h"
00035
00036 #include "ngramcache.h"
00037
00038 using namespace std;
00039
00040 void ngramcache::print (const int* ngp)
00041 {
00042 std::cerr << "ngp: size:" << ngsize << "|";
00043 for (int i=0; i<ngsize; i++)
00044 std::cerr << " " << ngp[i];
00045 std::cerr << " |\n";
00046 }
00047
00048 ngramcache::ngramcache(int n,int size,int maxentries,float lf)
00049 {
00050 if (lf<=0.0) lf=NGRAMCACHE_LOAD_FACTOR;
00051 load_factor=lf;
00052 ngsize=n;
00053 infosize=size;
00054 maxn=maxentries;
00055 entries=0;
00056 ht=new htable<int*>((size_t) (maxn/load_factor), ngsize * sizeof(int));
00057 mp=new mempool(ngsize * sizeof(int)+infosize,MP_BLOCK_SIZE);
00058 accesses=0;
00059 hits=0;
00060 };
00061
00062 ngramcache::~ngramcache()
00063 {
00064
00065
00066 delete ht;
00067 delete mp;
00068 };
00069
00070
00071
00072 void ngramcache::reset(int n)
00073 {
00074
00075 delete ht;
00076 delete mp;
00077 if (n>0) maxn=n;
00078 ht=new htable<int*> ((size_t) (maxn/load_factor), ngsize * sizeof(int));
00079 mp=new mempool(ngsize * sizeof(int)+infosize,MP_BLOCK_SIZE);
00080 entries=0;
00081 };
00082
00083 char* ngramcache::get(const int* ngp,char*& info)
00084 {
00085 char* found;
00086
00087 accesses++;
00088 if ((found=(char*) ht->find((int *)ngp))) {
00089 memcpy(&info,found+ngsize*sizeof(int),infosize);
00090 hits++;
00091 }
00092
00093 return found;
00094 };
00095
00096 char* ngramcache::get(const int* ngp,double& info)
00097 {
00098 char *found;
00099
00100 accesses++;
00101 if ((found=(char*) ht->find((int *)ngp))) {
00102 memcpy(&info,found+ngsize*sizeof(int),infosize);
00103 hits++;
00104 };
00105
00106 return found;
00107 };
00108
00109 char* ngramcache::get(const int* ngp,prob_and_state_t& info)
00110 {
00111 char *found;
00112
00113 accesses++;
00114 if ((found=(char*) ht->find((int *)ngp))) {
00115 memcpy(&info,found+ngsize*sizeof(int),infosize);
00116 hits++;
00117 };
00118
00119 return found;
00120 };
00121
00122 int ngramcache::add(const int* ngp,const char*& info)
00123 {
00124 char* entry=mp->allocate();
00125 memcpy(entry,(char*) ngp,sizeof(int) * ngsize);
00126 memcpy(entry + ngsize * sizeof(int),&info,infosize);
00127 char* found=(char*)ht->insert((int *)entry);
00128 assert(found == entry);
00129 entries++;
00130 return 1;
00131 };
00132
00133 int ngramcache::add(const int* ngp,const double& info)
00134 {
00135 char* entry=mp->allocate();
00136 memcpy(entry,(char*) ngp,sizeof(int) * ngsize);
00137 memcpy(entry + ngsize * sizeof(int),&info,infosize);
00138 char *found=(char*) ht->insert((int *)entry);
00139 assert(found == entry);
00140 entries++;
00141 return 1;
00142 };
00143
00144 int ngramcache::add(const int* ngp,const prob_and_state_t& info)
00145 {
00146 char* entry=mp->allocate();
00147 memcpy(entry,(char*) ngp,sizeof(int) * ngsize);
00148 memcpy(entry + ngsize * sizeof(int),&info,infosize);
00149 char *found=(char*) ht->insert((int *)entry);
00150 assert(found == entry);
00151 entries++;
00152 return 1;
00153 };
00154
00155
00156 void ngramcache::stat()
00157 {
00158 cerr << "ngramcache stats: entries=" << entries << " acc=" << accesses << " hits=" << hits
00159 << " ht.used= " << ht->used() << " mp.used= " << mp->used() << " mp.wasted= " << mp->wasted() << "\n";
00160 };
00161
00162