$30 off During Our Annual Pro Sale. View Details »

アルゴリズムを通じて よりよいアプリを

Ray Fix
August 30, 2018

アルゴリズムを通じて よりよいアプリを

One of the "hidden" themes of WWDC 2018 was algorithms. This talk looks at some of the sessions that talked about using algorithms to make better code and shows a toy example of using algorithms using UICollectionView animation.

Ray Fix

August 30, 2018
Tweet

More Decks by Ray Fix

Other Decks in Technology

Transcript

  1. ΞϧΰϦζϜΛ௨ͯ͡
    ΑΓΑ͍ΞϓϦΛ
    iOSDC 2018
    ϨΠɾϑΟοΫε
    @rayfix

    View Slide

  2. View Slide

  3. ΞϧΰϦζϜ͸
    ۀքͷֵ໋Λىͤ͜·͢!
    ϑΝΠϧ
    0 15 30 45 60
    ΦʔσΟΦσʔλ ѹॖ͞ΕͨΦʔσΟΦσʔλ

    View Slide

  4. ؾ͖ͮ·͔ͨ͠ʁ

    View Slide

  5. ALGORITHMS

    View Slide

  6. iOS 12 ϚʔέοτΠϯ
    • ΑΓ଎͘ɺΑΓ൓Ԡྑ͘

    • Ξχϝʔγϣϯ͕ҰஈͱεϜʔζʹ

    • ΞϓϦέʔγϣϯͷىಈ͕࠷େ40%଎͘

    • ΩʔϘʔυͷද͕ࣔ࠷େ50%଎͘

    • εϫΠϓʹΑΔΧϝϥͷىಈ͕࠷େ70%଎͘

    • ෛՙ࣌ͷڞ༗γʔτͷද͕ࣔ࠷େ2ഒ଎͘
    iPhone 5s

    View Slide

  7. O(1) ఆ਺࣌ؒ








    O(log n) ର਺
    O(n) ઢܗؔ਺
    O(n log n) ઢܗର਺
    O(n2) ೋ৐࣌ؒ
    O(nc) ଟ߲ࣜ࣌ؒ
    O(2n) ؒࢦ਺࣌ؒ
    O(n!) ֊৐ؔ਺
    ϥϯμ΢ͷه߸
    εέʔϧ͢Δ
    εέʔϧ͠ͳ͍

    View Slide

  8. O(1)








    O(log n)
    O(n)
    O(n log n)
    O(n2)
    O(nc)
    O(2n)
    O(n!)
    Auto Layout

    Session 202
    iOS 12

    View Slide

  9. Auto Layout
    ϨΞ΢τ੍໿ͷ਺
    ॲཧ࣌ؒ
    O(n)
    iOS 12

    View Slide

  10. Layout “Churn”
    class MyView: UIView {
    override func updateConstraints() {
    }
    }
    ϨΠΞ΢τ੍໿࠶ॳظԽʹ஫ҙ
    ̍ඵʹ̍̎̌ճݺͼग़͞ΕΔՄೳੑ͕͋Δ

    Session 220

    View Slide

  11. ίϨΫγϣϯΛޮՌతʹ
    ࢖༻͢Δํ๏
    • ஗Ԇॲཧ΍εϥΠεͷίετΛཧղ͠ͳ͕Βɺ

    ίϨΫγϣϯ࢖͍·͠ΐ͏

    • Swiftܕ ͱ FoundationܕͷؒʹϒϦοδ͕ඞཁͰ͢ɻ

    ίετ͸O(1) ͔ΒO(n) Ͱ͢ɻ
    let story = “σοΧ͍ string"
    let text = NSMutableAttributedString(string: story)
    let string = text.string
    Session 229

    View Slide

  12. ԕ͗ͨ͢ڮ
    let story = “σοΧ͍ string"
    let text = NSMutableAttributedString(string: story)
    let string = text.string
    as NSString
    for index in 1 ... {
    }
    let nsRange = string.range(of: “kitten")

    n
    InstrumentsͰܭΓ·͠ΐ͏

    View Slide

  13. ΞϧΰϦζϜΛड͚ೖΕΔ
    Crusty
    Dave Abrahams
    @DaveAbrahams

    Swift library designer / Professor of blowing-your-mind @apple
    @Mind your own business
    .
    Session 223

    View Slide

  14. γΣʔϓͷ࡟আ
    extension Canvas {
    mutating func deleteSelection() {
    for i in 0 ..< shapes.count {
    if shapes[i].isSelected {
    shapes.remove(at: i)
    }
    }
    }
    }

    View Slide

  15. γΣʔϓͷ࡟আ
    extension Canvas {
    mutating func deleteSelection() {
    for i in 0 ..< shapes.count {
    if shapes[i].isSelected {
    shapes.remove(at: i)
    }
    }
    }
    }
    TRAP

    View Slide

  16. γΣʔϓͷ࡟আ (V2)
    extension Canvas {
    mutating func deleteSelection() {
    var i = 0
    while i < shapes.count {
    if shapes[i].isSelected {
    shapes.remove(at: i)
    }
    i += 1
    }
    }
    }

    View Slide

  17. γΣʔϓͷ࡟আ (V2)
    extension Canvas {
    mutating func deleteSelection() {
    var i = 0
    while i < shapes.count {
    if shapes[i].isSelected {
    shapes.remove(at: i)
    }
    i += 1
    }
    }
    }
    ͋Εʁ

    View Slide

  18. γΣʔϓͷ࡟আ (V3)
    extension Canvas {
    mutating func deleteSelection() {
    var i = 0
    while i < shapes.count {
    if shapes[i].isSelected {
    shapes.remove(at: i)
    }
    else {
    i += 1
    }
    }
    }
    }

    View Slide

  19. γΣʔϓͷ࡟আ (V4)
    extension Canvas {
    mutating func deleteSelection() {
    for i in (0..if shapes[i].isSelected {
    shapes.remove(at: i)
    }
    }
    }
    }

    ͔͠͠…

    View Slide

  20. γΣʔϓͷ࡟আ (V4)
    extension Canvas {
    mutating func deleteSelection() {
    for i in (0..if shapes[i].isSelected {
    shapes.remove(at: i)
    }
    }
    }
    }
    O(n)
    O(n)
    O(n2) ೋ৐࣌ؒ

    View Slide

  21. Ϗοάɾσʔλ





    View Slide

  22. γΣʔϓͷ࡟আ (V5)
    extension Canvas {
    mutating func deleteSelection() {
    shapes.removeAll { $0.isSelected }
    }
    }
    O(n)

    View Slide

  23. ΩʔϙΠϯτ
    •ඪ४ϥΠϒϥϦͷΞϧΰϦζϜΛ

    ࢖ͬͨ΄͏͕෼͔Γ΍͍͢

    •ඪ४ϥΠϒϥϦͷΞϧΰϦζϜ͸ੑೳͷ

    ༧ଌੑ͕͋Δ

    •O(1) Ҏ֎͸ඞͣυΩϡϝϯτ͞Ε͍ͯΔ

    View Slide

  24. Raw LoopΛ࢖Θͳ͍
    http://sean-parent.stlab.cc/presentations/
    2013-09-11-cpp-seasoning/cpp-seasoning.pdf
    Sean Parent
    @SeanParent

    Software Engineer at Adobe
    C++ Seasoning

    View Slide

  25. Raw LoopΛ࢖Θͳ͍
    •ඪ४ͷΞϧΰϦζϜΛ࢖͏

    •ඪ४ͷΞϧΰϦζϜΛ࣮૷͢Δ

    •શͯͷίʔυ͸ϥΠϒϥϦʔԽ͢Δ

    •৽͍͠ΞϧΰϦζϜΛ։ൃ͢Δ

    •༗໊ʹͳΔ


    View Slide

  26. ׽ࣈ͕೉͍͠
    cards.shuffle()
    let shuffledIndices = cards.indices.shuffled()
    ฤूεςοϓ
    for (new, orig) in shuffledIndices.enumerated() {
    newCards[new] = originalCards[orig]
    }
    collectionView.performBatchUpdates({
    for ( ) in shuffledIndices.enumerated() {
    let src = IndexPath(item: orig, section: 0)
    let dst = IndexPath(item: new, section: 0)
    collectionView.moveItem(at: src, to: dst)
    }
    }, completion: nil)
    new
    orig,
    Session 225
    όά
    ʢnew, orig)
    ͕ਖ਼͍͠

    View Slide

  27. ࠓ೔ͷӳޠڭࣨ

    View Slide

  28. ΤϏϯάϋ΢ε๨٫ۂઢ
    R = e− t
    S

    View Slide

  29. zip
    2
    ( )
    ,
    0
    ( )
    ,
    1
    ( )
    ,
    4
    ( )
    ,
    3
    ( )
    ,

    View Slide

  30. sort
    2
    ( )
    ,
    0
    ( )
    ,
    1
    ( )
    ,
    4
    ( )
    ,
    3
    ( )
    ,

    View Slide

  31. δΣωϦοΫʹ
    extension Collection {
    func sortedWithIndices(by order: (Element, Element) -> Bool) ->
    ([Element], moves: [(from: Index, to: Index)]) {
    let elementsAndIndices = zip(self, self.indices)
    let sorting = elementsAndIndices.sorted { order($0.0, $1.0) }
    let elements = sorting.map { $0.0 }
    let moves = sorting.enumerated().map {
    (offset, element) -> (from: Index, to: Index) in
    var toIndex: Index = startIndex
    formIndex(&toIndex, offsetBy: offset)
    let fromIndex: Index = element.1
    return (from: fromIndex, to: toIndex)
    }.filter { $0.from != $0.to }
    return (elements, moves: moves)
    }
    }

    View Slide

  32. ࠩ෼ϥΠϒϥϦʔ
    • DeepDiff

    • Differ

    • DifferenceKit

    • IGListKit

    • RxDataSources
    DifferenceKit
    Heckel’s Algorithm O(n)

    View Slide

  33. View Slide

  34. Summary
    •ΞϧΰϦζϜΛษڧ͠·͠ΐ͏

    •ඪ४ϥΠϒϥϦʔΛಡΈ·͠ΐ͏

    •Raw Loop Λආ͚·͠ΐ͏

    View Slide

  35. ͝੩ௌ
    ͋Γ͕ͱ͏͍͟͝·ͨ͠!

    View Slide