diff options
| author | Henrik Rydberg <rydberg@euromail.se> | 2011-10-08 20:30:28 +0200 |
|---|---|---|
| committer | Henrik Rydberg <rydberg@euromail.se> | 2011-10-08 20:30:28 +0200 |
| commit | 5df79c53745fde5d6c3340a2979b1429cd5892c1 (patch) | |
| tree | 1a81af141708b826e9c61e8a04019994fcca8298 /src/hdb/sortable.cc | |
Initial import of htcd system 1.0
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Diffstat (limited to 'src/hdb/sortable.cc')
| -rw-r--r-- | src/hdb/sortable.cc | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/hdb/sortable.cc b/src/hdb/sortable.cc new file mode 100644 index 0000000..39adf58 --- /dev/null +++ b/src/hdb/sortable.cc | |||
| @@ -0,0 +1,81 @@ | |||
| 1 | /************************************************************************* | ||
| 2 | * | ||
| 3 | * HTCd - Copyright (C) 1998-2006 Henrik Rydberg | ||
| 4 | * | ||
| 5 | * This program is free software; you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License as published by | ||
| 7 | * the Free Software Foundation; either version 2 of the License, or | ||
| 8 | * (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This program is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU General Public License | ||
| 16 | * along with this program; if not, write to the Free Software | ||
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 18 | */ | ||
| 19 | |||
| 20 | #include <hdb/sortable.h> | ||
| 21 | #include <algorithm> | ||
| 22 | using namespace std; | ||
| 23 | |||
| 24 | int int_stream::appc(char c) { return val=c,1; } | ||
| 25 | int int_stream::appi(int i) { return val=i,1; } | ||
| 26 | int int_stream::appf(double f) { return val=(int)f,1; } | ||
| 27 | int int_stream::apps(const sref& s) { return val=atoi(s),s.size(); } | ||
| 28 | |||
| 29 | /////////////////////////////////////////////////// | ||
| 30 | |||
| 31 | record_t::record_t(const spile& list) | ||
| 32 | { | ||
| 33 | pos[0].set(0); | ||
| 34 | for(int i=0;i<list.size();i++) { | ||
| 35 | memcpy(data+pos[i].get(),list[i].data(),list[i].size()); | ||
| 36 | pos[i+1].set(pos[i].get()+list[i].size()); | ||
| 37 | } | ||
| 38 | for(int i=list.size();i<maxtabs;i++) pos[i+1]=pos[i]; | ||
| 39 | } | ||
| 40 | |||
| 41 | void record_t::set(const spile& list) | ||
| 42 | { | ||
| 43 | pos[0].set(0); | ||
| 44 | for(int i=0;i<list.size();i++) { | ||
| 45 | memcpy(data+pos[i].get(),list[i].data(),list[i].size()); | ||
| 46 | pos[i+1].set(pos[i].get()+list[i].size()); | ||
| 47 | } | ||
| 48 | for(int i=list.size();i<maxtabs;i++) pos[i+1]=pos[i]; | ||
| 49 | } | ||
| 50 | |||
| 51 | void record_t::resize(int col,int n) | ||
| 52 | { | ||
| 53 | int delta=n+pos[col].get()-pos[col+1].get(); | ||
| 54 | if(delta) { | ||
| 55 | memmove(data+pos[col+1].get()+delta,data+pos[col+1].get(), | ||
| 56 | pos[maxtabs].get()-pos[col+1].get()); | ||
| 57 | for(int i=col+1;i<=maxtabs;i++) pos[i].add(delta); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | /////////////////////////////////////////////////// | ||
| 62 | |||
| 63 | int indexmap::get(const sref& t) const | ||
| 64 | { | ||
| 65 | pair<const_iterator,const_iterator> p=equal_range(begin(),end(),idx_t(t,0),comp); | ||
| 66 | return p.first!=p.second?p.first-begin():-1; | ||
| 67 | } | ||
| 68 | |||
| 69 | int indexmap::add(const sref& t,record_t* rec) | ||
| 70 | { | ||
| 71 | idx_t tmp(t,rec); | ||
| 72 | pair<iterator,iterator> p=equal_range(begin(),end(),tmp,comp); | ||
| 73 | if(p.first!=p.second) return &(*p.first=tmp)-begin(); | ||
| 74 | else return insert(p.second,tmp)-begin(); | ||
| 75 | } | ||
| 76 | |||
| 77 | void indexmap::sort(const op_t* op) | ||
| 78 | { | ||
| 79 | comp.func=op->f2; | ||
| 80 | ::sort(begin(),end(),comp); | ||
| 81 | } | ||
