eg_perm_it.c

Go to the documentation of this file.
00001 /* EGlib "Efficient General Library" provides some basic structures and
00002  * algorithms commons in many optimization algorithms.
00003  *
00004  * Copyright (C) 2005 Daniel Espinoza and Marcos Goycoolea.
00005  * 
00006  * This library is free software; you can redistribute it and/or modify it
00007  * under the terms of the GNU Lesser General Public License as published by the
00008  * Free Software Foundation; either version 2.1 of the License, or (at your
00009  * option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful, but 
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
00013  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
00014  * License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this library; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
00019  * */
00020 /* ========================================================================= */
00021 /** @file 
00022  * @ingroup EGpermIt */
00023 /** @addtogroup EGpermIt */
00024 /** @{ */
00025 #include "eg_perm_it.h"
00026 /* ========================================================================= */
00027 #if __PERM_IT_NO_INLINE__
00028 int EGpermItNext(EGpermIt_t*const prit)
00029 {
00030   int*const c = prit->c_focus;
00031   int*const o = prit->o_focus;
00032   int*const arr = prit->tuple; 
00033   int tmp, j=prit->sz-1, s=0, pos=0;
00034   int  q=c[j]+o[j];
00035   /* find where we should make the following switch */
00036   while(q<0 || q>j)
00037   {
00038     if(q>j)
00039     {
00040       if (!j) return 0;
00041       s+=1;
00042     }
00043     o[j]=-o[j];
00044     j-=1;
00045     q=c[j]+o[j];
00046   }
00047   /* now we just switch positions j-c_j+s and j-q+s */
00048   prit->changed_pos = pos = c[j] > q ? j-c[j]+s:j-q+s;
00049   c[j] = q;
00050   tmp = arr[pos];
00051   arr[pos] = arr[pos+1];
00052   arr[pos+1] = tmp;
00053   /* and we are done */
00054   return 1;
00055 }
00056 #endif
00057   
00058 /* ========================================================================= */
00059 /** @} */
00060 /* end of eg_gcit.c */
00061