3
3
using System . Collections . Generic ;
4
4
using System . IO ;
5
5
using System . Reflection ;
6
+ using System . Text ;
6
7
7
8
namespace DataStructures . Common
8
9
{
@@ -22,7 +23,6 @@ public sealed class PrimesList
22
23
23
24
//
24
25
// INSTANCE VARIABLES
25
- private static string _primesDocPath = string . Empty ;
26
26
private readonly static List < int > _primes = new List < int > ( ) ;
27
27
28
28
// Picked the HashPrime to be (101) because it is prime, and if the ‘hashSize - 1’ is not a multiple of this HashPrime, which is
@@ -64,10 +64,9 @@ public static PrimesList Instance
64
64
/// </summary>
65
65
private static void _initializeData ( )
66
66
{
67
- _primesDocPath = Path . Combine ( Path . GetDirectoryName ( typeof ( PrimesList ) . GetTypeInfo ( ) . Assembly . Location ) , @"Data/PrimesDocument_10K.csv" ) ;
68
- string [ ] lines = File . ReadAllLines ( _primesDocPath ) ;
67
+ string [ ] lines = _readResource ( "DataStructures.Data.PrimesDocument_10K.csv" ) ;
69
68
70
- foreach ( var line in lines )
69
+ foreach ( var line in lines )
71
70
{
72
71
// Split the line by commas and convert the collection to a list.
73
72
var numbersAsStrings = line . Split ( ',' ) . ToList < string > ( ) ;
@@ -209,6 +208,24 @@ public void CopyTo(int[] array, int index = 0)
209
208
}
210
209
}
211
210
212
- }
211
+ /// <summary>
212
+ /// Reads an embedded resource as a text file.
213
+ /// </summary>
214
+ /// <returns></returns>
215
+ private static string [ ] _readResource ( string resourceName )
216
+ {
217
+ try
218
+ {
219
+ using ( var stream = typeof ( PrimesList ) . GetTypeInfo ( ) . Assembly . GetManifestResourceStream ( resourceName ) )
220
+ using ( var reader = new StreamReader ( stream ?? throw new InvalidOperationException ( "Failed to read resource" ) , Encoding . UTF8 ) )
221
+ return reader . ReadToEnd ( ) . Split ( "\r \n " ) ;
222
+ }
223
+ catch ( Exception ex )
224
+ {
225
+ throw new Exception ( $ "Failed to read resource { resourceName } ", ex ) ;
226
+ }
227
+ }
228
+
229
+ }
213
230
214
231
}
0 commit comments