77
88use std:: cmp;
99use std:: cmp:: PartialEq ;
10- use std:: collections:: { BTreeSet , HashMap , HashSet } ;
10+ use std:: collections:: { BTreeSet , HashSet } ;
1111use std:: ffi:: { OsStr , OsString } ;
1212use std:: fmt:: Write as FmtWrite ;
1313use std:: fs:: File ;
@@ -269,10 +269,10 @@ struct FileContent {
269269 offset : usize ,
270270}
271271
272- type FileMap = HashMap < OsString , FileContent > ;
272+ type FileMap = Vec < ( OsString , FileContent ) > ;
273273
274274fn read_input ( input_files : & [ OsString ] ) -> std:: io:: Result < FileMap > {
275- let mut file_map: FileMap = HashMap :: new ( ) ;
275+ let mut file_map: FileMap = FileMap :: new ( ) ;
276276 let mut offset: usize = 0 ;
277277 for filename in input_files {
278278 let reader: BufReader < Box < dyn Read > > = BufReader :: new ( if filename == "-" {
@@ -287,14 +287,14 @@ fn read_input(input_files: &[OsString]) -> std::io::Result<FileMap> {
287287 // Since we will be jumping around the line a lot, we dump the content into a Vec<char>, which can be indexed in constant time.
288288 let chars_lines: Vec < Vec < char > > = lines. iter ( ) . map ( |x| x. chars ( ) . collect ( ) ) . collect ( ) ;
289289 let size = lines. len ( ) ;
290- file_map. insert (
290+ file_map. push ( (
291291 filename. clone ( ) ,
292292 FileContent {
293293 lines,
294294 chars_lines,
295295 offset,
296296 } ,
297- ) ;
297+ ) ) ;
298298 offset += size;
299299 }
300300 Ok ( file_map)
@@ -751,8 +751,13 @@ fn write_traditional_output(
751751 }
752752
753753 for word_ref in words {
754- let file_map_value: & FileContent = file_map
755- . get ( & word_ref. filename )
754+ let ( _, file_map_value) = file_map
755+ . iter ( )
756+ . find ( |( name, content) | {
757+ name == & word_ref. filename
758+ && word_ref. global_line_nr >= content. offset
759+ && word_ref. global_line_nr < content. offset + content. lines . len ( )
760+ } )
756761 . expect ( "Missing file in file map" ) ;
757762 let FileContent {
758763 ref lines,
0 commit comments