
C============================================================
C
C         Function for Queue
C
C============================================================

      SUBROUTINE enQ(cell)
      INCLUDE "exp_wolff.h" 
      INTEGER cell(row:col)

      Qsize=Qsize+1
      IF(Qsize .GT. Qmax)  THEN
         WRITE(6,*) 'Error:Overflow Queue'
         CALL  KABORT(1)
      ENDIF

      Qtail=Qtail+1
      IF(Qtail .EQ. Qmax)  THEN
         Qtail=0
      ENDIF

      Qdata(Qtail,row)=cell(row)
      Qdata(Qtail,col)=cell(col)

      RETURN
      END


C-----------------------------------------------------

      SUBROUTINE deQ(cell)
      INCLUDE "exp_wolff.h"
      INTEGER cell(row:col)
     
      Qsize=Qsize-1
      IF(Qsize .LT. 0) THEN
         WRITE(6,*) 'Error:Underflow Queue' 
         CALL  KABORT(2)
      ENDIF

      cell(row)=Qdata(Qhead,row)
      cell(col)=Qdata(Qhead,col)
      Qhead=Qhead+1
      IF(Qhead .EQ. Qmax)  THEN
         Qhead=0
      ENDIF
      
      RETURN
      END


C-----------------------------------------------------

      SUBROUTINE clearQ
      INCLUDE "exp_wolff.h"
      Qhead=0
      Qtail=-1
      Qsize=0

      RETURN
      END


C-----------------------------------------------------     

      LOGICAL FUNCTION emptyQ(Qsize)
      INTEGER Qsize
      IF(Qsize .EQ. 0) THEN
        emptyQ = .TRUE.
      ELSE
        emptyQ = .FALSE.
      ENDIF

      RETURN
      END




C===============================================================
C
C        Function for Buffer
C
C===============================================================


      SUBROUTINE  putB(Bdata,cell,Bptr)
      INCLUDE "exp_wolff.h"
      INTEGER  Bdata(row:val,0:Bufmax-1), cell(row:val)
      INTEGER  Bptr

      Bptr = Bptr + 1
      IF( Bptr .GE. Bufmax )  THEN
         WRITE(6,*) 'Error: Overflow Buffer !'
         CALL  KABORT(3)
      ENDIF   
   
      Bdata(row,Bptr) = cell(row)
      Bdata(col,Bptr) = cell(col)
      Bdata(val,Bptr) = cell(val)

      RETURN
      END



C----------------------------------------------------------

      SUBROUTINE  getB(Bdata,Bptr,cell)
      INCLUDE "exp_wolff.h"
      INTEGER  Bdata(row:val,0:Bufmax-1), cell(row:val)
      INTEGER  Bptr

      IF( Bptr .LT. 0)  THEN   
         WRITE(6,*) 'Error: Underflow Buffer !'
         CALL  KABORT(4)
      ENDIF

      cell(row)  = Bdata(row,Bptr)
      cell(col)  = Bdata(col,Bptr)
      cell(val)  = Bdata(val,Bptr)

      Bptr = Bptr - 1

      RETURN
      END


C---------------------------------------------------------

      LOGICAL FUNCTION  emptyB(Bptr)
      INTEGER Bptr
 
      IF(Bptr .EQ. -1)  THEN
         emptyB = .TRUE.
      ELSE
         emptyB = .FALSE.
      ENDIF

      RETURN
      END


C---------------------------------------------------------

      SUBROUTINE set_ptrB(Bptr,p)
      INTEGER  Bptr,p

      Bptr = p

      RETURN
      END
 

C---------------------------------------------------------

      SUBROUTINE clearB
      INCLUDE "exp_wolff.h"

      send_Lptr = -1
      send_Rptr = -1
      recv_Lptr = -1
      recv_Rptr = -1

      RETURN
      END

C---------------------------------------------------------

      INTEGER FUNCTION  sizeB(Bptr)
      INTEGER Bptr

      sizeB = Bptr + 1

      RETURN
      END


C----------------------------------------------------------

      LOGICAL FUNCTION lbound(j)
      INCLUDE "exp_wolff.h"
      INTEGER j
       
      lbound=(MOD(j,width) .EQ. 0)

      RETURN
      END


C----------------------------------------------------------

      LOGICAL FUNCTION rbound(j)
      INCLUDE "exp_wolff.h"
      INTEGER j
       
      rbound=(MOD(j,width) .EQ. width-1)

      RETURN
      END


C----------------------------------------------------------

      INTEGER FUNCTION int_ran()
      INCLUDE "exp_wolff.h"
             
      int_ran=ranmar()*N

      RETURN
      END


C----------------------------------------------------------

      LOGICAL FUNCTION Log2(n)
      INTEGER n,pow

      pow=1
      DO WHILE (pow .LT. n)
         pow=pow*2
      ENDDO
   
      IF(pow .EQ. n) THEN      
         Log2=.TRUE.
      ELSE
         Log2=.FALSE.
      ENDIF

      RETURN
      END





