📄 lcmap.c
字号:
if (cm == NULL) HError(15092,"WriteClassMap: Class map is NULL"); WriteMapHeader(f,cm); for (h=0; h<CLMHASHSIZE; h++) { len = 0; for (ce = cm->htab[h]; ce != NULL; ce = ce->next ){ ++len; OutString(f,cm->htkEsc,ce->id); fprintf(f," %d %d %s\n", ce->ndx, ce->size, (ce->inClass)?"IN":"NOTIN"); if (ce->size > 0) { ndxlist = (int *)New(&gstack,sizeof(int)*ce->size); GetClassMembers(cm,ce->ndx, ndxlist); for (i=0; i<ce->size; i++) { OutString(f,cm->htkEsc,WordLMName(ndxlist[i], cm->wmap)); fprintf(f,"\n"); } Dispose(&gstack,ndxlist); } } if (len>maxlen) maxlen = len; avelen += len; } if (debug) { avelen /= CLMHASHSIZE; fprintf(f,"Entries=%d, maxClndx=%d, nfree=%d\n", cm->entries, cm->maxClndx, cm->nfree); fprintf(f,"Max Hash length=%d, Average Hash length=%d\n", maxlen,avelen); }}/* ------------------- Interface Routines ------------------- *//* EXPORT->CreateClassMap: create class map and load fn if non-null */void CreateClassMap(char *fn, ClassMap *c, WordMap *w){ Source src; char buf[MAXSTRLEN],buf2[MAXSTRLEN]; int h,n; /* Initialise configuration values */ strcpy(buf,"CMap:"); if (fn != NULL) strcat(buf,NameOf(fn,buf2)); CreateHeap(&(c->mem),buf,MSTAK,1,0.5,1000,100000); c->entries = 0; c->hdrless = FALSE; c->name = c->lang = NULL; c->maxClndx = 0; c->wmap = w; c->flist = NULL; c->nfree = 0; for (h=0; h<CLMHASHSIZE; h++) c->htab[h] = NULL; if (fn != NULL) { /* read header */ if (InitSource(fn,&src,LCMapFilter)!=SUCCESS) { HError(15010,"CreateClassMap: Cannot open file %s", fn); } ReadMapHeader(&src,c,&n); if (trace&T_CML) printf("Creating class map from %s with %d entries\n", fn, n); } else { if (trace&T_CML) printf("Creating empty class map\n"); } if (fn != NULL) { LoadMapData(&src,c,n); if (trace&T_CML) printf("%d classes loaded\n",c->entries); }}/* EXPORT->SaveClassMap: Write given class map to file fn. */void SaveClassMap(char *fn, ClassMap *c){ FILE *f; Boolean isPipe; f = FOpen(fn,LCMapOFilter,&isPipe); if (f==NULL) HError(15011,"SaveClassMap: Cannot create %s",fn); WriteClassMap(f, c, FALSE); FClose(f,isPipe);}/* EXPORT->ShowClassMap: Write given class map to file fn. */void ShowClassMap(ClassMap *c){ WriteClassMap(stdout,c,TRUE);}/* EXPORT->MakeNewClass: create new class and return index */ClassEntry *MakeNewClass(ClassMap *c, LabId id, int clndx, Boolean inClass){ int h; ClassEntry *ce; ce = (ClassEntry *)id->aux; if (ce != NULL) HError(15092,"MakeNewClass: %s already in map (index=%d)",id->name,ce->ndx); /* determine ndx of new class */ if (clndx < 0) clndx = ++c->maxClndx; else { if (clndx > c->maxClndx) c->maxClndx = clndx; } /* create the class record */ if (c->nfree == 0) /* freelist empty so make new one */ ce = (ClassEntry *)New(&(c->mem),sizeof(ClassEntry)); else { /* reuse deleted class entry */ ce = c->flist; c->flist = ce->next; --c->nfree; } ce->id = id; ce->ndx = clndx; ce->size = 0; ce->inClass = inClass; /* link it to hashtable and the class name */ id->aux = ce; /* NB. This is overwritten in LGCopy.c - beware! */ h = clndx % CLMHASHSIZE; ce->next = c->htab[h]; c->htab[h] = ce; ++c->entries; return ce;}/* EXPORT->DeleteClass: delete the given class */void DeleteClass(ClassMap *c, int clndx){ int h,i,nwords=0; ClassEntry *pce, *ce, *delce; MapEntry *me; if (c->entries == 0) HError(15092,"DeleteClass: Class map %s is empty",c->name); /* find class record */ h = clndx % CLMHASHSIZE; if (c->htab[h]->ndx == clndx) { delce = c->htab[h]; c->htab[h] = delce->next; } else { pce = c->htab[h]; delce = NULL; for (ce = pce->next; ce != NULL; ce = ce->next) { if (ce->ndx == clndx) {delce = ce; break;} pce = ce; } if (delce == NULL) HError(15092,"DeleteClass: Class %d not in map %s",clndx,c->name); pce->next = delce->next; } /* delete it and add to freelist */ ++c->nfree; --c->entries; delce->next = c->flist; c->flist = delce; delce->id->aux = NULL; /* make sure no words reference it */ for (me=c->wmap->me,i=0; i<c->wmap->used; i++,me++) if ((ClassEntry *)me->class == delce) { me->class = NULL; ++nwords;} if (nwords != delce->size) HError(15092,"DeleteClass: Found %d words in class of %d words",nwords,delce->size);}/* EXPORT->AddWordToClass: add given word to specified class */void AddWordToClass(ClassMap *c, int clndx, int wdndx){ LabId id; MapEntry *me; ClassEntry *ce; id = WordLMName(wdndx,c->wmap); if ((me = (MapEntry *)id->aux) == NULL) HError(15090,"AddWordToClass: Word %d has no map entry",wdndx); if ((ce = (ClassEntry *)me->class) != NULL) HError(15090,"AddWordToClass: Word %d already in class %d",wdndx,ce->ndx); ce = GetClassEntry(c,clndx); ++ce->size; me->class = ce;}/* EXPORT->RemWordFromClass: delete given word from class */void RemWordFromClass(ClassMap *c, int clndx, int wdndx){ LabId id; MapEntry *me; ClassEntry *ce; id = WordLMName(wdndx,c->wmap); me = (MapEntry *)id->aux; if (me == NULL) HError(15090,"RemWordFromClass: Word %d has no map entry",wdndx); ce = GetWordClassEntry(c,wdndx); if (ce == NULL) HError(15090,"RemWordFromClass: Word %d has no class",wdndx); if ((ClassEntry *)me->class != ce) HError(15090,"RemWordFromClass: Word %d not in class %d",wdndx,clndx);; if (--ce->size < 0) HError(15092,"RemWordFromClass: Class %d has negative size",clndx); me->class = NULL;}/* EXPORT->WordClass: return class of word, -1 if no class found */int WordClass(ClassMap *c, int wdndx){ ClassEntry *ce; ce = GetWordClassEntry(c,wdndx); if (ce == NULL) return -1; return ce->ndx;}/* EXPORT->ClassSize: return size of class, -1 if class not found */int ClassSize(ClassMap *c, int clndx){ ClassEntry *ce; ce = GetClassEntry(c,clndx); if (ce == NULL) HError(15092,"ClassSize: %d is not a class index",clndx); return ce->size;}/* EXPORT->GetClassMembers: copy class members into words. Indexing is [0..Classsize(c,clndx)-1] */void GetClassMembers(ClassMap *c, int clndx, int *words){ ClassEntry *ce; MapEntry *me; int i,j=0,nfound=0; ce = GetClassEntry(c,clndx); for (me=c->wmap->me,i=0; i<c->wmap->used; i++,me++) if ((ClassEntry *)me->class == ce){ ++nfound; words[j++] = me->ndx; } if (ce->size != nfound) HError(15092,"GetClassMembers: %d words found in class of size %d",nfound,ce->size);}/* EXPORT->IsAnInClass: true if class is a regular 'inClass' set */Boolean IsAnInClass(ClassMap *c, int clndx){ ClassEntry *ce; ce = GetClassEntry(c,clndx); if (ce == NULL) HError(15092,"IsAnInClass: %d is not a class index",clndx); return ce->inClass;}/* EXPORT->IsClassMember: true if word is in class */Boolean IsClassMember(ClassMap *c, int clndx, int wdndx){ ClassEntry *ce; ce = GetWordClassEntry(c,wdndx); if (ce == NULL) HError(15090,"IsClassMember: Word %d has no class",wdndx); return ce->ndx == clndx;}/* ------------------- Low Level Access ---------------- *//* EXPORT->GetClassEntry: find class entry via cache */ClassEntry *GetClassEntry(ClassMap *c, int clndx){ int h; ClassEntry *ce; h = clndx % CLMHASHSIZE; for (ce = c->htab[h]; ce != NULL; ce = ce->next) if (ce->ndx == clndx) return ce; return NULL;}/* EXPORT->GetWordClassEntry: find class entry via word ndx */ClassEntry *GetWordClassEntry(ClassMap *c, int wdndx){ LabId id; MapEntry *me; id = WordLMName(wdndx,c->wmap); me = (MapEntry *)id->aux; if (me == NULL) HError(15090,"GetWordClassEntry: Word %d has no map entry",wdndx); return (ClassEntry *)me->class;}/* -------------------- End of LCMap.c ---------------------- */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -