effizienter algo



  • es geht um folgenden fall:
    ich habe einen path zb.: /Foo_0_0/FOO_0_1/FOO_0_2/FOO_0_3
    jeder teilpath ist ein knoten!
    
    dir_depth = calcdepth("/Foo_0_0/FOO_0_1/FOO_0_2/FOO_0_3"); -> 5
    
    while(1)
    {
       tmp = search_node("/Foo_0_0/FOO_0_1/FOO_0_2/FOO_0_3");
       // beim zweiten mal: suche: tmp = search_node("/Foo_0_0/FOO_0_1/FOO_0_2");
       // drittes mal: tmp = search_node("/Foo_0_0/FOO_0_1");
       // viertes mal: tmp = search_node("/Foo_0_0");
       // fuenftes mal: tmp = search_node("/");
       subpath_count++;
       if(tmp != NULL || subpath_count == dir_depth)
          break; -> abbruch via: subpath_count == dir_depth
    }
    
    while(subpath_count < dir_depth)
    {
    	fuege die knoten nacheinander hinzu:
    	also zuerst ("/"); dann ("/Foo_0_0"); dann ("/Foo_0_0/FOO_0_1"); usw...
    	add_node(fullpath, subpath);
    	zb.: add_node("/Foo_0_0/FOO_0_1", "FOO_0_1");
    }
    

    ich habs versucht die teilpath in ein 2 dim array zu speichern, aber das ist nicht effizient genug...jemand einen anderen ansatz, der ev. schneller ist?



  • uint32_t dir_depth_start = 2;
    	uint32_t dir_depth_count = 0;
    
    	strcpy( dir_copy, "/FOO_0_0/FOO_0_1/FOO_0_2/FOO_0_3" );
    
    	str_tmp = strchr(dir_copy, '/');
    	char_old = dir_copy[str_tmp-dir_copy+1];
    	dir_copy[str_tmp-dir_copy+1] = '\0';
    	if(dir_depth_start_count++ >= dir_depth_start)
    		printf ("%s\n",dir_copy);
    	dir_copy[str_tmp-dir_copy+1] = char_old;
    	while(str_tmp != NULL)
    	{
    		str_tmp = strchr(str_tmp+1, '/');
    		if(str_tmp != NULL) {
    			dir_copy[str_tmp-dir_copy] = '\0';
    			if(dir_depth_start_count++ >= dir_depth_start)
    				printf ("%s\n",dir_copy);
    			dir_copy[str_tmp-dir_copy] = '/';
    		}
    		else {
    			if(dir_depth_start_count++ >= dir_depth_start)
    				printf ("%s\n",dir_copy);
    		}
    	}
    
    output:
    /FOO_0_0/FOO_0_1
    /FOO_0_0/FOO_0_1/FOO_0_2
    /FOO_0_0/FOO_0_1/FOO_0_2/FOO_0_3
    

    wie kann ich den algo in 1 schleife packen?



  • #include <iostream>
    using namespace std;
    
    void printSubDirs(char const* path,int depthStart){
        char pathCopy[1000];
        int depth=0;
        for(int i=0;;++i){
            char ch=path[i];
            if(ch=='/' || ch==0){
                ++depth;
                if(depth>depthStart){
                    pathCopy[i]='\0';
                    cout<<pathCopy<<'\n';
                }
                if(ch=='\0')
                    break;
            }
            pathCopy[i]=ch;
        }
    }
    
    int main(){
        printSubDirs("/FOO_0_0/FOO_0_1/FOO_0_2/FOO_0_3",2);
    }
    

Anmelden zum Antworten