anchor.diff:  !
   END MODULE MODHASHCODE
   ! ---------------------------------------------------------------------------------------------------------------------------------+
  -                                                     MODULE MODHASH
  +                                                     MODULE MODBTREE
   ! ---------------------------------------------------------------------------------------------------------------------------------+
   IMPLICIT NONE
   PRIVATE
  -PUBLIC :: HASH,HASH_RESIZE,HASH_ADD,HASH_SORT
  -TYPE HASH
  -  INTEGER(KIND=4)             :: CAPACITY
  -  INTEGER(KIND=4)             :: COUNT
  -  INTEGER(KIND=4),ALLOCATABLE :: HASH(:,:)
  -END TYPE HASH
  -INTEGER(KIND=4),PARAMETER     :: DEFAULT_BLOCKSIZE = 64
  -INTEGER(KIND=4)               :: BLOCKSIZE
  +PUBLIC :: BTREE,BTREE_ADD,BTREE_SORT,BTREE_SETBLOCKSIZE,BTREE_INIT
  +TYPE BTREE                                                      ! BTREE data type
  +  INTEGER(KIND=4)             :: CAPACITY                       ! Hash maximum capacity
  +  INTEGER(KIND=4)             :: COUNT                          ! Current count of items stored in hash.
  +  INTEGER(KIND=4),ALLOCATABLE :: TREE(:,:)                      ! Hash Data dimensioned (1:3,1:CAPACITY)
  +END TYPE BTREE
  +INTEGER(KIND=4),PARAMETER     :: DEFAULT_BLOCKSIZE = 4
  +INTEGER(KIND=4)               :: BLOCKSIZE = DEFAULT_BLOCKSIZE  ! Block size for resizing hash
   ! ---------------------------------------------------------------------------------------------------------------------------------+
                                                              CONTAINS
   ! ---------------------------------------------------------------------------------------------------------------------------------+
  -SUBROUTINE HASH_RESIZE(H, CAPACITY)
  -TYPE(HASH),INTENT(INOUT) :: H
  -INTEGER(KIND=4),INTENT(IN)   :: CAPACITY
  -INTEGER(KIND=4),ALLOCATABLE  :: TEMP(:,:)
  -INTEGER(KIND=4)              :: OLDCAPACITY,I,J,IERR
  -  IF (ALLOCATED(H%HASH)) THEN
  -    OLDCAPACITY = SIZE(H%HASH,2)
  +SUBROUTINE BTREE_SETBLOCKSIZE(BSIZE)
  +INTEGER(KIND=4),INTENT(IN) :: BSIZE
  +  IF (BSIZE.GT.0) BLOCKSIZE = BSIZE
  +END SUBROUTINE BTREE_SETBLOCKSIZE
  +! ---------------------------------------------------------------------------------------------------------------------------------+
  +SUBROUTINE BTREE_INIT(BT)
  +TYPE(BTREE),INTENT(INOUT) :: BT                          ! Hash to be initialized
  +  CALL BTREE_RESIZE(BT,BLOCKSIZE)
  +END SUBROUTINE BTREE_INIT
  +! ---------------------------------------------------------------------------------------------------------------------------------+
  +SUBROUTINE BTREE_RESIZE(BT, CAPACITY)
  +TYPE(BTREE),INTENT(INOUT)   :: BT                        ! Hash to be resized
  +INTEGER(KIND=4),INTENT(IN)  :: CAPACITY                  ! Capacity to which to resize BTREE
  +INTEGER(KIND=4),ALLOCATABLE :: TEMP(:,:)                 ! Temporary storage array for existing data
  +INTEGER(KIND=4)             :: OLDCAPACITY,I,J,IERR      ! Temp variables, error status, and counters
  +  IF (ALLOCATED(BT%TREE)) THEN
  +    OLDCAPACITY = SIZE(BT%TREE,2)
       ALLOCATE(TEMP(3,OLDCAPACITY),STAT=IERR)
       IF (IERR.NE.0) THEN
  -      WRITE(0,'(A)') '0*** UNABLE TO ALLOCATE TEMPORARY MEMORY FOR HASH.'
  +      WRITE(0,'(A)') '0*** BTREE_RESIZE: UNABLE TO ALLOCATE TEMPORARY MEMORY FOR BTREE.'
         STOP
       END IF
  -    TEMP = H%HASH
  -    CALL HASH_ALLOCATE(H, CAPACITY)
  +    TEMP = BT%TREE
  +    CALL BTREE_ALLOCATE(BT, CAPACITY)
       DO J = 1, MIN(OLDCAPACITY,CAPACITY)
         DO I = 1,3
  -        H%HASH(I,J) = TEMP(I,J)
  +        BT%TREE(I,J) = TEMP(I,J)
         END DO
       END DO
       DEALLOCATE(TEMP,STAT=IERR)
       IF (IERR.NE.0) THEN
  -      WRITE(0,'(A)') '0*** UNABLE TO DEALLOCATE TEMPORARY MEMORY FOR HASH.'
  +      WRITE(0,'(A)') '0*** BTREE_RESIZE: UNABLE TO DEALLOCATE TEMPORARY MEMORY FOR BTREE.'
         STOP
       END IF
     ELSE
  -    CALL HASH_ALLOCATE(H, CAPACITY)
  -  END IF
  -END SUBROUTINE HASH_RESIZE
  -! ---------------------------------------------------------------------------------------------------------------------------------+
  -SUBROUTINE HASH_ALLOCATE(H, CAPACITY)
  -TYPE(HASH),INTENT(INOUT) :: H
  -INTEGER(KIND=4),INTENT(IN)   :: CAPACITY
  -INTEGER(KIND=4)              :: IERR
  -  IF (ALLOCATED(H%HASH)) THEN
  -    DEALLOCATE(H%HASH,STAT=IERR)
  +    CALL BTREE_ALLOCATE(BT, CAPACITY)
  +  END IF
  +END SUBROUTINE BTREE_RESIZE
  +! ---------------------------------------------------------------------------------------------------------------------------------+
  +SUBROUTINE BTREE_ALLOCATE(BT, CAPACITY)
  +TYPE(BTREE),INTENT(INOUT)  :: BT
  +INTEGER(KIND=4),INTENT(IN) :: CAPACITY
  +INTEGER(KIND=4)            :: IERR
  +  IF (ALLOCATED(BT%TREE)) THEN
  +    DEALLOCATE(BT%TREE,STAT=IERR)
       IF (IERR.NE.0) THEN
  -      WRITE(0,'(A)') '0*** UNABLE TO DEALLOCATE MEMORY FOR HASH.'
  +      WRITE(0,'(A)') '0*** BTREE_ALLOCATE: UNABLE TO DEALLOCATE MEMORY FOR BTREE.'
         STOP
       END IF
     END IF
  -  ALLOCATE(H%HASH(3,CAPACITY),STAT=IERR)
  +  ALLOCATE(BT%TREE(3,CAPACITY),STAT=IERR)
     IF (IERR.NE.0) THEN
  -    WRITE(0,'(A)') '0*** UNABLE TO ALLOCATE MEMORY FOR HASH.'
  +    WRITE(0,'(A)') '0*** BTREE_ALLOCATE: UNABLE TO ALLOCATE MEMORY FOR BTREE.'
       STOP
     END IF
  -  H%HASH = 0
  -  H%CAPACITY = CAPACITY
  -END SUBROUTINE HASH_ALLOCATE
  -! ---------------------------------------------------------------------------------------------------------------------------------+
  -INTEGER(KIND=4) FUNCTION HASH_ADD(H,HASHCODE)
  -TYPE(HASH),INTENT(INOUT) :: H
  -INTEGER(KIND=4),INTENT(IN)   :: HASHCODE
  -INTEGER(KIND=4)              :: I,J
  -
  -  IF (H%COUNT.EQ.0) THEN                   ! Special case where this is the first value.
  -    H%COUNT = 1
  -    H%HASH(1,H%COUNT) = HASHCODE
  -    H%HASH(2,1) = 0
  -    H%HASH(3,1) = 0
  -    HASH_ADD = H%COUNT
  +  BT%TREE = 0
  +  BT%CAPACITY = CAPACITY
  +END SUBROUTINE BTREE_ALLOCATE
  +! ---------------------------------------------------------------------------------------------------------------------------------+
  +INTEGER(KIND=4) FUNCTION BTREE_ADD(BT,HASHCODE)
  +TYPE(BTREE),INTENT(INOUT)  :: BT
  +INTEGER(KIND=4),INTENT(IN) :: HASHCODE
  +INTEGER(KIND=4)            :: I,J
  +
  +  IF (BT%COUNT.EQ.0) THEN                   ! Special case where this is the first value.
  +    BT%COUNT = 1
  +    BT%TREE(1,BT%COUNT) = HASHCODE
  +    BT%TREE(2,1) = 0
  +    BT%TREE(3,1) = 0
  +    BTREE_ADD = BT%COUNT
       RETURN
  -  ELSE IF (H%COUNT.EQ.SIZE(H%HASH,2)) THEN
  -    WRITE(0,'(A)') '0*** HASH IS FULL.'
  -    FLUSH(0)
  -    STOP
     ENDIF
   
  +  IF (BT%COUNT.EQ.SIZE(BT%TREE,2)) CALL BTREE_RESIZE(BT,BT%CAPACITY+BLOCKSIZE)
  +
     J = 1                                      ! Start at top of the table.
  -  DO WHILE (J.LE.H%COUNT)
  +  DO WHILE (J.LE.BT%COUNT)
       IF (J.EQ.0) RETURN
   
  -    IF (HASHCODE.LT.H%HASH(1,J)) THEN
  +    IF (HASHCODE.LT.BT%TREE(1,J)) THEN
         I = 2
  -    ELSE IF (HASHCODE.GT.H%HASH(1,J)) THEN
  +    ELSE IF (HASHCODE.GT.BT%TREE(1,J)) THEN
         I = 3
       ELSE
         I = 0
       END IF
  -    HASH_ADD = J
  +    BTREE_ADD = J
       IF (I.EQ.0) RETURN
       
  -    IF (H%HASH(I,J).EQ.0) THEN
  -      H%COUNT = H%COUNT + 1
  -      H%HASH(1,H%COUNT) = HASHCODE
  -      H%HASH(I,J) = H%COUNT
  -      HASH_ADD = H%COUNT
  +    IF (BT%TREE(I,J).EQ.0) THEN
  +      BT%COUNT = BT%COUNT + 1
  +      BT%TREE(1,BT%COUNT) = HASHCODE
  +      BT%TREE(I,J) = BT%COUNT
  +      BTREE_ADD = BT%COUNT
         J = 0
       ELSE
  -      J = H%HASH(I,J)
  +      J = BT%TREE(I,J)
       END IF
   
     END DO
   
  -END FUNCTION HASH_ADD
  -! ---------------------------------------------------------------------------------------------------------------------------------+
  -SUBROUTINE HASH_SORT(H, ARRAY, ASCENDING)
  -TYPE(HASH), INTENT(IN)                 :: H
  +END FUNCTION BTREE_ADD
  +! ---------------------------------------------------------------------------------------------------------------------------------+
  +SUBROUTINE BTREE_SORT(BT, ARRAY, ASCENDING)
  +TYPE(BTREE), INTENT(IN)                :: BT
   INTEGER(KIND=4),INTENT(INOUT)          :: ARRAY(:)
   LOGICAL(KIND=4),INTENT(IN),   OPTIONAL :: ASCENDING
   INTEGER(KIND=4)                        :: I,C
  
