/** * * @param n_props number of properties (html_tags+property-names) * @param properties array of props and html tag names alternately * @return an initialised matrix */ matrix *matrix_create( int n_props, UChar **properties ) { matrix *m = calloc(1,sizeof(matrix) ); if ( m != NULL ) { m->n_props = n_props/2; m->names = calloc( m->n_props, sizeof(UChar*) ); m->html_tags = calloc( m->n_props, sizeof(UChar*) ); if ( m->names != NULL && m->html_tags != NULL ) { int i,j; for ( j=0,i=0;i<n_props-1;j++,i+=2 ) { m->names[j] = u_strdup(properties[i]); //printf("%s\n",properties[i]); m->html_tags[j] = (properties[i+1]==NULL) ?NULL:u_strdup(properties[i+1]); } m->cells = (int*)calloc( m->n_props*m->n_props, sizeof(int) ); if ( m->cells == NULL ) { matrix_dispose( m ); m = NULL; warning("failed to allocate %dx%d matrix\n",n_props,n_props); } else { m->lookup = hashset_create(); if ( m->lookup == NULL ) { matrix_dispose( m ); m = NULL; } else { int k; for ( k=0;k<m->n_props;k++ ) { hashset_put( m->lookup, m->names[k] ); } } } } else { warning("matrix: failed to allocate names and tags\n"); matrix_dispose( m ); m = NULL; } } return m; }
bool hstr_regexp_match( HstrRegexp *hstrRegexp, const char *regexp, const char *text, regmatch_t *match, char *errorMessage, const size_t errorMessageSize) { regex_t* compiled; if(hashset_contains(&hstrRegexp->cache,regexp)) { compiled=hashset_get(&hstrRegexp->cache, regexp); } else { compiled=malloc(sizeof(regex_t)); int compilationFlags=(hstrRegexp->caseSensitive?0:REG_ICASE); int compilationStatus=regcomp(compiled, regexp, compilationFlags); if(!compilationStatus) { hashset_put(&hstrRegexp->cache, hstr_strdup(regexp), compiled); } else { regerror(compilationStatus, compiled, errorMessage, errorMessageSize); free(compiled); return false; } } int matches=REGEXP_MATCH_BUFFER_SIZE; regmatch_t matchPtr[REGEXP_MATCH_BUFFER_SIZE]; int matchingFlags=0; int matchingStatus=regexec(compiled, text, matches, matchPtr, matchingFlags); if(!matchingStatus) { if(matchPtr[0].rm_so != -1) { match->rm_so=matchPtr[0].rm_so; match->rm_eo=matchPtr[0].rm_eo; return true; } } return false; }
HistoryItems *get_prioritized_history() { using_history(); char *historyFile = get_history_file_name(); if(read_history(historyFile)!=0) { fprintf(stderr, "\nUnable to read history file from '%s'!\n",historyFile); exit(EXIT_FAILURE); } HISTORY_STATE *historyState=history_get_history_state(); if(historyState->length > 0) { HashSet rankmap; hashset_init(&rankmap); HashSet blacklist; int i; hashset_init(&blacklist); for(i=0; i<4; i++) { hashset_add(&blacklist, commandBlacklist[i]); } RadixSorter rs; radixsort_init(&rs, 100000); RankedHistoryItem *r; RadixItem *radixItem; HIST_ENTRY **historyList=history_list(); char **rawHistory=malloc(sizeof(char*) * historyState->length); int rawOffset=historyState->length-1; char *line; for(i=0; i<historyState->length; i++, rawOffset--) { line=historyList[i]->line; rawHistory[rawOffset]=line; if(hashset_contains(&blacklist, line)) { continue; } if((r=hashset_get(&rankmap, line))==NULL) { r=malloc(sizeof(RankedHistoryItem)); r->rank=HISTORY_RANKING_FUNCTION(0, i, strlen(line)); r->item=historyList[i]->line; hashset_put(&rankmap, line, r); radixItem=malloc(sizeof(RadixItem)); radixItem->key=r->rank; radixItem->data=r; radixItem->next=NULL; radixsort_add(&rs, radixItem); } else { radixItem=radix_cut(&rs, r->rank, r); assert(radixItem); if(radixItem) { r->rank=HISTORY_RANKING_FUNCTION(r->rank, i, strlen(line)); radixItem->key=r->rank; radixsort_add(&rs, radixItem); } } } DEBUG_RADIXSORT(); RadixItem **prioritizedRadix=radixsort_dump(&rs); prioritizedHistory=malloc(sizeof(HistoryItems)); prioritizedHistory->count=rs.size; prioritizedHistory->items=malloc(rs.size * sizeof(char*)); prioritizedHistory->raw=rawHistory; for(i=0; i<rs.size; i++) { if(prioritizedRadix[i]->data) { prioritizedHistory->items[i]=((RankedHistoryItem *)(prioritizedRadix[i]->data))->item; } free(prioritizedRadix[i]->data); free(prioritizedRadix[i]); } radixsort_destroy(&rs); return prioritizedHistory; } else { return NULL; } }
int main( int argc, char **argv ) { hashset *hs = hashset_create(); if ( hs != NULL ) { char utmp[32]; hashset_put( hs, str2ustr("banana",utmp,32) ); hashset_put( hs, str2ustr("apple",utmp,32) ); hashset_put( hs, str2ustr("pineapple",utmp,32) ); hashset_put( hs, str2ustr("guava",utmp,32) ); hashset_put( hs, str2ustr("watermelon",utmp,32) ); hashset_put( hs, str2ustr("orange",utmp,32) ); hashset_put( hs, str2ustr("starfruit",utmp,32) ); hashset_put( hs, str2ustr("durian",utmp,32) ); hashset_put( hs, str2ustr("cherry",utmp,32) ); hashset_put( hs, str2ustr("apricot",utmp,32) ); hashset_put( hs, str2ustr("peach",utmp,32) ); hashset_put( hs, str2ustr("pear",utmp,32) ); hashset_put( hs, str2ustr("nectarine",utmp,32) ); hashset_put( hs, str2ustr("plum",utmp,32) ); hashset_put( hs, str2ustr("grape",utmp,32) ); hashset_put( hs, str2ustr("mandarin",utmp,32) ); hashset_put( hs, str2ustr("lemon",utmp,32) ); hashset_put( hs, str2ustr("clementine",utmp,32) ); hashset_put( hs, str2ustr("cumquat",utmp,32) ); hashset_put( hs, str2ustr("custard apple",utmp,32) ); hashset_put( hs, str2ustr("asian pear",utmp,32) ); hashset_put( hs, str2ustr("jakfruit",utmp,32) ); hashset_put( hs, str2ustr("rambutan",utmp,32) ); hashset_put( hs, str2ustr("lime",utmp,32) ); hashset_put( hs, str2ustr("lychee",utmp,32) ); hashset_put( hs, str2ustr("mango",utmp,32) ); hashset_put( hs, str2ustr("mangosteen",utmp,32) ); hashset_put( hs, str2ustr("avocado",utmp,32) ); hashset_put( hs, str2ustr("grandilla",utmp,32) ); hashset_put( hs, str2ustr("grumichama",utmp,32) ); hashset_put( hs, str2ustr("breadfruit",utmp,32) ); // repeats hashset_put( hs, str2ustr("banana",utmp,32) ); hashset_put( hs, str2ustr("apple",utmp,32) ); hashset_put( hs, str2ustr("pineapple",utmp,32) ); hashset_put( hs, str2ustr("guava",utmp,32) ); hashset_put( hs, str2ustr("watermelon",utmp,32) ); hashset_print( hs ); printf("number of elements in set=%d\n",hashset_size(hs)); } }