00001 /* 00002 * lib/sourcepos/spcache.cc 00003 * 00004 * Classes for sophisticated source position (file/line/lpos, 00005 * include hierarchy) handling. 00006 * 00007 * Copyright (c) 2003--2004 by Wolfgang Wieser ] wwieser (a) gmx <*> de [ 00008 * 00009 * This file may be distributed and/or modified under the terms of the 00010 * GNU General Public License version 2 as published by the Free Software 00011 * Foundation. (See COPYING.GPL for details.) 00012 * 00013 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00014 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00015 * 00016 */ 00017 00018 #include "spcache.h" 00019 00021 #include <stdio.h> 00022 00023 00024 SourcePosition SourcePositionCache::find(int line,int lpos) 00025 { 00026 SourcePosition sp; 00027 00028 // [Need not check for top<0 here, the for() loop is safe.] 00029 // Do a linear lookup. A binary search or something like that 00030 // would just be overkill at the applied cache size of 4..8. 00031 for(int i=cache_size; i>0; i--) // <-- CORRECT!! 00032 { 00033 const SourcePosition &spi=(cache[(top+i)%cache_size]); 00034 if(!spi) break; 00035 if(spi->line()==line && spi->lpos()==lpos) 00036 { sp=spi; ++hits; return(sp); } 00037 } 00038 00039 ++misses; 00040 return(sp); 00041 } 00042 00043 00044 SourcePositionCache::SourcePositionCache() 00045 { 00046 top=-1; // correct 00047 00048 hits=0; 00049 misses=0; 00050 } 00051 00052 SourcePositionCache::~SourcePositionCache() 00053 { 00054 // This is debug; should be commented out. 00055 fprintf(stderr,"SourcePositionCache: hits=%d, misses=%d\n", 00056 hits,misses); 00057 }