/* * tldlist_add adds the TLD contained in `hostname' to the tldlist if * `d' falls in the begin and end dates associated with the list; * returns 1 if the entry was counted, 0 if not */ int tldlist_add(TLDList *tld, char *hostname, Date *d) { // check if it's within the tld dates if (date_compare(tld->begin, d) > 0 ||date_compare(tld->end, d) < 0) return 0; int i , counter=0; int len = strlen(hostname); for(i=len;i>0;i--){ if(hostname[i] == '.'){ counter=i; break; } } char *tempTLDnode = hostname+counter+1; int hostlen = strlen(tempTLDnode); char *tldstr = (char *)malloc((hostlen + 1)); tldstr[hostlen] = '\0'; // make sure there is a null end for(i=0;i<hostlen;i++){ tldstr[i] = tempTLDnode[i]; } tld->root = addnode(tld, tldstr, tld->root); tld->count++; return 1; }
int tldlist_add(TLDList *tld, char *hostname, Date *d){ //create node with 'hostname' to the tldlist when 'd' falls in the begin and end date if ((date_compare(tld->beg, d)<=0)&&(date_compare(tld->end, d)>=0)){ //char *tldToken=strtok(strrchr(hostname, '.'), "."); int i; int len = strlen(hostname); i = len; int dotCount; while(i > 0){ if(hostname[i] == '.'){ dotCount = i; break; } i--; } char *tempChar = hostname+dotCount+1; int charLen = strlen(tempChar); char *tldname=(char *)malloc(charLen+1); if(tldname == NULL){ free(tldname); return NULL; } tldname[charLen]='\0'; for(i=0;i<charLen;i++){ tldname[i] = tempChar[i]; } tld->root =insert_node(tld, tld->root, tldname); tld->numOfNodes++; return 1; } return 0; }
/* * tldlist_add adds the TLD contained in `hostname' to the tldlist if * `d' falls in the begin and end dates associated with the list; * returns 1 if the entry was counted, 0 if not */ int tldlist_add(TLDList *tld, char *hostname, Date *d) { long i; // check if the date is whithin the limit if (date_compare(d,tld->begin) >= 0 && date_compare(tld->end, d) >= 0) { #ifdef DEBUG printf("[ADD] Valid hostname, will be added\n"); #endif // extract the last dot location for (i=strlen(hostname); i>0 && hostname[i] != '.'; i--); // check if it is the first element of the tree if (tld->root == NULL) { // create a new node and set it as the first root tld->root = tldnode_new(hostname+i+1); } else { tldnode_add(tld, hostname+i+1, tld->root); } tld->host_count++; return 1; } #ifdef DEBUG printf("[ADD] Invalid host, date outside the limit\n"); #endif return 0; }
list getBetweenDate(struct tm start, struct tm end, struct error* err) { err->error = NO_ERROR; if(!db) { if(!initializeConnection(NULL)) { exit(1); } } sqlite3_stmt* statement; const char* sql = "select * from tasks"; const char* unused; int rc = sqlite3_prepare(db, sql, -1, &statement, &unused); if(rc != SQLITE_OK) { err->error = DATABASE_SELECT; err->description = sqlite3_errmsg(db); return NULL; } list l = l_new(); while(sqlite3_step(statement) == SQLITE_ROW) { struct tm s; struct tm e; strptime((char*)sqlite3_column_text(statement, 2), "%a %b %d %H:%M:%S %Y%n", &s); strptime((char*)sqlite3_column_text(statement, 3), "%a %b %d %H:%M:%S %Y%n", &e); if(date_compare(start, s) > 0 || date_compare(end, e) < 0) { continue ; } char* title; char* description; int title_length = strlen((char*)sqlite3_column_text(statement, 0)) + 1; int descr_length = strlen((char*)sqlite3_column_text(statement, 1)) + 1; title = (char*)malloc(title_length*sizeof(char)); description = (char*)malloc(descr_length*sizeof(char)); strcpy(title,(char*)sqlite3_column_text(statement, 0)); strcpy(description, (char*)sqlite3_column_text(statement, 1)); Task t = (Task)malloc(sizeof(struct task)); t->title = title; t->description = description; t->startDay = s; t->dueDay = e; l_push(l, t); } sqlite3_finalize(statement); return l; }
int main(){ Date* date_year1 = date_create("11/10/1991"); Date* date_year2 = date_create("11/10/1992"); Date* date_month1 = date_create("11/10/1991"); Date* date_month2 = date_create("11/11/1991"); Date* date_day1 = date_create("11/10/1991"); Date* date_day2 = date_create("12/10/1991"); Date* date_same1 = date_create("10/10/1991"); Date* date_same2 = date_create("10/10/1991"); int comp1; int comp2; int comp3; int comp4; int comp5; int comp6; int comp7; int comp8; comp1 = date_compare(date_year1, date_year2); comp2 = date_compare(date_month1, date_month2); comp3 = date_compare(date_day1, date_day2); comp4 = date_compare(date_same1, date_same2); comp5 = date_compare(date_month2, date_month1); comp6 = date_compare(date_year2, date_year1); comp7 = date_compare(date_day2, date_day1); comp8 = date_compare(date_same2, date_same1); printf("%i, %i, %i, %i, %i, %i, %i, %i", comp1, comp2, comp3, comp4, comp5, comp6, comp7, comp8); //printf("%i\n", all_chars(&validate_slash, 4, 8, '/', '/', '/')); return 0; }
int tldlist_add(TLDList *tld, char *hostname, Date *d) { if (date_compare(tld->begin, d) > 0 || date_compare(tld->end, d) < 0 ) return 0; char *tldStr, *cur; cur = hostname; while ( *cur != '\0') { if ( *cur == '.') { tldStr = cur; } cur++; } tldStr++; char *tldValue = (char *)malloc(sizeof(char)*4); strcpy(tldValue, tldStr); if (isEmpty(tld)) { tld->root = tldnode_create(NULL, tldValue, 1); if (tld->root != NULL) { tld->size++; tld->totalTLDs++; return 1; } else return 0; } else { int result = tldlist_insert(tldValue, tld->root, tld); if ( result != 0 ) { if (result != 2) // if tld already exists, you don't need to increment the size of the tree. tld->size++; tld->totalTLDs++; return 1; } else return 0; } return 0; }
tDDAPRecord *db_search_birth_date( tDateTime birthDate ) { uint32_t i; for ( i = 0; i < g_recordCount; i++ ) { if ( date_compare( g_ddapDatabase[i].birthDate, birthDate ) == 0 ) return (&g_ddapDatabase[i]); } return (NULL); }
void do_date_search( eParserSymbol comparison_symbol, tDateTime dateCompare ) { uint32_t index; uint32_t record_total = db_get_record_count(); tItemStack results; results.type = ITEM_TYPE_RESULT; results.result_list_size = 0; for ( index = 0; index < record_total; index++ ) { tDDAPRecord *pCur = db_search_index( index ); int32_t comparison_result = date_compare( pCur->birthDate, dateCompare ); int32_t add_item = 0; switch ( comparison_symbol ) { case SYM_LESS_THAN: if ( comparison_result == -1 ) add_item = 1; break; case SYM_GREATER_THAN: if ( comparison_result == 1 ) add_item = 1; break; case SYM_EQUAL: if ( comparison_result == 0 ) add_item = 1; break; case SYM_NOT_EQUAL: if ( comparison_result != 0 ) add_item = 1; break; default: THROW( PARSER_EXCEPTION_INVALID_TOKEN ); break; } // Add the result to the result list if ( add_item ) results.data.result_list[results.result_list_size++] = index; } // Add results push_parser_stack( results ); }
// THIS METHOD IS OF TIME COMPLEXITY O(ALen + BLen) . Hence this is preferred struct transaction * sortedArraysCommonElements(struct transaction *A, int ALen, struct transaction *B, int BLen) { int i = 0, j = 0, date, len = 0; struct transaction *result = NULL; if (A == NULL || B == NULL || ALen == NULL || BLen == NULL) return NULL; while (i < ALen && j < BLen) { date = date_compare(A[i].date, B[j].date); if (date == 1) j++; else if (date == 2) i++; // If transactions are high, (> 1000), we can increment i, j in steps of powers of 2. // Here, this is enough as there are less transactions else { if (A[i].amount == B[j].amount && stringcompare(A[i].description, B[j].description)) { len++; result = (struct transaction*)realloc(result, len*sizeof(struct transaction)); result[len - 1].amount = A[i].amount; string_copy(result[len - 1].date, A[i].date); string_copy(result[len - 1].description, A[i].description); } // For the given testcases and question , this else is not needed.(special case) else { len++; result = (struct transaction*)realloc(result, len*sizeof(struct transaction)); result[len - 1].amount = A[i].amount; string_copy(result[len - 1].date, A[i].date); string_copy(result[len - 1].description, A[i].description); len++; result = (struct transaction*)realloc(result, len*sizeof(struct transaction)); result[len - 1].amount = B[j].amount; string_copy(result[len - 1].date, B[j].date); string_copy(result[len - 1].description, B[j].description); } i++; j++; } } return result; }