Cover fe ature 47 january 2012 Published by the ieee computer Society 0018-9162/12/$31. 00 2012 ieee



Download 1,78 Mb.
Pdf ko'rish
bet24/32
Sana13.01.2022
Hajmi1,78 Mb.
#355100
1   ...   20   21   22   23   24   25   26   27   ...   32
Bog'liq
Software-for-infrastructure

void drag_item_to(Vector& v,

                 Vector::iterator source,

                  Coordinate p)

{

   auto dest = find_if(v.begin(), v.end(), contains(p));

    if (source < dest)

        // from before insertion point:

        rotate(source, source+1, dest); 

 

    else

        // from after insertion point:

        rotate(dest, source, source+1);

}

That is, find the insertion point using the standard library 

algorithm 

find_if

 and then move the element to the in-

sertion point using the standard library algorithm 

rotate

Obvious once you see it! 



The original solution was complicated enough to 

raise worries about correctness, plus it was completely 

special-purpose. What might happen to such hard-to- 

understand code during maintenance? The improved code 

is shorter, simpler, more general, and runs faster. It uses 

only well-documented standard library facilities known to 

experienced C++ developers.

But why move only one element? Some user interfaces 

allow selecting and moving a collection of elements. And 

why use application-specific notions such as 



Vector

 and 


Coordinate

? Dealing with this more general problem turns 

out to be simpler still: 

template

pair

gather(Iter first, Iter last, Iter p, Predicate pred)

// move e for which pred(e) to the insertion point p

{

     return make_pair(

        // from before insertion point:

        stable_partition(first, p, !bind(pred, _1)),

        // from after insertion point:

        stable_partition(p, last, bind(pred, _1)) 

      );

}

 

The 



Predicate

 determines which elements are moved. 

Admittedly, 

stable_partition

 tends to be used only 

by specialists, but its meaning isn’t obscure: 

stable_


Download 1,78 Mb.

Do'stlaringiz bilan baham:
1   ...   20   21   22   23   24   25   26   27   ...   32




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish