hashmap.c

00001 /***************************************************************************
00002                           hashmap.c  -  description
00003                              -------------------
00004     begin                : Fri Feb 20 2004
00005     copyright            : (C) 2004 by Dynacube Team
00006     email                : 
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018  #include "common/ds/hashmap.h"
00019  
00020   PAIR win_map[MAX_PROC][WINDOW_MAX];
00021   PAIR frm_map[MAX_PROC][FRAME_MAX];
00022   PAIR comp_map[MAX_PROC][COMPONENT_MAX];
00023 
00024   DW win_map_max[MAX_PROC] = {0};
00025   DW frm_map_max[MAX_PROC] = {0};
00026   DW comp_map_max[MAX_PROC]= {0};
00027 
00028   void hash_init()
00029   {
00030     DW i, j;
00031 
00032     for(i = 0 ; i < MAX_PROC ; i++)
00033     {
00034       for(j = 0 ; j < WINDOW_MAX ; j++)
00035       {
00036        win_map[i][j].avl  =  AVL;
00037       }
00038 
00039       for(j = 0 ; j < FRAME_MAX ; j++)
00040       {
00041         frm_map[i][j].avl = AVL;
00042       }
00043       
00044       for(j = 0 ; j < COMPONENT_MAX ; j++)
00045       {
00046         comp_map[i][j].avl = AVL;
00047       }      
00048     }
00049     
00050   }
00051   
00052   SDW hadd(DW pid, DB type, DW key, DW val)
00053   {
00054    PAIR *p;
00055    DW max1;
00056    DW *max2;  
00057    DW i;
00058    
00059    switch(type)
00060    {
00061     case WIND_TYPE:
00062           max1 = WINDOW_MAX;
00063           max2 = &win_map_max[pid];
00064           p = win_map[pid];
00065           break;
00066           
00067     case FRM_TYPE:
00068           max1 = FRAME_MAX;
00069           max2 = &frm_map_max[pid];
00070           p = frm_map[pid];    
00071           break;
00072           
00073     case COMP_TYPE:
00074           max1 = COMPONENT_MAX;
00075           max2 = &comp_map_max[pid];
00076           p = comp_map[pid];    
00077           break;
00078           
00079     default:
00080           return -1;
00081    }
00082 
00083               if(*max2 < max1)
00084               {
00085                 p[*max2].avl = UNAVL;
00086                 p[*max2].key = key;
00087                 p[*max2].val = val;
00088                 *max2 = *max2 + 1;
00089                 return 0;
00090               }
00091               else
00092               {
00093                 for(i = 0 ; i < max1 ; i++)
00094                 {
00095                  if(p[i].avl == AVL)
00096                  {
00097                   p[i].avl = UNAVL;
00098                   p[i].key = key;
00099                   p[i].val = val;
00100                   return 0;
00101                  }
00102                 }
00103                 
00104                 if( i == max1)
00105                   return -1;
00106               }
00107     
00108   }
00109   
00110   SDW hremove(DW pid, DB type, DW key)
00111   {
00112    PAIR *p;
00113    DW *max2;
00114    DW i;
00115     
00116    switch(type)
00117    {
00118     case WIND_TYPE:
00119           max2 = &win_map_max[pid];
00120           p = win_map[pid];
00121           break;
00122 
00123     case FRM_TYPE:
00124           max2 = &frm_map_max[pid];
00125           p = frm_map[pid];
00126           break;
00127 
00128     case COMP_TYPE:
00129           max2 = &comp_map_max[pid];
00130           p = comp_map[pid];
00131           break;
00132    }
00133 
00134    for(i = 0 ; i < *max2 ; i++)
00135    {
00136      if(p[i].avl == UNAVL)
00137      {
00138        if(p[i].key == key)
00139        {
00140         p[i].avl = AVL;
00141         return 0;
00142        }
00143      }
00144    }
00145 
00146    return -1;    
00147   }
00148   
00149   SDW  hfind(DW pid, DB type, DW key) //Return read_id or -1
00150   {
00151    PAIR *p;
00152    DW *max2;
00153    DW i;
00154     
00155    switch(type)
00156    {
00157     case WIND_TYPE:
00158           max2 = &win_map_max[pid];
00159           p = win_map[pid];
00160           break;
00161 
00162     case FRM_TYPE:
00163           max2 = &frm_map_max[pid];
00164           p = frm_map[pid];
00165           break;
00166 
00167     case COMP_TYPE:
00168           max2 = &comp_map_max[pid];
00169           p = comp_map[pid];
00170           break;
00171    }
00172 
00173    for(i = 0 ; i < *max2 ; i++)
00174    {
00175      if(p[i].avl == UNAVL)
00176      {
00177        if(p[i].key == key)
00178         return p[i].val;
00179      }
00180    }
00181    
00182    return -1;
00183        
00184   }
00185 
00186   SDW  afind(DW pid, DB type, DW val) //Return alias_id or -1
00187   {
00188    PAIR *p;
00189    DW *max2;
00190    DW i;
00191 
00192    switch(type)
00193    {
00194     case WIND_TYPE:
00195           max2 = &win_map_max[pid];
00196           p = win_map[pid];
00197           break;
00198 
00199     case FRM_TYPE:
00200           max2 = &frm_map_max[pid];
00201           p = frm_map[pid];
00202           break;
00203 
00204     case COMP_TYPE:
00205           max2 = &comp_map_max[pid];
00206           p = comp_map[pid];
00207           break;
00208    }
00209 
00210    for(i = 0 ; i < *max2 ; i++)
00211    {
00212      if(p[i].avl == UNAVL)
00213      {
00214        if(p[i].val == val)
00215         return p[i].key;
00216      }
00217    }
00218 
00219    return -1;
00220 
00221   }
00222   
00223   void printHash(DW pid, DB type)
00224   {
00225     PAIR *p;
00226     DW *max2;
00227     DW i;
00228 
00229    printf("\n");
00230     
00231    switch(type)
00232    {
00233     case WIND_TYPE:
00234           max2 = &win_map_max[pid];
00235           p = win_map[pid];
00236           break;
00237 
00238     case FRM_TYPE:
00239           max2 = &frm_map_max[pid];
00240           p = frm_map[pid];
00241           break;
00242 
00243     case COMP_TYPE:
00244           max2 = &comp_map_max[pid];
00245           p = comp_map[pid];
00246           break;
00247    }
00248    
00249    for(i = 0 ; i < *max2 ; i++)
00250    {
00251      if(p[i].avl == UNAVL)
00252      {
00253        printf(" (%d) %d ",p[i].key,p[i].val);
00254      }
00255    }
00256         
00257   }
00258 
00259   void hremoveAll(DW pid)
00260   {
00261     win_map_max[pid] = 0;
00262     frm_map_max[pid] = 0;
00263     comp_map_max[pid] = 0;    
00264   }
00265   

Generated on Thu Jul 27 23:52:26 2006 for Dynacube by  doxygen 1.4.7