#include "Dlist.h"

char *FindElm(RemotePointer head, MPI_Win win, const char *key)
{
 static ListElm local_copy;
 ListElm        *local_copy_ptr;
 RemotePointer  ptr;
 int            myrank, *attrval, flag;
 MPI_Group      win_group;

 MPI_Win_get_attr(win, MPE_LISTWIN_KEY_RANK, &attrval, &flag);
 if (!flag) {
   /* win not properly initialized */
   return 0;
 }
 myrank = (int)(MPI_Aint)attrval;  /* We store the rank in
                the attrval, which is an address-sized value */

 ptr = head;
 MPI_Win_lock_all(0, win);
 while (ptr.rank >= 0) {
   /* Make sure we have the data */
   if (ptr.rank != myrank) {
       MPI_Get(&local_copy, 1, listelmType,
               ptr.rank, ptr.disp, 1, listelmType, win);
       MPI_Win_flush(ptr.rank, win);
       local_copy_ptr = &local_copy;
   } else
       local_copy_ptr = (ListElm *)(ptr.local_pointer);

   if (strcmp(local_copy_ptr->key, key) == 0) {
       MPI_Win_unlock_all(win);
       return local_copy_ptr->value;
   }
   ptr = local_copy_ptr->next;
 }
 MPI_Win_unlock_all(win);
 return 0;  /* Did not find key */
}