Is there a specific way to use tuples as given elements in Swift?

I want to perform installation operations on the coordinates of paired elements from the xy grid. For example. {(0,0), (1,4), (1,5), (2,3)} union with {(2,3), (1,4), (2,6)} = {(0 , 0), (1,4), (1,5), (2,3), (2,6)}, unfortunately, I cannot develop a way to insert tuples into Swift Set commands, since it says that they do not conform to the hashable protocol. I think I have a job, but it has a lot of code. Is there a simple way that I am missing before I hit the grindstone?

I'm completely new to the iOS framework, so I'm currently trying to do everything in Swift. This may be my problem ...

+5
source share
3 answers

Instead of using tuples to represent points, use the built-in type CGPoint. You can expand CGPointto be hashed by expanding it:

import UIKit

extension CGPoint: Hashable {
    public var hashValue: Int {
        return self.x.hashValue << sizeof(CGFloat) ^ self.y.hashValue
    }
}

// Hashable requires Equatable, so define the equality function for CGPoints.
public func ==(lhs: CGPoint, rhs: CGPoint) -> Bool {
    return CGPointEqualToPoint(lhs, rhs)
}

Now that CGPoint is Hashable, you can use it in sets. For example:

let point1 = CGPoint(x: 0, y: 1)
let point2 = CGPoint(x: 0, y: 2)
let point3 = CGPoint(x: 1, y: 1)
let point4 = CGPoint(x: 3, y: 3)
let point5 = CGPoint(x: 3, y: 3)  // Intentionally the same as point4 to see the effect in union and difference.

let set1 = Set([point1, point2 , point5])
let set2 = Set([point4, point3])

let union = set1.union(set2) // -> {{x 0 y 2}, {x 3 y 3}, {x 0 y 1}, {x 1 y 1}}
let difference = set1.intersect(set2) // -> {{x 3 y 3}}
+3
source

Here you go:

class Pair {
    var x: Int
    var y: Int
    init(x: Int, y:Int){
        self.x = x
        self.y = y
    }

    func isExisted(inPairs pairs:[Pair]) -> Bool {
        for p in pairs {
            if p.y == self.y && p.x == self.x{
                return true
            }
        }
        return false
    }

    static func union (pairs1: [Pair], pairs2: [Pair]) -> [Pair] {
        var pairsFinal = [Pair]()

        for p in pairs1 {
            pairsFinal.append(p)
        }

        for p in pairs2 {
            if !p.isExisted(inPairs: pairsFinal){
                pairsFinal.append(p)
            }
        }
        return pairsFinal
    }
}

let pari1 = Pair(x: 4, y: 7)
let pair2 = Pair(x: 5, y: 2)
let pair3 = Pair(x: 4, y: 7)
let pair4  = Pair(x: 3, y: 9)

let pairs1 = [pari1, pair2]
let pairs2 = [pair3, pair4]

let f = Pair.union(pairs1, pairs2: pairs2)

And this is the result of the union:

enter image description here

0
source

You can do structas type Hashable:

struct Point: Hashable {
  let x: Int
  let y: Int
}

Now that you have the hashed tuple, you can use the usual operations Set:

let set1 = Set([
  Point(x:0,y:0),
  Point(x:1,y:4),
  Point(x:1,y:5),
  Point(x:2,y:3)
]) 

let set2 = Set([
  Point(x:2,y:3),
  Point(x:1,y:4),
  Point(x:2,y:6)
])

let setUnion = set1.union(set2)

/*
setUnion = {
  Point(x: 1, y: 5), 
  Point(x: 0, y: 0), 
  Point(x: 1, y: 4), 
  Point(x: 2, y: 3), 
  Point(x: 2, y: 6)
}
*/
0
source

Source: https://habr.com/ru/post/1610554/


All Articles