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

Commit0b05b0c

Browse files
saulKevinRansom
authored andcommitted
[FS-1041] Add IReadOnlyCollection/List/Dictionary implementations to FSharp.Core collections (dotnet#4014)
* Add IReadOnlyCollection/List/Dictionary implementations to FSharp.Core collections* Add tests, remove FSCORE_PORTABLE_OLD as it's not used anywhere
1 parentdc0bf5e commit0b05b0c

File tree

13 files changed

+87
-67
lines changed

13 files changed

+87
-67
lines changed

‎src/assemblyinfo/assemblyinfo.FSharp.Core.dll.fs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ open System.Runtime.InteropServices
99
[<assembly:AssemblyTitle("FSharp.Core.dll")>]
1010
[<assembly:AssemblyCopyright("\169 Microsoft Corporation. All Rights Reserved.")>]
1111
[<assembly:AssemblyProduct("Microsoft\174 F#")>]
12-
#if!FSCORE_PORTABLE_OLD
1312
[<assembly:ComVisible(false)>]
14-
#endif
1513

1614
#if PORTABLE
1715
[<assembly:AssemblyProduct("Microsoft\174 F#")>]

‎src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/ListType.fs‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ type ListType() =
8383
Assert.AreEqual(false,enum.MoveNext())
8484
CheckThrowsInvalidOperationExn(fun()->enum.Current|> ignore)
8585

86-
#if!FSCORE_PORTABLE_OLD
8786
[<Test>]
8887
memberthis.IReadOnlyCollection_T()=
8988

@@ -100,7 +99,21 @@ type ListType() =
10099
letc=[]:> IReadOnlyCollection<int>
101100

102101
Assert.AreEqual(c.Count,0)
103-
#endif
102+
103+
[<Test>]
104+
memberthis.IReadOnlyList_T()=
105+
106+
letc=['a';'b';'c']:> IReadOnlyList<char>
107+
108+
Assert.AreEqual(c.[1],'b')
109+
110+
letc=[1..10]:> IReadOnlyList<int>
111+
112+
Assert.AreEqual(c.[5],6)
113+
114+
letc=[]:> IReadOnlyList<int>
115+
116+
CheckThrowsArgumentException(fun()-> c.[0]|> ignore)
104117

105118
// Base class methods
106119
[<Test>]

‎src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/MapType.fs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,27 @@ type MapType() =
111111
CheckThrowsKeyNotFoundException(fun()-> id.[1]|> ignore)
112112
Assert.AreEqual(id.Keys,[||])
113113
Assert.AreEqual(id.Values,[||])
114+
115+
[<Test>]
116+
memberthis.IReadOnlyDictionary()=
117+
letirod=(Map.ofArray[|(1,1);(2,4);(3,9)|]):> IReadOnlyDictionary<_,_>
118+
119+
Assert.IsTrue(irod.ContainsKey(1))
120+
Assert.IsFalse(irod.ContainsKey(5))
121+
Assert.AreEqual(irod.[1],1)
122+
Assert.AreEqual(irod.[3],9)
123+
Assert.AreEqual(irod.Keys,[|1;2;3|])
124+
Assert.AreEqual(irod.Values,[|1;4;9|])
125+
126+
Assert.IsTrue(irod.TryGetValue(1, ref1))
127+
Assert.IsFalse(irod.TryGetValue(100, ref1))
128+
129+
// Empty IROD
130+
letirod= Map.empty:> IReadOnlyDictionary<int, int>// Note no type args
131+
Assert.IsFalse(irod.ContainsKey(5))
132+
CheckThrowsKeyNotFoundException(fun()-> irod.[1]|> ignore)
133+
Assert.AreEqual(irod.Keys,[||])
134+
Assert.AreEqual(irod.Values,[||])
114135

115136
[<Test>]
116137
memberthis.ICollection()=
@@ -136,6 +157,18 @@ type MapType() =
136157
letnewArr= Array.create5(new KeyValuePair<int,int>(0,0))
137158
ic.CopyTo(newArr,0)
138159

160+
[<Test>]
161+
memberthis.IReadOnlyCollection()=
162+
// Legit IROC
163+
letiroc=(Map.ofArray[|(1,1);(2,4);(3,9)|]):> IReadOnlyCollection<KeyValuePair<_,_>>
164+
165+
Assert.AreEqual(iroc.Count,3)
166+
167+
// Empty IROC
168+
letiroc= Map.empty:> IReadOnlyCollection<KeyValuePair<int, int>>
169+
170+
Assert.AreEqual(iroc.Count,0)
171+
139172
[<Test>]
140173
memberthis.IComparable()=
141174
// Legit IC

‎src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/SetType.fs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ type SetType() =
9999
Assert.IsFalse(ic.Contains("A"))
100100
letnewArr= Array.create5"a"
101101
ic.CopyTo(newArr,0)
102+
103+
[<Test>]
104+
memberthis.IReadOnlyCollection()=
105+
// Legit IROC
106+
letiroc=(new Set<int>([1;2;3;4])):> IReadOnlyCollection<int>
107+
Assert.AreEqual(iroc.Count,4)
108+
109+
// Empty IROC
110+
letiroc=(new Set<string>([])):> IReadOnlyCollection<string>
111+
Assert.AreEqual(iroc.Count,0)
102112

103113
[<Test>]
104114
memberthis.IComparable()=

‎src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Control/AsyncModule.fs‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ open System
99
openSystem.Threading
1010
openFSharp.Core.Unittests.LibraryTestFx
1111
openNUnit.Framework
12-
#if!(FSCORE_PORTABLE_OLD||FSCORE_PORTABLE_NEW)
12+
#if!FSCORE_PORTABLE_NEW
1313
openFsCheck
1414
#endif
1515

@@ -25,7 +25,7 @@ type [<Struct>] Dummy (x: int) =
2525
memberthis.Dispose()=()
2626

2727

28-
#if!(FSCORE_PORTABLE_OLD||FSCORE_PORTABLE_NEW)
28+
#if!FSCORE_PORTABLE_NEW
2929
[<AutoOpen>]
3030
moduleChoiceUtils=
3131

@@ -417,7 +417,7 @@ type AsyncModule() =
417417
Assert.Fail("TimeoutException expected")
418418
with
419419
:? System.TimeoutException->()
420-
#if!FSCORE_PORTABLE_OLD
420+
421421
[<Test>]
422422
memberthis.``RunSynchronously.NoThreadJumpsAndTimeout.DifferentSyncContexts``()=
423423
letrun syncContext=
@@ -434,7 +434,6 @@ type AsyncModule() =
434434
if!failedthen Assert.Fail("TimeoutException expected")
435435
runnull
436436
run(System.Threading.SynchronizationContext())
437-
#endif
438437

439438
[<Test>]
440439
memberthis.``RaceBetweenCancellationAndError.AwaitWaitHandle``()=
@@ -447,7 +446,7 @@ type AsyncModule() =
447446
memberthis.``RaceBetweenCancellationAndError.Sleep``()=
448447
testErrorAndCancelRace(Async.Sleep(-5))
449448

450-
#if!(FSCORE_PORTABLE_OLD||FSCORE_PORTABLE_NEW|| coreclr)
449+
#if!(FSCORE_PORTABLE_NEW|| coreclr)
451450
[<Test; Category("Expensive"); Explicit>]// takes 3 minutes!
452451
memberthis.``Async.Choice specification test``()=
453452
ThreadPool.SetMinThreads(100,100)|> ignore
@@ -573,7 +572,7 @@ type AsyncModule() =
573572
Assert.AreEqual("boom",!r)
574573

575574

576-
#if!FSCORE_PORTABLE_OLD&&!FSCORE_PORTABLE_NEW
575+
#if!FSCORE_PORTABLE_NEW
577576
[<Test>]
578577
memberthis.``SleepContinuations``()=
579578
letokCount= ref0

‎src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Core/BigIntType.fs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ type BigIntType() =
198198
Assert.AreEqual(BigInteger.One,1I)
199199

200200
()
201-
#if!FSCORE_PORTABLE_OLD
201+
202202
[<Test>]
203203
memberthis.Parse()=
204204
Assert.AreEqual(BigInteger.Parse("12345678901234567890"),
@@ -220,7 +220,6 @@ type BigIntType() =
220220
CheckThrowsArgumentNullException(fun()-> BigInteger.Parse(null)|> ignore)
221221

222222
()
223-
#endif
224223

225224
[<Test>]
226225
memberthis.Pow()=

‎src/fsharp/FSharp.Core/map.fs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,16 @@ namespace Microsoft.FSharp.Collections
612612
|_->
613613
invalidArg"obj"(SR.GetString(SR.notComparable))
614614

615+
interface IReadOnlyCollection<KeyValuePair<'Key, 'Value>>with
616+
members.Count= s.Count
617+
618+
interface IReadOnlyDictionary<'Key, 'Value>with
619+
members.Itemwith get(key)= s.[key]
620+
members.Keys=seq{for kvpin s-> kvp.Key}
621+
members.TryGetValue(key,value)=if s.ContainsKey(key)then(value<- s.[key];true)elsefalse
622+
members.Values=seq{for kvpin s-> kvp.Value}
623+
members.ContainsKey key= s.ContainsKey key
624+
615625
overridex.ToString()=
616626
match List.ofSeq(Seq.truncate4 x)with
617627
|[]->"map []"

‎src/fsharp/FSharp.Core/map.fsi‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ namespace Microsoft.FSharp.Collections
6161
interface IEnumerable<KeyValuePair<'Key, 'Value>>
6262
interface System.IComparable
6363
interface System.Collections.IEnumerable
64+
interface IReadOnlyCollection<KeyValuePair<'Key,'Value>>
65+
interface IReadOnlyDictionary<'Key,'Value>
6466
overrideEquals:obj->bool
6567

6668
/// <summary>Functional programming operators related to the <c>Map&lt;_,_&gt;</c> type.</summary>

‎src/fsharp/FSharp.Core/math/z.fs‎

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -402,49 +402,6 @@ namespace Microsoft.FSharp.Core
402402
if okthen
403403
res
404404
else
405-
#if FSCORE_PORTABLE_OLD
406-
// SL5 (and therefore Portable Profile47) does not have Parse, so make our own simple implementation
407-
letparse(s:string)=
408-
// ws* sign? digits+ ws*
409-
let mutablei=0
410-
// leading whitespace
411-
while i< s.Length&& System.Char.IsWhiteSpace(s.[i])do
412-
i<- i+1
413-
if i= s.Lengththen
414-
raise<|new System.ArgumentException()
415-
// optional sign
416-
let mutableisNegative=false
417-
if s.[i]='+'then
418-
i<- i+1
419-
elif s.[i]='-'then
420-
isNegative<-true
421-
i<- i+1
422-
if i= s.Lengththen
423-
raise<|new System.ArgumentException()
424-
// digits
425-
letstartDigits= i
426-
while i< s.Length&& System.Char.IsDigit(s.[i])do
427-
i<- i+1
428-
letendDigits= i
429-
letlen= endDigits- startDigits
430-
if len=0then
431-
raise<|new System.ArgumentException()
432-
// trailing whitespace
433-
while i< s.Length&& System.Char.IsWhiteSpace(s.[i])do
434-
i<- i+1
435-
if i<> s.Lengththen
436-
raise<|new System.ArgumentException()
437-
// text is now valid, parse it
438-
let mutabler=new System.Numerics.BigInteger(int(s.[startDigits])- int('0'))
439-
letten=new System.Numerics.BigInteger(10)
440-
for jin startDigits+1.. endDigits-1do
441-
r<- r* ten
442-
r<- r+new System.Numerics.BigInteger(int(s.[j])- int('0'))
443-
if isNegativethen
444-
r<-new System.Numerics.BigInteger(0)- r
445-
r
446-
letv= parse s
447-
#else
448405
letv=
449406
#if FX_NO_BIGINT
450407
BigInteger.Parse s
@@ -453,7 +410,6 @@ namespace Microsoft.FSharp.Core
453410
BigInteger.Parse(s.[2..],NumberStyles.AllowHexSpecifier,CultureInfo.InvariantCulture)
454411
else
455412
BigInteger.Parse(s,NumberStyles.AllowLeadingSign,CultureInfo.InvariantCulture)
456-
#endif
457413
#endif
458414
res<- v
459415
tabParse.[s]<- res

‎src/fsharp/FSharp.Core/prim-types.fs‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3034,10 +3034,8 @@ namespace Microsoft.FSharp.Collections
30343034
|(::): Head: 'T* Tail: 'T list-> 'T list
30353035
interface System.Collections.Generic.IEnumerable<'T>
30363036
interface System.Collections.IEnumerable
3037-
3038-
#if!FSCORE_PORTABLE_OLD
30393037
interface System.Collections.Generic.IReadOnlyCollection<'T>
3040-
#endif
3038+
interface System.Collections.Generic.IReadOnlyList<'T>
30413039

30423040
and 'T list= List<'T>
30433041

@@ -3220,11 +3218,12 @@ namespace Microsoft.FSharp.Collections
32203218

32213219
interface System.Collections.IEnumerablewith
32223220
memberl.GetEnumerator()=(PrivateListHelpers.mkListEnumerator l:> System.Collections.IEnumerator)
3223-
3224-
#if!FSCORE_PORTABLE_OLD
3221+
32253222
interface IReadOnlyCollection<'T>with
32263223
memberl.Count= l.Length
3227-
#endif
3224+
3225+
interface IReadOnlyList<'T>with
3226+
memberl.Itemwith get(index)= l.[index]
32283227

32293228
typeseq<'T>= IEnumerable<'T>
32303229

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp