Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2b61c06

Browse files
authored
Merge pull requestkodecocodes#271 from JaapWijnen/migrate-fixedsizearray-swift3
migrate fixedsizearray to swift3
2 parents5f64dc0 +b15b383 commit2b61c06

File tree

6 files changed

+125
-81
lines changed

6 files changed

+125
-81
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//: Playground - noun: a place where people can play
2+
3+
/*
4+
An unordered array with a maximum size.
5+
6+
Performance is always O(1).
7+
*/
8+
structFixedSizeArray<T>{
9+
privatevarmaxSize:Int
10+
privatevardefaultValue:T
11+
privatevararray:[T]
12+
private(set)varcount=0
13+
14+
init(maxSize:Int, defaultValue:T){
15+
self.maxSize= maxSize
16+
self.defaultValue= defaultValue
17+
self.array=[T](repeating: defaultValue, count: maxSize)
18+
}
19+
20+
subscript(index:Int)->T{
21+
assert(index>=0)
22+
assert(index< count)
23+
returnarray[index]
24+
}
25+
26+
mutatingfunc append(newElement:T){
27+
assert(count< maxSize)
28+
array[count]= newElement
29+
count+=1
30+
}
31+
32+
mutatingfunc removeAtIndex(index:Int)->T{
33+
assert(index>=0)
34+
assert(index< count)
35+
count-=1
36+
letresult=array[index]
37+
array[index]=array[count]
38+
array[count]= defaultValue
39+
return result
40+
}
41+
42+
mutatingfunc removeAll(){
43+
foriin0..<count{
44+
array[i]= defaultValue
45+
}
46+
count=0
47+
}
48+
}
49+
50+
vararray=FixedSizeArray(maxSize:5, defaultValue:0)
51+
array.append(newElement:4)
52+
array.append(newElement:2)
53+
array[1]
54+
array.removeAtIndex(index:0)
55+
array.removeAll()
56+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playgroundversion='5.0'target-platform='ios'>
3+
<timelinefileName='timeline.xctimeline'/>
4+
</playground>

‎Fixed Size Array/FixedSizeArray.playground/playground.xcworkspace/contents.xcworkspacedata‎

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version ="3.0">
4+
<TimelineItems>
5+
<LoggerValueHistoryTimelineItem
6+
documentLocation ="#CharacterRangeLen=5&amp;CharacterRangeLoc=1228&amp;EndingColumnNumber=6&amp;EndingLineNumber=52&amp;StartingColumnNumber=1&amp;StartingLineNumber=52&amp;Timestamp=499271741.074357"
7+
selectedRepresentationIndex ="0"
8+
shouldTrackSuperviewWidth ="NO">
9+
</LoggerValueHistoryTimelineItem>
10+
<LoggerValueHistoryTimelineItem
11+
documentLocation ="#CharacterRangeLen=5&amp;CharacterRangeLoc=1295&amp;EndingColumnNumber=6&amp;EndingLineNumber=54&amp;StartingColumnNumber=1&amp;StartingLineNumber=54&amp;Timestamp=499271760.916283"
12+
selectedRepresentationIndex ="0"
13+
shouldTrackSuperviewWidth ="NO">
14+
</LoggerValueHistoryTimelineItem>
15+
</TimelineItems>
16+
</Timeline>

‎Fixed Size Array/FixedSizeArray.swift‎

Lines changed: 0 additions & 46 deletions
This file was deleted.

‎Fixed Size Array/README.markdown‎

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#Fixed-Size Arrays
22

3-
Early programming languages didn't have very fancy arrays. You'd create the array with a specific size and from that moment on it would never grow or shrink. Even the standard arrays in C and Objective-C are still of this type.
3+
Early programming languages didn't have very fancy arrays. You'd create the array with a specific size and from that moment on it would never grow or shrink. Even the standard arrays in C and Objective-C are still of this type.
44

55
When you define an array like so,
66

77
int myArray[10];
8-
8+
99
the compiler allocates one contiguous block of memory that can hold 40 bytes (assuming an`int` is 4 bytes):
1010

1111
![An array with room for 10 elements](Images/array.png)
@@ -42,7 +42,7 @@ The expensive operations are inserting and deleting. When you insert an element
4242

4343
![Insert requires a memory copy](Images/insert.png)
4444

45-
If your code was using any indexes into the array beyond the insertion point, these indexes are now referring to the wrong objects.
45+
If your code was using any indexes into the array beyond the insertion point, these indexes are now referring to the wrong objects.
4646

4747
Deleting requires a copy the other way around:
4848

@@ -95,38 +95,45 @@ Here is an implementation in Swift:
9595

9696
```swift
9797
structFixedSizeArray<T> {
98-
privatevar maxSize:Int
99-
privatevar defaultValue: T
100-
privatevar array: [T]
101-
private (set)var count=0
102-
103-
init(maxSize:Int,defaultValue: T) {
104-
self.maxSize= maxSize
105-
self.defaultValue= defaultValue
106-
self.array= [T](count: maxSize,repeatedValue: defaultValue)
107-
}
108-
109-
subscript(index:Int)-> T {
110-
assert(index>=0)
111-
assert(index< count)
112-
return array[index]
113-
}
114-
115-
mutatingfuncappend(newElement: T) {
116-
assert(count< maxSize)
117-
array[count]= newElement
118-
count+=1
119-
}
120-
121-
mutatingfuncremoveAtIndex(index:Int)-> T {
122-
assert(index>=0)
123-
assert(index< count)
124-
count-=1
125-
let result= array[index]
126-
array[index]= array[count]
127-
array[count]= defaultValue
128-
return result
129-
}
98+
privatevar maxSize:Int
99+
privatevar defaultValue: T
100+
privatevar array: [T]
101+
private (set)var count=0
102+
103+
init(maxSize:Int,defaultValue: T) {
104+
self.maxSize= maxSize
105+
self.defaultValue= defaultValue
106+
self.array= [T](repeating: defaultValue,count: maxSize)
107+
}
108+
109+
subscript(index:Int)-> T {
110+
assert(index>=0)
111+
assert(index< count)
112+
return array[index]
113+
}
114+
115+
mutatingfuncappend(newElement: T) {
116+
assert(count< maxSize)
117+
array[count]= newElement
118+
count+=1
119+
}
120+
121+
mutatingfuncremoveAtIndex(index:Int)-> T {
122+
assert(index>=0)
123+
assert(index< count)
124+
count-=1
125+
let result= array[index]
126+
array[index]= array[count]
127+
array[count]= defaultValue
128+
return result
129+
}
130+
131+
mutatingfuncremoveAll() {
132+
for iin0..<count {
133+
array[i]= defaultValue
134+
}
135+
count=0
136+
}
130137
}
131138
```
132139

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp