int main(void) { std::vector<Complex> v; // Citeste date de test. v = read_data(".date.in"); std::cout << "Vectorul initial" << std::endl; write_vector(v); // Verifica sortarea. std::vector<Complex> sorted = get_sorted(v); std::cout << "Vectorul sortat" << std::endl; write_vector(sorted); // Verifica maparea. std::cout << "Maparea" << std::endl; std::map<Complex, int> mapping = get_mapping(v); for (unsigned int i = 0; i < sorted.size(); i++) { std::cout << sorted[i] << " e pe pozitia " << mapping[sorted[i]] << std::endl; } return 0; }
std::map<Complex, int> get_mapping(const std::vector<Complex> &v) { std::map<Complex, int> res; //TODO Adăugati în map, pentru fiecare element din v, poziția sa în vectorul sortat. std::vector<Complex> sorted = get_sorted(v); std::vector<Complex>::iterator it; for (unsigned int i = 0; i < v.size (); i++) { it = std::find (sorted.begin (), sorted.end (), v[i]); res.insert (std::pair<Complex, int>(v[i], it - sorted.begin ())); } return res; }
/** Compare a WicedFS filesystem against a Host PC directory * * Recursively compares the contents of the WicedFS to a Host PC directory * * @param[in] fs_handle : Filesystem handle obtained from wicedfs_init * @param[in] wdir_name : Directory path in the WicedFS filesystem * @param[in] dir_name : Directory path on the Host PC * * @return 0 = Directories match exactly */ static int test_compare_dir( wiced_filesystem_t* fs_handle, const char* wdir_name, const char* dir_name ) { WDIR wdir; char namebuf[PATH_BUFFER_SIZE]; char ** sorted_ptr_list; unsigned long sorted_count; unsigned long entry_num; /* Open the WicedFS directory */ if ( 0 != wicedfs_opendir( fs_handle, &wdir, wdir_name ) ) { printf( "Error opening wicedfs dir %s\n", wdir_name ); return -1; } printf("opened dir %s at %I64u\n", wdir_name, (uint64_t)wdir.file_table_location ); /* Open the local Host PC directory and get a sorted list of items */ if ( 0 != get_sorted( dir_name, &sorted_ptr_list, &sorted_count )) { printf( "Error sorting disk dir %s\n", dir_name ); wicedfs_closedir( &wdir ); return -2; } /* Loop for each local Host PC item */ entry_num = 0; while ( entry_num < sorted_count ) { FILE* file; char path[PATH_BUFFER_SIZE]; char wpath[PATH_BUFFER_SIZE]; struct stat st; wicedfs_entry_type_t entry_type; /* Ignore "." and ".." */ if ( ( 0 == strcmp( sorted_ptr_list[entry_num], ".." ) ) || ( 0 == strcmp( sorted_ptr_list[entry_num], "." ) ) ) { entry_num++; continue; } /* Start constructing the full host path of this item - Copy the base directory */ strcpy( path, dir_name ); /* If the base directory does not have a separator, add one */ if ( dir_name[ strlen(dir_name) - 1 ] != DIRECTORY_SEPARATOR_CHR ) { strcat( path, DIRECTORY_SEPARATOR_STR ); } /* Add the item name to complete the path */ strcat( path, sorted_ptr_list[entry_num] ); /* Start constructing the full WicedFS path of this item - Copy the base directory */ strcpy( wpath, wdir_name ); /* If the base directory does not have a separator, add one */ if ( wdir_name[ strlen(wdir_name) - 1 ] != DIRECTORY_SEPARATOR_CHR ) { strcat( wpath, DIRECTORY_SEPARATOR_STR ); } /* Add the item name to complete the path */ strcat( wpath, sorted_ptr_list[entry_num] ); /* Check if the host PC item is a directory */ if ( ( stat( path, &st ) == 0) && ( S_ISDIR(st.st_mode ) ) ) { /* Item is a sub-directory - Recurse into it. */ int retval; printf( "recursing to %s , %s\n", wpath, path ); retval = test_compare_dir( fs_handle, wpath, path ); if ( retval != 0 ) { free_list( &sorted_ptr_list, sorted_count ); wicedfs_closedir( &wdir ); return retval; } } /* Read the next item name from the WicedFS directory*/ if ( 0 != wicedfs_readdir( &wdir, namebuf, sizeof(namebuf), &entry_type ) ) { printf( "Error reading wicedfs directory\n" ); free_list( &sorted_ptr_list, sorted_count ); wicedfs_closedir( &wdir ); return -3; } /* Compare the item name against the one from the Host PC directory */ if ( 0 != strcmp( namebuf, sorted_ptr_list[entry_num] ) ) { printf( "Error names do not match: wicedfs \"%s\", disk \"%s\"\n", namebuf, sorted_ptr_list[entry_num] ); free_list( &sorted_ptr_list, sorted_count ); wicedfs_closedir( &wdir ); return -4; } /* Check if the item is a file */ if ( (stat(path, &st) == 0) && (! S_ISDIR(st.st_mode) ) ) { /* Item is a file - compare the content */ size_t num_read; WFILE bfile; /* Open the Host PC file for reading */ if ( NULL == ( file = fopen ( path, "rb" ) ) ) { printf( "Error opening disk file %s , %s\n", path, strerror( errno ) ); free_list( &sorted_ptr_list, sorted_count ); wicedfs_closedir( &wdir ); return -5; } /* Open the WicedFS for reading */ if ( 0 != wicedfs_fopen( fs_handle, &bfile, wpath ) ) { printf( "Error opening wicedfs file %s\n", wpath ); free_list( &sorted_ptr_list, sorted_count ); wicedfs_closedir( &wdir ); return -5; } /* Read chunks from Host PC file until end-of-file reached */ while ( ( num_read = fread( buffer1, 1, sizeof(buffer1), file ) ) != 0 ) { size_t bnum_read; /* Read a chunk from the WicedFS file */ bnum_read = (size_t) wicedfs_fread( buffer2, (wicedfs_usize_t) 1, (wicedfs_usize_t) sizeof(buffer2), &bfile ); /* Check that the number of bytes read matches */ if ( bnum_read != num_read ) { printf( "Error: num read mismatch %d %d\n", num_read, bnum_read ); free_list( &sorted_ptr_list, sorted_count ); wicedfs_closedir( &wdir ); return -6; } /* Check that the data matches */ if ( 0 != memcmp( buffer1, buffer2, num_read ) ) { printf( "Error data mismatch in file %s\n", wpath ); free_list( &sorted_ptr_list, sorted_count ); wicedfs_closedir( &wdir ); return -7; } } /* Close both files */ wicedfs_fclose( &bfile ); fclose( file ); } printf( "verified %s\n", namebuf ); entry_num++; } /* Cleanup */ free_list( &sorted_ptr_list, sorted_count ); wicedfs_closedir( &wdir ); return 0; }