positive.code:   END IF
    I = 1
    C = 0
    CALL HASH_SORTED(H,ARRAY,I,C,ASC)
  END SUBROUTINE HASH_SORT
  ! ---------------------------------------------------------------------------------------------------------------------------------+
  RECURSIVE SUBROUTINE HASH_SORTED(H,ARRAY,I,C,ASCENDING)
  TYPE(HASH), INTENT(IN)    :: H
  INTEGER(KIND=4),INTENT(INOUT) :: ARRAY(:)
  INTEGER(KIND=4),INTENT(IN)    :: I
  INTEGER(KIND=4),INTENT(INOUT) :: C
  LOGICAL(KIND=4),INTENT(IN)    :: ASCENDING
  IF (ASCENDING) THEN
    IF (H%HASH(2,I).NE.0) CALL HASH_SORTED(H,ARRAY,H%HASH(2,I),C,ASCENDING)
    IF (C.GT.SIZE(ARRAY)) RETURN
    C = C + 1
    ARRAY(C) = H%HASH(1,I)
    IF (H%HASH(3,I).NE.0) CALL HASH_SORTED(H,ARRAY,H%HASH(3,I),C,ASCENDING)
  ELSE
    IF (H%HASH(3,I).NE.0) CALL HASH_SORTED(H,ARRAY,H%HASH(3,I),C,ASCENDING)
    IF (C.GT.SIZE(ARRAY)) RETURN
    C = C + 1
    ARRAY(C) = H%HASH(1,I)
    IF (H%HASH(2,I).NE.0) CALL HASH_SORTED(H,ARRAY,H%HASH(2,I),C,ASCENDING)
  END IF
  END SUBROUTINE HASH_SORTED
  ! ---------------------------------------------------------------------------------------------------------------------------------+
  END MODULE MODHASH
  ! ---------------------------------------------------------------------------------------------------------------------------------+
  PROGRAM HASHTEST
  USE MODHASHCODE
  USE MODHASH
  IMPLICIT NONE
  INTEGER(KIND=4)           :: RES,I,J
  INTEGER(KIND=1),PARAMETER :: IDIV = 11
  
negative.code:                                               !  |  14  |  87  |
                                                !  |  18  |  85  |
                                                !  |  42  |  78  |
    CALL BTREE_SORT(BT,ASC)                     !  |  44  |  72  |        
    CALL BTREE_SORT(BT,DESC,.FALSE.)            !  |  60  |  64  |
    DO I = 1, 10                                !  |  64  |  60  |
      WRITE(6,'(2I8)') ASC(I),DESC(I)           !  |  72  |  44  |
    END DO                                      !  |  78  |  42  |
  