degate  0.1.2
IrregularGrid.cc
Go to the documentation of this file.
00001 /*
00002 
00003  This file is part of the IC reverse engineering tool degate.
00004 
00005  Copyright 2008, 2009, 2010 by Martin Schobert
00006 
00007  Degate is free software: you can redistribute it and/or modify
00008  it under the terms of the GNU General Public License as published by
00009  the Free Software Foundation, either version 3 of the License, or
00010  any later version.
00011 
00012  Degate is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU General Public License for more details.
00016 
00017  You should have received a copy of the GNU General Public License
00018  along with degate. If not, see <http://www.gnu.org/licenses/>.
00019 
00020  */
00021 
00022 #include "IrregularGrid.h"
00023 
00024 #include <assert.h>
00025 #include <stdlib.h>
00026 
00027 using namespace degate;
00028 
00029 int IrregularGrid::snap_to_grid(int pos) const {
00030 
00031         if(pos <= grid_offsets.front()) return grid_offsets.front();
00032         else if(pos >= grid_offsets.back()) return grid_offsets.back();
00033     else {
00034 
00035                 int last = grid_offsets.front();
00036                 for(grid_iter it = grid_offsets.begin(); it != grid_offsets.end(); ++it) {
00037                         int current = *it;
00038                         if(current < pos) last = current;
00039                         else if(current >= pos) {
00040                                 int d1 = abs(pos - last);
00041                                 int d2 = abs(current - pos);
00042                                 return d1 < d2 ? last : current;
00043                         }
00044                 }
00045 
00046                 // should not reach this line
00047                 assert(1 == 0);
00048     }
00049 
00050 }