junbjn98

bla blo

Oct 8th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. public class SafeLinkedList<T: Equatable> {
  2.  
  3. // MARK: Linked List's Node Class Declaration
  4. public class Node {
  5. var value:T
  6. var next:Node?
  7. weak var previous:Node?
  8.  
  9. init(_ value: T) {
  10. self.value = value
  11. }
  12.  
  13. deinit {
  14. print("xoa \(String(describing: value)) roi nha")
  15. }
  16. }
  17.  
  18. private var count = 0
  19. private var head: Node?
  20. private var last: Node?
  21.  
  22. // MARK: Constructor
  23. public init() {
  24. }
  25.  
  26. public init(_ value: T) {
  27. add(value)
  28. }
  29.  
  30. public init(_ array: [T]) {
  31. addAll(array)
  32. }
  33.  
  34. public init(_ array: T...) {
  35. addAll(array)
  36. }
  37.  
  38. public init(_ list: SafeLinkedList) {
  39. addAll(list)
  40. }
  41. }
  42.  
  43. // MARK: add
  44. extension SafeLinkedList {
  45.  
  46. public func add(_ value: T) {
  47. let newNode = Node(value)
  48. add(newNode)
  49. }
  50.  
  51. public func add(_ node: Node) {
  52. let newNode = node
  53. if let lastNode = last {
  54. newNode.previous = lastNode
  55. lastNode.next = newNode
  56. } else {
  57. head = newNode
  58. }
  59. last = newNode
  60. count += 1
  61. }
  62.  
  63. public func addFirst(_ value: T) {
  64. let newNode = Node(value)
  65. addFirst(newNode)
  66. }
  67.  
  68. public func addFirst(_ node: Node) {
  69. let newNode = node
  70. if let headNode = head {
  71. headNode.previous = newNode
  72. newNode.next = headNode
  73. }
  74. head = newNode
  75. count += 1
  76. }
  77.  
  78. public func addAll(_ array: [T]) {
  79. array.forEach { add($0) }
  80. }
  81.  
  82. public func addAll(_ array: T...) {
  83. array.forEach { add($0) }
  84. }
  85.  
  86. public func addAll(_ list: SafeLinkedList) {
  87. var nodeToCopy = list.head
  88. while let node = nodeToCopy {
  89. add(node.value)
  90. nodeToCopy = node.next
  91. }
  92. }
  93.  
  94. public func add(_ value: T, atIndex index: Int) {
  95. let newNode = Node(value)
  96. add(newNode, atIndex: index)
  97. }
  98.  
  99. public func add(_ node: Node, atIndex index: Int) {
  100. if index <= 0 {
  101. addFirst(node)
  102. } else if index >= count {
  103. add(node)
  104. } else {
  105. let prev = _node(atIndex: index - 1)!
  106. let next = prev.next
  107. node.previous = prev
  108. node.next = next
  109. next?.previous = node
  110. prev.next = node
  111. }
  112. }
  113. }
  114.  
  115. // MARK: remove
  116. extension SafeLinkedList {
  117.  
  118. public func removeAll() -> Int {
  119. let n = count
  120. head = nil
  121. last = nil
  122. count = 0
  123. return n
  124. }
  125.  
  126. public func remove(_ node: Node) {
  127. if count == 0 {
  128. return
  129. }
  130.  
  131. let prev = node.previous
  132. let next = node.next
  133.  
  134. if let prev = prev {
  135. prev.next = next
  136. } else {
  137. head = next
  138. }
  139. if let next = next {
  140. next.previous = prev
  141. } else {
  142. last = prev
  143. }
  144.  
  145. node.previous = nil
  146. node.next = nil
  147. count -= 1
  148. }
  149.  
  150. public func removeLast() -> Int {
  151. if last == nil {
  152. return 0
  153. }
  154. remove(last!)
  155. return 1
  156. }
  157.  
  158. public func removeFirst() -> Int {
  159. if head == nil {
  160. return 0
  161. }
  162. remove(head!)
  163. return 1
  164. }
  165.  
  166. public func remove(atIndex index: Int) -> Int {
  167. if (index < 0 || index >= count) {
  168. return 0
  169. }
  170. if index == 0 {
  171. return removeFirst()
  172. }
  173. if index == count - 1 {
  174. return removeLast()
  175. }
  176. let node = _node(atIndex: index)
  177. remove(node!)
  178. return 1
  179. }
  180.  
  181. public func remove(_ value: T) -> Int {
  182. var node = head
  183. while let nd = node {
  184. if nd.value == value {
  185. remove(nd)
  186. return 1
  187. }
  188. node = nd.next
  189. }
  190. return 0
  191. }
  192.  
  193. public func remove(_ array: [T]) -> Int {
  194. var c = 0
  195. for v in array {
  196. c += remove(v)
  197. }
  198. return c
  199. }
  200.  
  201. public func remove(_ array: T...) -> Int {
  202. var c = 0
  203. for v in array {
  204. c += remove(v)
  205. }
  206. return c
  207. }
  208.  
  209. public func remove(_ list: SafeLinkedList) -> Int {
  210. var nodeToCopy = list.head
  211. var c = 0
  212. while let node = nodeToCopy {
  213. c += remove(node.value)
  214. nodeToCopy = node.next
  215. }
  216. return c
  217. }
  218. }
  219.  
  220. // MARK: subscript
  221. extension SafeLinkedList {
  222.  
  223. public subscript(index: Int) -> T? {
  224. get {
  225. let node = _node(atIndex: index)
  226. return node?.value
  227. }
  228. set {
  229. let node = _node(atIndex: index)
  230. if let node = node {
  231. node.value = newValue!
  232. }
  233. }
  234. }
  235.  
  236. public func objects(from: Int, to: Int) -> [T] {
  237. var objs = [T]()
  238. var nodeToCopy = head
  239. var i = 0
  240. while let node = nodeToCopy {
  241. if (i > to) {
  242. break
  243. }
  244. if (i >= from && i <= to) {
  245. objs.append(node.value)
  246. }
  247. nodeToCopy = node.next
  248. i += 1
  249. }
  250. return objs
  251. }
  252.  
  253. public subscript(bounds: CountableRange<Int>) -> [T] {
  254. return self.objects(from: bounds.lowerBound, to: bounds.upperBound - 1)
  255. }
  256.  
  257. public subscript(bounds: CountableClosedRange<Int>) -> [T] {
  258. return self.objects(from: bounds.lowerBound, to: bounds.upperBound)
  259. }
  260.  
  261. public subscript(bounds: CountablePartialRangeFrom<Int>) -> [T] {
  262. return self.objects(from: bounds.lowerBound, to: count - 1)
  263. }
  264.  
  265. public subscript(bounds: PartialRangeThrough<Int>) -> [T] {
  266. return self.objects(from: 0, to: bounds.upperBound)
  267. }
  268.  
  269. public subscript(bounds: PartialRangeUpTo<Int>) -> [T] {
  270. return self.objects(from: 0, to: bounds.upperBound - 1)
  271. }
  272. }
  273.  
  274. // MARK: get
  275. extension SafeLinkedList {
  276.  
  277. public func _node(atIndex index: Int) -> Node? {
  278. if index < 0 || index >= count {
  279. return nil
  280. }
  281.  
  282. if index == 0 {
  283. return head!
  284. } else {
  285. var node = head!.next
  286. for _ in 1..<index {
  287. node = node?.next
  288. if node == nil {
  289. break
  290. }
  291. }
  292. return node!
  293. }
  294. }
  295.  
  296. public func firstObject() -> T? {
  297. return head?.value
  298. }
  299.  
  300. public func lastObject() -> T? {
  301. return last?.value
  302. }
  303.  
  304. public func object(atIndex index: Int) -> T? {
  305. return self[index]
  306. }
  307.  
  308. public func object(inBound bounds:CountableRange<Int>) -> [T] {
  309. return self[bounds]
  310. }
  311.  
  312. public func object(inBound bounds:CountableClosedRange<Int>) -> [T] {
  313. return self[bounds]
  314. }
  315.  
  316. public func object(inBound bounds:CountablePartialRangeFrom<Int>) -> [T] {
  317. return self[bounds]
  318. }
  319.  
  320. public func object(inBound bounds:PartialRangeThrough<Int>) -> [T] {
  321. return self[bounds]
  322. }
  323.  
  324. public func object(inBound bounds:PartialRangeUpTo<Int>) -> [T] {
  325. return self[bounds]
  326. }
  327.  
  328. public func size() -> Int {
  329. return count
  330. }
  331. }
  332.  
  333. // MARK: convert
  334. extension SafeLinkedList {
  335.  
  336. public func toArray() -> [T] {
  337. var arr = [T]()
  338. var nodeToCopy = head
  339. while let node = nodeToCopy {
  340. arr.append(node.value)
  341. nodeToCopy = node.next
  342. }
  343. return arr
  344. }
  345.  
  346. public func toReversedArray() -> [T] {
  347. var arr = [T]()
  348. var nodeToCopy = last
  349. while let node = nodeToCopy {
  350. arr.append(node.value)
  351. nodeToCopy = node.previous
  352. }
  353. return arr
  354. }
  355.  
  356. public func clone() -> SafeLinkedList {
  357. return SafeLinkedList<T>(self)
  358. }
  359. }
  360.  
  361. // MARK: Sequence Conformance
  362. extension SafeLinkedList: Sequence {
  363.  
  364. public __consuming func makeIterator() -> Iterator {
  365. return Iterator(node: head)
  366. }
  367.  
  368. public struct Iterator: IteratorProtocol {
  369. private var currentNode: Node?
  370.  
  371. fileprivate init(node: Node?) {
  372. currentNode = node
  373. }
  374.  
  375. public mutating func next() -> T? {
  376. guard let node = currentNode else {
  377. return nil
  378. }
  379. currentNode = node.next
  380. return node.value
  381. }
  382. }
  383. }
  384.  
  385. // MARK: - operator
  386. extension SafeLinkedList {
  387. public static func -(lhs: SafeLinkedList, rhs: SafeLinkedList) -> SafeLinkedList {
  388. var clone = lhs.clone()
  389. clone.remove(rhs)
  390. return clone
  391. }
  392.  
  393. public static func -=(lhs: SafeLinkedList, rhs: SafeLinkedList) -> SafeLinkedList {
  394. lhs.remove(rhs)
  395. return lhs
  396. }
  397.  
  398. public static func -=(lhs: SafeLinkedList, rhs: T) -> SafeLinkedList {
  399. lhs.remove(rhs)
  400. return lhs
  401. }
  402.  
  403. public static func +(lhs: SafeLinkedList, rhs: SafeLinkedList) -> SafeLinkedList {
  404. var clone = lhs.clone()
  405. clone.addAll(rhs)
  406. return clone
  407. }
  408.  
  409. public static func +=(lhs: SafeLinkedList, rhs: SafeLinkedList) -> SafeLinkedList {
  410. lhs.addAll(rhs)
  411. return lhs
  412. }
  413.  
  414. public static func +=(lhs: SafeLinkedList, rhs: T) -> SafeLinkedList {
  415. lhs.add(rhs)
  416. return lhs
  417. }
  418.  
  419. public static func ==(lhs: SafeLinkedList, rhs: SafeLinkedList) -> Bool {
  420. if lhs.size() != rhs.size() {
  421. return false
  422. }
  423. var lhsNode = lhs.head
  424. var rhsNode = rhs.head
  425. while lhsNode != nil {
  426. if lhsNode!.value != rhsNode!.value {
  427. return false
  428. }
  429. lhsNode = lhsNode!.next
  430. rhsNode = rhsNode!.next
  431. }
  432.  
  433. return true
  434. }
  435. }
  436.  
  437.  
  438. // MARK: - Extension to enable the standard conversion of a list to String
  439. extension SafeLinkedList: CustomStringConvertible {
  440. public var description: String {
  441. var s = "["
  442. var node = head
  443. while let nd = node {
  444. s += "\(nd.value)"
  445. node = nd.next
  446. if node != nil { s += ", " }
  447. }
  448. return s + "]"
  449. }
  450. }
  451.  
  452. /*
  453.  
  454. var n: Int = 0
  455. var x: String?
  456. var array: [String]?
  457.  
  458. let a = SafeLinkedList<String>()
  459. let b = SafeLinkedList<String>("1")
  460. let c = SafeLinkedList<String>(["0", "1", "2", "3", "5", "6"])
  461. let d = SafeLinkedList<String>("a", "b", "c", "d", "e", "f")
  462.  
  463. a.add("A")
  464. b.addFirst("0")
  465. c.add("4", atIndex: 4)
  466. c.add("-1", atIndex: -10)
  467. c.add("999", atIndex: 999)
  468. d.addAll(["g", "h"])
  469. d.addAll("i", "j")
  470. a.addAll(b)
  471.  
  472. print("a = \(a)")
  473. print("b = \(b)")
  474. print("c = \(c)")
  475. print("d = \(d)")
  476.  
  477. print("before remove all: a = \(a)")
  478. n = a.removeAll()
  479. print("after remove all: a = \(a)")
  480. print("n = \(n)")
  481.  
  482. a.add("a")
  483. print("before remove last: a = \(a)")
  484. n = a.removeLast()
  485. print("after remove last: a = \(a)")
  486. print("n = \(n)")
  487. a.add("a")
  488. print("before remove first: a = \(a)")
  489. n = a.removeFirst()
  490. print("after remove first: a = \(a)")
  491. print("n = \(n)")
  492.  
  493. print("before remove last: c = \(c)")
  494. n = c.removeLast()
  495. print("after remove last: c = \(c)")
  496. print("n = \(n)")
  497. print("before remove first: c = \(c)")
  498. n = c.removeFirst()
  499. print("after remove first: c = \(c)")
  500. print("n = \(n)")
  501.  
  502. print("before remove atIndex 1: d = \(d)")
  503. n = d.remove(atIndex: 1)
  504. print("after remove atIndex 1: d = \(d)")
  505. print("n = \(n)")
  506. print("before remove atIndex '3': c = \(c)")
  507. n = c.remove("3")
  508. print("after remove atIndex '3': c = \(c)")
  509. print("n = \(n)")
  510.  
  511. print("before remove 'c', 'h', 'j': d = \(d)")
  512. n = d.remove(["c", "h", "j"])
  513. print("after remove remove 'c', 'h', 'j': d = \(d)")
  514. print("n = \(n)")
  515. print("before remove '2', '4': c = \(c)")
  516. n = c.remove("2", "4")
  517. print("after remove '2', '4': c = \(c)")
  518. print("n = \(n)")
  519.  
  520. print("before remove 'a', 'g', 'c': d = \(d)")
  521. let e = SafeLinkedList<String>("a", "g", "c")
  522. n = d.remove(e)
  523. print("after remove remove 'a', 'g', 'c': d = \(d)")
  524. print("n = \(n)")
  525.  
  526. print("c[-69] = \(c[-69])")
  527. print("c[96] = \(c[96])")
  528. x = c[2]
  529. print("c[2] = \(x)")
  530. print("before c = \(c)")
  531. c[2] = "22"
  532. print("after c = \(c)")
  533.  
  534. x = c.firstObject()
  535. print("c first = \(x)")
  536. x = c.lastObject()
  537. print("c last = \(x)")
  538. x = c.object(atIndex: 2)
  539. print("c[2] = \(x)")
  540.  
  541. array = c[1...3]
  542. print("c[1...3] = \(array)")
  543. array = c.object(inBound: 1...3)
  544. print("c[1...3] = \(array)")
  545. n = d.size()
  546. print("d.size() = \(n)")
  547.  
  548. for item in d {
  549. print(item)
  550. }
  551. array = d.toArray()
  552. dump(array)
  553.  
  554. array = d.toReversedArray()
  555. dump(array)
  556.  
  557. print("d contain 'e': \(d.contains("e"))")
  558. print("d contain 'a': \(d.contains("a"))")
  559.  
  560. print("clone")
  561. let f = SafeLinkedList<String>("a", "b", "c", "d", "e", "f")
  562. let g = f.clone()
  563.  
  564. print(f)
  565. print(g)
  566. f.removeAll()
  567. print(f)
  568. print(g)
  569.  
  570. print("compare")
  571. let c1 = SafeLinkedList<String>("a", "b", "c", "d", "e", "f")
  572. let c2 = SafeLinkedList<String>("a", "b", "c", "d", "e", "g")
  573. print("c1 == g : \(c1 == g)")
  574. print("c1 == f : \(c1 == f)")
  575. print("c1 == c2 : \(c1 == c2)")
  576.  
  577. let c3 = c1 + c2
  578. print(c1)
  579. print(c3)
  580. c1 += "xxx"
  581. print(c1)
  582.  
  583. let c4 = c3 - c1
  584. print(c3)
  585. print(c1)
  586. print(c4)
  587.  
  588. let c5 = SafeLinkedList<String>("k", "l", "c", "d", "m", "n")
  589. c3 -= c5
  590. print(c5)
  591. print(c3)
  592.  
  593. c5 -= "k"
  594. print(c5)
  595.  
  596. */
Add Comment
Please, Sign In to add comment