Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Challenge: 3-way merge:
#4
Well it looks like nobody jumped in with a solution. You probably figured out that this is a tricky program. Let me give you a few tips that really simplify the logic of this program:

COMPARE CONTROL INDICATOR:
----------------------------------------
I call this NEXTLOW. This is a integer whose bits indicate the results of comparing the keys of the 3 files (AAA,BBB,CCC).
Bit 1 of NEXTLOW corresponds to file AAA.
Bit 2 of NEXTLOW corresponds to file BBB.
Bit 4 of NEXTLOW corresponds to file CCC.

As a result of the compares, you OR the corresponding bit or bits into NEXTLOW based on which file key or keys are low.
Then:
NEXTLOW is 1 when key of file AAA compared the lowest.
NEXTLOW is 2 when key of file BBB compared the lowest.
NEXTLOW is 3 when keys of AAA & BBB compared the lowest.
NEXTLOW is 4 when key of file CCC compared the lowest.
NEXTLOW is 5 when keys of AAA & CCC compared the lowest.
NEXTLOW is 6 when keys of BBB & CCC compared the lowest.
NEXTLOW is 7 when keys of AAA, BBB & CCC compared equal.

When you're done with the compares and setting up NEXTLOW, which is about 16 lines of code, the rest of the program is trivial because the NEXTLOW indicator tells you what you have to do. Example: If NEXTLOW IS 5, you have to write the records from file AAA and CCC to the output, and then you have to read new records from AAA and CCC, and go back to the compare logic again.

HIGHVALUE CONTROL:
---------------------------
Using the concept of "highvalue" save you from setting switches to indicate that particular files have gone to end-of-file. How does this work? When the read for a particular file detects an end-of-file, you stuff a "highvalue" into the key for this file. A highvalue is a string or number that you have determined will be higher that any key of any file. In this sample program, we have a 4 byte key, To be absolutely safe we could set up a highvalue of 5 bytes of hex'FF'.
Having done this for all 3 files, then at the end of the compare logic we add one line of code:
IF NEXTLOW = 7 AND KEY.OF.AAA = HIGHVALUE THEN GOTO EOJ

What has happened is that the HIGHVALUE has flushed all the files. When the keys of all 3 files are sitting at HIGHVALUE, the program is finished --- there's no more records to process.

The above concepts of NEXTLOW and HIGHVALUE have been used successfully for over 40 years by industry expert. I didn't just pull them out of my hat.
*****
Reply


Messages In This Thread
Challenge: 3-way merge: - by Moneo - 07-04-2003, 09:53 PM
Challenge: 3-way merge: - by Antoni Gual - 07-05-2003, 04:09 AM
Challenge: 3-way merge: - by Moneo - 07-05-2003, 04:47 AM
Challenge: 3-way merge: - by Moneo - 07-09-2003, 11:48 PM
Challenge: 3-way merge: - by Antoni Gual - 07-12-2003, 04:56 AM
Challenge: 3-way merge: - by Moneo - 07-12-2003, 06:12 AM
Challenge: 3-way merge: - by Antoni Gual - 07-12-2003, 01:11 PM
Challenge: 3-way merge: - by Moneo - 07-13-2003, 04:25 AM
Challenge: 3-way merge: - by Moneo - 07-20-2003, 02:24 AM

Forum Jump:


Users browsing this thread: 2 Guest(s)