33using System . Collections . Generic ;
44using System . IO ;
55using System . Reflection ;
6+ using System . Text ;
67
78namespace DataStructures . Common
89{
@@ -22,7 +23,6 @@ public sealed class PrimesList
2223
2324 //
2425 // INSTANCE VARIABLES
25- private static string _primesDocPath = string . Empty ;
2626 private readonly static List < int > _primes = new List < int > ( ) ;
2727
2828 // 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
6464 /// </summary>
6565 private static void _initializeData ( )
6666 {
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" ) ;
6968
70- foreach ( var line in lines )
69+ foreach ( var line in lines )
7170 {
7271 // Split the line by commas and convert the collection to a list.
7372 var numbersAsStrings = line . Split ( ',' ) . ToList < string > ( ) ;
@@ -209,6 +208,24 @@ public void CopyTo(int[] array, int index = 0)
209208 }
210209 }
211210
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+ }
213230
214231}
0 commit comments