Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 12
  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    4,826

    [c++ stl]funzione sort chiarimenti

    ciao.
    ho la seguente struttura cheserve per fare il sort di un immagine sapendo la larghezza e l'altezza:
    questa il semplice istruzione che fa il sort di un a mappa
    codice:
    for ( fmSort = formatMap.begin(); fmSort != formatMap.end(); ++fmSort)
            std::sort( (*fmSort).second.begin(), (*fmSort).second.end(), Texture2DGreater() );
    codice:
    typedef struct _TEXTURE
    {
        bool operator()(Texture2D const *s1, Texture2D const *s2) const
        {
    
            if (s1->GetHeight()*s1->GetWidth() > s2->GetHeight()*s2->GetWidth())
                return true;
            else if (   (s1->GetHeight() > s2->GetHeight())
                     && (s1->GetHeight()*s1->GetWidth() == s2->GetHeight()*s2->GetWidth()))
                return true;
            else if (   (s1->GetWidth()  > s2->GetWidth())
                     && (s1->GetHeight()*s1->GetWidth() == s2->GetHeight()*s2->GetWidth()))
                return true;
            else 
                return false;
        }
    } Texture2DGreater;
    test:
    codice:
    s1->GetHeight() = 1024
    s2->GetHeight  = 512
    
    s1->GetWidth() = 512;
    s2->GetWidth() = 1024;
    di questa immagine viene fatto il sort tranquillamente

    ma
    codice:
    s1->GetHeight() = 512
    s2->GetHeight  = 1024
    
    s1->GetWidth() = 1024;
    s2->GetWidth() = 512;

    e qui mi ritorna un errore.

    a cosa è dovuto?
    ho visto che ci sono gia texture con le stesse dimensioni e penso che l'errore sia dovuto al fatto che per una texture did stesse deimensioni la struct di comparazione mi ritorna due bool uguali?
    vorrei capire a fondo la situzione e , si anche correggerlo , ma capire bene il perchè.
    grazie.

  2. #2
    La mappa è un container ad ordinamento fissato dal predicato passatole come parametro template (di fatto in genere è un binary tree), che senso ha cercare di ordinarla?
    Amaro C++, il gusto pieno dell'undefined behavior.

  3. #3
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    4,826
    scusa, non è una mappa, è un vector

  4. #4
    Che errore viene generato?
    Amaro C++, il gusto pieno dell'undefined behavior.

  5. #5
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    4,826
    expression : invalid operator <

  6. #6
    Scusa, ma continuo a non capire: che tipo di errore è? Un assert? Un errore di compilazione? Una eccezione?
    Perché nel 90% dei casi problemi di questo genere vengono da un operatore di confronto che non definisce un ordine totale stretto, ma così ad occhio il tuo mi pare giusto...
    Amaro C++, il gusto pieno dell'undefined behavior.

  7. #7
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    4,826
    allora, mi spiego meglio, pensavo fosse un errore piu' semplice :
    il sort deve mettere in ordine le texture in base all'altezza e poi larghezza della texture, ma questo lo capisci meglio di me , percio'
    posto una parte del codice:
    codice:
    typedef std::map <FORMAT, TTexture2DPtrVector >           TNewFormatMap;
    typedef std::vector<Texture2D *>                          TTexture2DPtrVector;//texture2d ha il width e l'height
    
    TNewFormatMap::iterator fmSort;
        for ( fmSort = formatMap.begin(); fmSort != formatMap.end(); ++fmSort)
            std::sort( (*fmSort).second.begin(), (*fmSort).second.end(), Texture2DGreater() );
    valori che generano l'eccezione:

    s1->GetHeight() = 1024
    s2->GetHeight = 512

    s1->GetWidth() = 512;
    s2->GetWidth() = 1024;

    o

    s1->GetHeight() = 128
    s2->GetHeight = 64

    s1->GetWidth() = 128;
    s2->GetWidth() = 256;


    da notare che in base ai dati sopra

    s1->GetHeight()* s1->GetWidth() == s2->GetHeight()* s2->GetWidth()

    puo' essere true per due combinazioni diverse.

    eccezione
    codice:
    typedef struct _TEXTURE2DGREATER 
    {
        bool operator()(Texture2D const *s1, Texture2D const *s2) const
        {
    	int h1 = s1->GetHeight();
    	int h2 = s2->GetHeight();
    	
    	int w1 = s1->GetWidth();
    	int w2 = s2->GetWidth();
    
    		if (s1->GetHeight()* s1->GetWidth() > s2->GetHeight()* s2->GetWidth())
                return true;
            else if (   (s1->GetHeight() > s2->GetHeight())
                     && (s1->GetHeight()* s1->GetWidth() == s2->GetHeight()* s2->GetWidth()))
                return true;//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!errore
            else if (   (s1->GetWidth()  > s2->GetWidth())
                     && (s1->GetHeight()* s1->GetWidth() == s2->GetHeight()* s2->GetWidth()))
                return true;
            else 
                return false;
        }
    } Texture2DGreater;
    codice:
    //codice eccezione
    template<class _Pr, class _Ty1, class _Ty2> inline
    	bool __CLRCALL_OR_CDECL _Debug_lt_pred(_Pr _Pred, _Ty1& _Left, _Ty2& _Right,
    		const wchar_t *_Where, unsigned int _Line)
    	{	// test if _Pred(_Left, _Right) and _Pred is strict weak ordering
    	if (!_Pred(_Left, _Right))
    		return (false);
    	else if (_Pred(_Right, _Left))
    		_DEBUG_ERROR2("invalid operator<", _Where, _Line);////errore!!!!!!!!!!
    	return (true);
    	}


    grazie e scusa per non aver postato tutto.

  8. #8
    L'errore che ti viene segnalato è un check per vedere se il tuo operatore definisce effettivamente uno strict weak order; nello specifico, il test fallito è che se (a < b) è vero, allora (b < a) deve essere falso.

    Secondo me dovresti rendere più rigorosa la scrittura di quell'operatore, in modo da evitare corner case strani: tu in sostanza vuoi ordinare usando come criteri:
    1. la superficie dell'immagine
    2. l'altezza dell'immagine
    3. la larghezza dell'immagine
    (dove si passa al criterio successivo se la proprietà del corrente risulta uguale)

    Per fare questo io farei una cosa del tipo:
    codice:
    struct Texture2DGreater
    {
        bool operator()(Texture2D const *s1, Texture2D const *s2) const
        {
            std::tuple<int, int, int> t1(s1->GetHeight()* s1->GetWidth(), s1->GetHeight(), s1->GetWidth());
            std::tuple<int, int, int> t2(s2->GetHeight()* s2->GetWidth(), s2->GetHeight(), s2->GetWidth());
            return t1<t2;
        }
    };
    Uso le tuple perché hanno uno strict weak ordering già fatto e sicuramente funzionante.

    (tra parentesi, perché, pur essendo C++, usi le struct con il typedef? )
    Amaro C++, il gusto pieno dell'undefined behavior.

  9. #9
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    4,826
    ti ringrazio ma non riesco a trovare la classe template tuple , è nelle tr1?
    cosa devo aggiungere come header o altro?
    ciao

  10. #10
    Amaro C++, il gusto pieno dell'undefined behavior.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.