• [^] # Re: normal

    Posté par . En réponse au message Glib et les Binary Trees. Évalué à 1.

    Oui oui ce que tu as dit est juste, mais c'était pas tout à fait ma question en fait :p

    J'ai besoin de traverser l'arbre pour tester des valeurs et supprimer des feuilles en fonctions de ces valeurs.
    Donc oui j'utilise g_tree_remove qui appel les destructeurs listés dans g_tree_new_full, mais je doit faire cela APRES avoir parcouru l'arbre en entier

    ze purpose of my question, c'était de savoir si j'utilisais la bonne méthode ;)

    j'utilise les 3 fonctions suivantes :



    /*! watchman for the B-Tree, wake up every 5 minutes and check every entries */
    void clean_table()
    {
    while (1)
    {
    /*! wake up every 5 minutes */
    sleep(300);

    /*! init pointer table */
    entrytoclean = g_ptr_array_new();

    /*! call the clean function for each value, delete the value if TRUE is returned*/
    g_tree_traverse( redirected_connections,(GHRFunc) match_old_value, G_IN_ORDER, NULL );

    /*! remove each key listed from the btree */
    g_ptr_array_foreach(entrytoclean,(GFunc) remove_old_value, NULL);

    /*! free the array */
    g_ptr_array_free(entrytoclean, TRUE);
    }
    }



    /*! called for each entry in the B-Tree, if a time value is upper to 5 minutes, entry is deleted */
    int match_old_value(gpointer key, gint value, gpointer trash)
    {
    struct tms actual;

    gint *valtime;
    valtime = g_strdup_printf("%d", times(&actual));

    if( (valtime - value) > 30000 )
    {
    g_ptr_array_add(entrytoclean, key);
    }
    return FALSE;
    }



    /*! called for each entry in the pointer array, each entry is a key that is deleted from the B-Tree */
    void remove_old_value(gpointer key, gpointer trash)
    {
    g_print("CLEANER => %s removed\n",key);
    if (TRUE != g_tree_remove(redirected_connections, key))
    {
    g_print("error while removing %s /!\ KEY NOT FOUND IN B-TREE /!\\n", key);
    }
    }