JavaAlgorithms
Elementary and no so elementary Java algorithms
sort.MergeSortFile< T extends Comparable< T > &Serializable > Class Reference

List of all members.

Classes

class  FileObjContainer
class  ReadFileValues

Public Member Functions

 MergeSortFile (int numObjs)
void sortFile (File inFile, File outFile) throws ClassNotFoundException, IOException

Protected Member Functions

String nextTempFileName ()
File writeSortedBlock (ArrayList< T > objList, File parentDir) throws IOException
File readAndSortBlock (BufferedInputStream bufStr, ObjectInputStream objStr, File parentDir) throws IOException, ClassNotFoundException
void mergeFiles (ArrayList< File > fileList, File outFile) throws IOException, ClassNotFoundException
ArrayList< File > buildSortedFileList (File inFile) throws IOException, ClassNotFoundException

Detailed Description

MergeSortFile

File based merge sort, implemented as a generic class that can sort objects that extend Comparable and implement the Serializable interface.

Jun 25, 2013

Author:
Ian Kaplan, iank@bearcave.com
Parameters:
<T>

Definition at line 35 of file MergeSortFile.java.


Constructor & Destructor Documentation

sort.MergeSortFile< T extends Comparable< T > &Serializable >.MergeSortFile ( int  numObjs)
Parameters:
numObjsthe number of objects to read into memory for sorting and, correspondingly, the maximum number of objects written to the temporary files. Since this code uses merge sort, twice this amount of memory will be needed (since merge sort uses a temporary array).

Definition at line 165 of file MergeSortFile.java.


Member Function Documentation

ArrayList<File> sort.MergeSortFile< T extends Comparable< T > &Serializable >.buildSortedFileList ( File  inFile) throws IOException, ClassNotFoundException [protected]

Read objects from an input file and sort them into a set of temporary files (these temporary files will be merged into the final sorted file).

Parameters:
inFilea file containing unsorted objects
Returns:
A files of file objects for the temporary files.
Exceptions:
IOException
ClassNotFoundException

Definition at line 337 of file MergeSortFile.java.

void sort.MergeSortFile< T extends Comparable< T > &Serializable >.mergeFiles ( ArrayList< File >  fileList,
File  outFile 
) throws IOException, ClassNotFoundException [protected]

Merge the values in the sorted files into a single file.

Parameters:
fileListone or more files of sorted objects
outFilethe file for the complete sorted data
Exceptions:
IOException
ClassNotFoundException

Definition at line 279 of file MergeSortFile.java.

String sort.MergeSortFile< T extends Comparable< T > &Serializable >.nextTempFileName ( ) [protected]

Return a new temporary file name

Returns:

Definition at line 174 of file MergeSortFile.java.

File sort.MergeSortFile< T extends Comparable< T > &Serializable >.readAndSortBlock ( BufferedInputStream  bufStr,
ObjectInputStream  objStr,
File  parentDir 
) throws IOException, ClassNotFoundException [protected]

Read a block of mNumObject unsorted objects, sort them and write them to a file. Note that this requires at least 3x the memory for mNumObjects due to the in-memory merge sort and the ArrayList in this function.

Both a BufferedInputStream and an ObjectInputStream are passed to this method. The only way to tell if an ObjectInputStream has reached the end of file is via an exception, which violates the idea that exceptions are for exceptional circumstance (reaching the end of file is expected, not exceptional). So the BufferedInputStream is also passed in so that the end of stream can be recognized.

Parameters:
bufStra buffered input stream for the file containing the unsorted objects
objStran object input stream
parentDirthe parent directory (e.g., the directory that will contain the temporary files)
Returns:
the temporary file containing the sorted objects
Exceptions:
IOException
ClassNotFoundException

Definition at line 251 of file MergeSortFile.java.

void sort.MergeSortFile< T extends Comparable< T > &Serializable >.sortFile ( File  inFile,
File  outFile 
) throws ClassNotFoundException, IOException

Use merge sort to sort the values in inFile into outFile. The input file and the output file may be the same file.

Parameters:
inFile
outFile
Exceptions:
ClassNotFoundException
IOException

Definition at line 377 of file MergeSortFile.java.

File sort.MergeSortFile< T extends Comparable< T > &Serializable >.writeSortedBlock ( ArrayList< T >  objList,
File  parentDir 
) throws IOException [protected]

Write am array of sorted objects to a File and return the associated File.

Parameters:
objListAn array of sorted objects
parentDirthe parent directory (where the temporary files will be allocated).
Returns:
the temporary File object for the file containing the sorted objects
Exceptions:
IOException

Definition at line 193 of file MergeSortFile.java.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables