Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Equatable, Comparable and Hashable

Equatable, Comparable and Hashable

Shigure Shimotori

August 08, 2017
Tweet

More Decks by Shigure Shimotori

Other Decks in Programming

Transcript

  1. iOSDCʹొஃ͠·͢ • #1349ʮARC vs. GCʁARC in GCʁʯ • 5෼ͰGCͷ࿩Λ͠·͢ •

    ݫ͍͠ • Reject͞Εͨ΍ͭ΋ద౰ʹڙཆ͢Δͭ΋Γ • ςετ • ԋࢉࢠ • Ͳ͜Ͱʁ
  2. ͓͞Β͍ • Equatable • ౳͍͔͠Ͳ͏͔ൺֱͰ͖Δ • Comparable • ؔ܎ԋࢉࢠͰൺֱͰ͖Δ •

    ͍͍ͪͪൺֱ༻ͷؔ਺Λ༻ҙ͠ͳͯ͘΋ιʔτͰ͖Δ • Hashable • ϋογϡ஋Λ࡞ΕΔ • Map/DictionaryͷΩʔʹͳΕΔ • Java͸ComparableͷΈinterfaceͰ࢒Γ͸class Objectʹੜ͑ͯΔ • Python͸duck typing
  3. EquatableͱΏ͔͍ͳ஥ؒͨͪ(Ұ෦লུ) &RVBUBCMF $PNQBSBCMF )BTIBCMF /VNFSJD 4FU"MHFCSB 4USJEFBCMF 4USJOH1SPUPDPM 3FGFSFODF$POWFSUJCMF Foundation಺ͷࢀরܕ΁ଓ͘ɻ

    String͕ଓ͘ɻ 1࣍ݩͷ࿈ଓతͳ஋ɻ ਺஋΍UnsafePointer͕ଓ͘ɻ ೋ߲ࢉज़ԋࢉɻ Set͕ଓ͘ɻ • ΋ͬͱ΋ਓؾͳProtocolͷ1ͭ • ͳʹΑΓଘࡏײ͕େ͖͍ɺେ͖͗͢Δ • Equatable.swiftʹ͸ͪΌ͔ͬΓಉҰ൑ఆͷ࣮૷΋͍Δ
  4. protocol Equatableͷཁ݅ • ୅ସՄೳੑ • ൓ࣹੑ • ରশੑ • ਪҠੑ

    • a == b ͕ਅͳΒ not a != b ͕ਅ • ͜ͷ͏ͪEquatable͔ΒϑΥϩʔ͕ೖΔͷ͸࠷ޙ͚ͩʂ • ҙਤతʹΦʔόʔϥΠυ͠ͳ͍ݶΓ==ͷٯͷ݁ՌΛฦ͢
  5. protocol Comparableͷ໾ׂͱཁ݅ • ܕʹࣗવॱং෇͚Λ༩͑Δ • σϑΥϧτͷιʔτํ๏ΛܾΊΔͱ͍͏͜ͱ • Swift͸ؔ܎ԋࢉࢠͰදݱ͍ͯ͠ΔͷͰEquatable͕ඞਢ • ࡶʹ࣮૷͠ͳ͍ݶΓ͸Equatableͱໃ६͠ͳ͍

    • ࡶʹ࣮૷͠ͳ͍ݶΓ͸ < = > ͷͲΕ͔1͚͕ͭͩtrueʹͳΔ • strict total orderingͱݺ͹ΕΔੑ࣭Λຬͨ͞ͳ͚Ε͹ͳΒͳ͍ • ʮ͖ͬͪΓϧʔϧΛܾΊͯશһҰྻʹฒ΂ͳ͍ͱͩΊʯ • ුಈখ਺఺਺ͳͲͰྫ֎͕ೝΊΒΕΔ৔߹͕͋Δ
  6. sort͸2छྨ • Ҿ਺ͳ͠ͷsort / sorted • ࣗવॱং෇͚ʹैͬͯιʔτ͢Δ • ͭ·Γཁૉ͕ComparableͰͳ͚Ε͹ͳΒͳ͍ •

    Ҿ਺ΛऔΔsort / sorted • ର৅ίϨΫγϣϯ಺ͷΈͰ௨༻͢Δॱং෇͚Ͱιʔτ͢Δ • ཁૉ͕ComparableͰͳͯ͘΋͍͍ • strict weak orderingͳॱং෇͚͕ڐ͞ΕΔ • Comparableͱͷҧ͍͸ൺֱෆೳ͕ڐ͞ΕΔ͜ͱ • ͲͪΒ΋࠷ޙ͸ॱং෇͚ʹैͬͯΠϯτϩιʔτ͞ΕΔ