@@ -40,9 +40,24 @@ extension Array where Element: RoutePathProtocol {
4040 self . removeAll ( )
4141 }
4242
43- /// 사이즈 반환
43+ /// 사이즈 계산
4444 public var size : Int {
45- return self . calculateSum ( in: self )
45+ return self . flatten. count
46+ }
47+
48+ /// 복잡한 재귀 배열을 1차원 배열로 사용합니다.
49+ public var flatten : [ Element ] {
50+ get {
51+ var flattenedArray : [ Element ] = [ ]
52+ flattenRecursive ( array: self , resultArray: & flattenedArray)
53+ return flattenedArray
54+ }
55+ set {
56+ var updatedArray : [ Element ] = [ ]
57+ var setArray : [ Element ] = newValue
58+ setRecursive ( array: & updatedArray, setArray: & setArray)
59+ self = updatedArray
60+ }
4661 }
4762}
4863
@@ -80,17 +95,7 @@ extension Array where Element: RoutePathProtocol {
8095 return appendLastPath ( in: & nodes[ nodes. count - 1 ] . stack, element: element)
8196 }
8297 }
83-
84- /// 사이즈 계산
85- private func calculateSum( in nodes: [ Element ] ) -> Int {
86- var sum = nodes. count
87-
88- for node in nodes {
89- sum += calculateSum ( in: node. stack)
90- }
91-
92- return sum
93- }
98+
9499}
95100
96101extension Array where Element: RoutePathProtocol {
@@ -105,3 +110,26 @@ extension Array where Element: RoutePathProtocol {
105110 }
106111 }
107112}
113+
114+ extension Array where Element: RoutePathProtocol {
115+ private func flattenRecursive( array: [ Element ] , resultArray: inout [ Element ] ) {
116+ for element in array {
117+ resultArray. append ( element)
118+ flattenRecursive ( array: element. stack, resultArray: & resultArray)
119+ }
120+ }
121+
122+ private func setRecursive( array: inout [ Element ] , setArray: inout [ Element ] ) {
123+ guard !setArray. isEmpty else { return }
124+
125+ if let first = setArray. first, let index = array. firstIndex ( where: { $0. id == first. id } ) {
126+ array [ index] = first
127+ setArray. removeFirst ( )
128+ setRecursive ( array: & array[ index] . stack, setArray: & setArray)
129+ } else {
130+ for i in 0 ..< array. count {
131+ setRecursive ( array: & array[ i] . stack, setArray: & setArray)
132+ }
133+ }
134+ }
135+ }
0 commit comments