Git Product home page Git Product logo

cities-ios's Introduction

iOS City List

Installation

pod install

Search Algorithm

Using binary search from swift-algorithm-club.

And apply with array extension to return matching more than one element.

The code and explanation

  • To using binary search: the array must be sorted.
func binarySearchFilter(key: Element) -> [Element] {

    guard let middleIndex = binarySearch(key: key) else { return [] }

    /// Find left side start from middle index
    var lowerBound = middleIndex
    while lowerBound > startIndex {
        if self[lowerBound - 1] != key {
            break
        } else {
            lowerBound -= 1
        }
    }

    /// Find right side start from middle index
    var upperBound = middleIndex
    while upperBound < endIndex - 1 {
        if self[upperBound + 1] != key {
            break
        } else {
            upperBound += 1
        }
    }

    return Array(self[lowerBound...upperBound])
}

After we got the middle index from binarySearch then we need to check element in left and right side until current element doesn't match with the key. Then we can know lower bound and upper bound index that elements matching.

Note

The important thing is how we implement Comparable and Equatable.

static func < (lhs: CityViewModel, rhs: CityViewModel) -> Bool {
    return lhs.name.lowercased() < rhs.name.lowercased()
}

static func == (lhs: CityViewModel, rhs: CityViewModel) -> Bool {
    return lhs.name.lowercased().hasPrefix(rhs.name.lowercased())
}

In search case insensitive, This code convert string to lowercase for comparing element and search text is match with prefix or not. You will see difference result of search String type and CityViewModel type in BinarySearchTests.

String need to match exactly with search text but CityViewModel can match insensitive case with prefix.

cities-ios's People

Contributors

chittapon avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.