Move tests
[lttoolbox.git] / lttoolbox / sorted_vector.cc
blobdb0374b85a0ec4b43edf61425c3a6ac64e2d3ace
1 /*
2 * Copyright (C) 2005 Universitat d'Alacant / Universidad de Alicante
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17 * 02111-1307, USA.
19 #include <lttoolbox/sorted_vector.h>
20 #include <cstdlib>
22 using namespace std;
24 void
25 SortedVector::copy(SortedVector const &o)
27 sv = new SVNode[o.size];
28 size = o.size;
30 for(int i = 0; i != size; i++)
32 sv[i].tag = o.sv[i].tag;
33 sv[i].dest = o.sv[i].dest;
37 void
38 SortedVector::destroy()
40 delete sv;
43 SortedVector::SortedVector(int const fixed_size)
45 sv = new SVNode[fixed_size];
46 size = fixed_size;
49 SortedVector::~SortedVector()
51 destroy();
54 SortedVector::SortedVector(SortedVector const &o)
56 copy(o);
59 SortedVector &
60 SortedVector::operator =(SortedVector const &o)
62 if(this != &o)
64 destroy();
65 copy(o);
67 return *this;
70 void
71 SortedVector::add(int tag, MatchNode *dest, int pos)
73 sv[pos].tag = tag;
74 sv[pos].dest = dest;
77 MatchNode *
78 SortedVector::search(int tag)
80 int left = 0, right = size-1;
81 while(left <= right)
83 int mid = (left+right)/2;
84 if(sv[mid].tag == tag)
86 return sv[mid].dest;
88 if(sv[mid].tag > tag)
90 right = mid - 1;
92 else
94 left = mid + 1;
98 return NULL;