forked from gl-lei/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSet.swift
More file actions
108 lines (93 loc) · 2.47 KB
/
Set.swift
File metadata and controls
108 lines (93 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// Set.swift
// Array
//
// Created by ggl on 2019/3/20.
// Copyright © 2019 ggl. All rights reserved.
// 动态扩容的数组(无序)
import Foundation
class DynamicExpansionSet {
/// 底层存储数组
var array: UnsafeMutablePointer<Int>
/// 元素的个数
var size: Int
/// 数组的大小
var capcity: Int
init(capcity: Int) {
self.capcity = capcity
array = UnsafeMutablePointer<Int>.allocate(capacity: capcity)
array.initialize(repeating: Int.min, count: capcity)
size = 0
}
/// 增加元素
///
/// - Parameter num: 要增加的数字
func add(num: Int) {
if size >= capcity {
let tempArray = array
array = UnsafeMutablePointer<Int>.allocate(capacity: capcity * 2)
array.initialize(repeating: Int.min, count: capcity * 2)
array.assign(from: tempArray, count: size)
tempArray.deallocate()
capcity *= 2
}
for i in 0..<capcity {
if array[i] == Int.min {
array[i] = num
size += 1
break
}
}
}
/// 删除元素
///
/// - Parameter num: 删除数字
/// - Returns: 是否成功
@discardableResult
func remove(num: Int) -> Bool {
if num == Int.min {
return false
}
var count = 0
for i in 0..<capcity {
guard count < size else {
return false
}
let element = array[i]
if element == Int.min {
continue
}
if num == element {
array[i] = Int.min
size -= 1
return true
}
count += 1
}
return false
}
/// 打印元素
func print() {
Swift.print("[", terminator: "")
var count = 0
for i in 0..<capcity {
guard count < size else {
return
}
let num = array[i]
if num != Int.min {
if count == size - 1 {
Swift.print(num, terminator: "]\n")
} else {
Swift.print(num, terminator: " ")
}
count += 1
} else {
continue
}
}
}
deinit {
array.deallocate()
}
}