home *** CD-ROM | disk | FTP | other *** search
-
- /* t4sort2.c (c)Copyright Sequiter Software Inc., 1987-1990. All rights reserved. */
-
- /* Create three databases.
-
- Sort using an index file lookups on the other databases.
- */
-
- #include "p4misc.h"
- #include "d4all.h"
- #include "w4.h"
- #include "u4error.h"
-
- #include <stdlib.h>
-
- int t4sort2(long,long) ;
-
- main(int argc, char **argv)
- {
- long num_rec, num_alloc ;
-
- #ifdef NO_HUGE
- #ifdef IS_386
- d4init() ;
- #else
- d4initialize( 4, 4, 20, 1000, 20000L ) ;
- #endif
- #else
- d4init() ;
- #endif
- w4clear(-1) ;
-
- if ( argc < 2 )
- {
- w4(0,0, "t4sort2 Num_Records [MemAlloc(Default 8000)]") ;
- w4(1,0, "t4sort2 Test 'x4sort' routine") ;
- w4cursor(2,0) ;
- w4exit(1) ;
- }
-
- num_rec = atol( argv[1] ) ;
-
- if ( argc > 2 )
- num_alloc = atol( argv[2] ) ;
- else
- num_alloc = 8000 ;
-
- w4( w4row()+1,0, "Num Records:" ) ; w4long( w4row(),w4col(), num_rec, 8 ) ;
- w4( w4row()+1,0, "Mem Alloc: " ) ; w4long( w4row(),w4col(), num_alloc,8 ) ;
-
- t4sort2( num_rec, num_alloc ) ;
- d4close_all() ;
-
- #ifdef H4TEST
- {
- int rc ;
- d4init_undo() ;
- rc = h4free_check(32000) ;
- d4init() ;
- if ( rc != 0 )
- {
- u4error( 0, "t4sort2: Memory items not freed", (char *) 0 );
- w4exit(1) ;
- }
- }
- #endif
-
- w4handle(1) ;
- w4( w4row()+1,0, "t4sort2: SUCCESS" ) ;
-
- w4exit(0) ;
- }
-
- void check_base(long), check_index(long) ;
-
- static FIELD start[] =
- {
- { "VAL", 'C', 6, 0, 0 },
- } ;
-
- static FIELD t4look_a[] =
- {
- { "VAL", 'C', 6, 0, 0 },
- } ;
-
- static FIELD t4look_b[] =
- {
- { "VAL", 'C', 6, 0, 0 },
- } ;
-
-
- /*
- Each index file is indexed according to record number ordering.
-
- 'T4LOOK_B' - has 'num_rec' records.
- - Reverse Order
-
- 'T4LOOK_A' - has 'num_rec*2' records.
- - Regular Order
-
- 'T4START' - has 'num_rec*4' records.
- - Sort T4LOOK_B->VAL + T4LOOK_A->VAL + STR(RECNO())
- - Reverse Order
-
- Example: 8 Records
-
- Rec 8 (1) -> Rec 4 (4) -> Rec 2 (1)
- Rec 4 (5) -> Rec 2 (2) -> Rec 1 (2)
- */
-
- int t4sort2( long num_recs, long mem_alloc )
- {
- int start_ref, new_ref, t4look_a_ref, i_t4look_a_ref,
- t4look_b_ref, i_t4look_b_ref ;
- long f_ref, i_rec ;
-
- d4buf_init( mem_alloc, mem_alloc, 1L ) ;
- start_ref = d4create( "t4start", 1, start, 0 ) ;
-
- t4look_a_ref = d4create( "T4LOOK_A", 1, t4look_a, 0 ) ;
- i_t4look_a_ref= i4index( "t4look_a", "STR(RECNO(),6,0)", 0, 0 ) ;
-
- t4look_b_ref = d4create( "t4look_b", 1, t4look_b, 0 ) ;
- i_t4look_b_ref= i4index( "t4look_b", "STR(RECNO(),6,0)", 0, 0 ) ;
-
- /* Define two relations, first to lookup in 'T4LOOK_A.DBF' and
- another recursive relation to lookup in 'T4LOOK_B.DBF'.
- */
- d4select( start_ref ) ;
- d4buf_total( 200L, 3, 1 ) ;
- d4buf_unit( 5L ) ;
- x4relate( "STR((RECNO()+.1)/2,6,0)", t4look_a_ref, i_t4look_a_ref, -1L ) ;
-
- d4select( t4look_a_ref ) ;
- d4buf_total( 20L, 3, 1 ) ;
- d4buf_unit( 15L ) ;
- x4relate( "STR((RECNO()+.1)/2,6,0)", t4look_b_ref, i_t4look_b_ref, -1L ) ;
-
- d4select( t4look_b_ref ) ;
- d4buf_total( 70L, 2, 1 ) ;
- d4buf_unit( 25L ) ;
-
- /* Now add the data. */
- d4select( start_ref ) ;
- f_ref = f4ref( "VAL" ) ;
- for ( i_rec = 1L; i_rec <= 4*num_recs; i_rec++ )
- {
- f4r_long( f_ref, 4*num_recs+1L-i_rec ) ;
- d4append() ;
- }
-
- d4select( t4look_a_ref ) ;
- f_ref = f4ref( "VAL" ) ;
- for ( i_rec = 1L; i_rec <= 2*num_recs; i_rec++ )
- {
- f4r_long( f_ref, i_rec ) ;
- d4append() ;
- }
-
- d4select( t4look_b_ref ) ;
- f_ref = f4ref( "VAL" ) ;
- for ( i_rec = 1L; i_rec <= num_recs; i_rec++ )
- {
- f4r_long( f_ref, num_recs+1L-i_rec ) ;
- d4append() ;
- }
-
- d4select( start_ref ) ;
- new_ref = x4sort( "T4SORT2", "T4LOOK_B->VAL + T4LOOK_A->VAL + VAL", 1L, 0) ;
-
- d4select( new_ref ) ;
- check_base( num_recs ) ;
-
- d4select( start_ref ) ;
- i4index_filter( "t4start", "T4LOOK_B->VAL + T4LOOK_A->VAL + VAL", 1, 0,
- x4relate_do ) ;
- check_index( num_recs ) ;
-
- if ( d4skip(1L) != 3 )
- {
- u4error( 0, "t4sort2", (char *) 0 ) ;
- w4exit(1) ;
- }
-
- return 0 ;
- }
-
-
- void check_index( long n )
- {
- long i,j,k ;
-
- d4top() ;
-
- for ( i=n; i>=1; i-- )
- {
- for ( j=i*2-1; j <= i*2; j++ )
- {
- for ( k=j*2; k>= j*2-1; k-- )
- {
- if ( d4recno() != k || d4eof())
- {
- u4error( 0, "t4sort2: checker", (char *) 0 ) ;
- w4exit(1) ;
- }
- d4skip(1L) ;
- }
- }
- }
- if ( ! d4eof())
- {
- u4error( 0, "t4sort2", (char *) 0 ) ;
- w4exit(1) ;
- }
- }
-
- void check_base( long n )
- {
- long i,j,k ;
-
- d4top() ;
- for ( i=n; i>=1; i-- )
- {
- for ( j=i*2-1; j <= i*2; j++ )
- {
- for ( k=j*2; k>= j*2-1; k-- )
- {
- long val ;
-
- val = f4long(f4ref("VAL")) ;
-
- if ( d4reccount()-val+1L != k || d4eof() )
- {
- u4error( 0, "t4sort2: checker", (char *) 0 ) ;
- w4exit(1) ;
- }
- d4skip(1L) ;
- }
- }
- }
- if ( ! d4eof())
- {
- u4error( 0, "t4sort2", (char *) 0 ) ;
- w4exit(1) ;
- }
- }
-