Changeset 4134


Ignore:
Timestamp:
Sep 6, 2014, 6:46:28 AM (4 years ago)
Author:
cameron
Message:

Eliminate duplicated output logic with write_matches routine.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/icgrep.cpp

    r4131 r4134  
    270270}
    271271
     272
     273typedef BitStreamScanner<BitBlock, uint32_t, uint32_t, SEGMENT_BLOCKS> ScannerT;
     274
     275//
     276// Write matched lines from a buffer to an output file, given segment
     277// scanners for line ends and matches (where matches are a subset of line ends).
     278// The buffer pointer must point to the first byte of the segment
     279// corresponding to the scanner indexes.   The first_line_start is the
     280// start position of the first line relative to the buffer start position.
     281// It must be zero or negative;  if negative, the buffer must permit negative
     282// indexing so that the lineup to the buffer start position can also be printed.   
     283// The start position of the final line in the processed segment is returned.
     284//
     285
     286ssize_t write_matches(FILE * outfile, ScannerT line_scanner, ScannerT match_scanner, char * buffer, ssize_t first_line_start) {
     287
     288  ssize_t line_start = first_line_start;
     289  size_t match_pos;
     290  size_t line_end;
     291  while (match_scanner.has_next()) {
     292    match_pos = match_scanner.scan_to_next();
     293    // If we found a match, it must be at a line end.
     294    line_end = line_scanner.scan_to_next();
     295    while (line_end < match_pos) {
     296      line_start = line_end + 1;
     297      line_end = line_scanner.scan_to_next();
     298    }
     299    fwrite(&buffer[line_start], 1, line_end - line_start + 1, outfile);
     300    line_start = line_end + 1;
     301
     302  }
     303  while(line_scanner.has_next()) {
     304    line_end = line_scanner.scan_to_next();
     305    line_start = line_end+1;
     306  }
     307  return line_start;
     308}
     309   
     310
     311
    272312#ifndef USE_MMAP
    273313void do_process(FILE *infile, FILE *outfile, int count_only_option, int carry_count, process_block_fcn process_block) {
     
    297337    int line_no = 0;
    298338
    299     BitStreamScanner<BitBlock, uint32_t, uint32_t, SEGMENT_BLOCKS> LF_scanner;
    300     BitStreamScanner<BitBlock, uint32_t, uint32_t, SEGMENT_BLOCKS> match_scanner;
    301 
    302 
     339    ScannerT LF_scanner;
     340    ScannerT match_scanner;
     341
     342    char * buffer_ptr;
    303343#ifndef USE_MMAP
    304344    ATTRIBUTE_SIMD_ALIGN char src_buffer[SEGMENT_SIZE];
    305 
     345    buffer_ptr = &src_buffer;
    306346    chars_read = fread((void *)&src_buffer[0], 1, SEGMENT_SIZE, infile);
    307347    chars_avail = chars_read;
     
    371411#endif
    372412#ifdef USE_MMAP
    373 
     413    buffer_ptr = &infile_buffer[segment_base];
    374414#endif
    375415
    376416        if (!count_only_option) {
    377 #ifndef USE_MMAP
    378             line_start = 0;
    379 #endif
    380 
    381             while (match_scanner.has_next()) {
    382                 match_pos = match_scanner.scan_to_next();
    383                 line_end = LF_scanner.scan_to_next();
    384                 while (line_end < match_pos) {
    385                     line_start = line_end+1;
    386                     line_no++;
    387                     line_end = LF_scanner.scan_to_next();
    388                 }
    389 #ifndef USE_MMAP
    390                 fwrite(&src_buffer[line_start], 1, line_end - line_start + 1, outfile);
    391 #endif
    392 #ifdef USE_MMAP
    393                 fwrite(&infile_buffer[segment_base + line_start], 1, line_end - line_start + 1, outfile);
    394 
    395 #endif
    396 
    397                 line_start = line_end+1;
    398                 line_no++;
    399             }
    400             while (LF_scanner.has_next()) {
    401                 line_end = LF_scanner.scan_to_next();
    402                 line_start = line_end+1;
    403                 line_no++;
    404             }
    405 
     417          line_start = write_matches(outfile, LF_scanner, match_scanner, buffer_ptr, line_start);
    406418        }
    407419#ifndef USE_MMAP
     
    508520        line_start = 0;
    509521#endif
    510         while (match_scanner.has_next())
    511         {
    512             match_pos = match_scanner.scan_to_next();
    513             line_end = LF_scanner.scan_to_next();
    514             while(line_end < match_pos)
    515             {
    516                 line_start = line_end + 1;
    517                 line_no++;
    518                 line_end = LF_scanner.scan_to_next();
    519             }
    520 #ifndef USE_MMAP
    521             fwrite(&src_buffer[line_start], 1, line_end - line_start + 1, outfile);
    522 #endif
    523 #ifdef USE_MMAP
    524             fwrite(&infile_buffer[segment_base + line_start], 1, line_end - line_start + 1, outfile);
    525 
    526 #endif
    527             line_start = line_end + 1;
    528             line_no++;
    529         }
    530         while(LF_scanner.has_next())
    531         {
    532             line_end = LF_scanner.scan_to_next();
    533             line_start = line_end+1;
    534             line_no++;
    535         }
     522#ifdef USE_MMAP
     523        buffer_ptr = &infile_buffer[segment_base];
     524#endif
     525        line_start = write_matches(outfile, LF_scanner, match_scanner, buffer_ptr, line_start);
    536526    }
    537527
Note: See TracChangeset for help on using the changeset viewer.