| 1 | // This file is part of A2Amapper. |
| 2 | // Copyright (c) 2005 J. Craig Venter Institute |
| 3 | // Author: Brian Walenz |
| 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 (LICENSE.txt) a copy of the GNU General Public |
| 16 | // License along with this program; if not, write to the Free Software |
| 17 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | |
| 19 | #include "overlap.H" |
| 20 | |
| 21 | matchTree::matchTree(atacMatchList *L, uint32 side) { |
| 22 | |
| 23 | // Construct a list of pointers to the atacMatchList data |
| 24 | // |
| 25 | // kazlib was modified to be qsort() compatible and so it passes a |
| 26 | // pointer to whatever it is sorting. Since kazlib operates on |
| 27 | // pointers anyway, this means that it passes the compare function |
| 28 | // a pointer to a pointer to the object. |
| 29 | // |
| 30 | // Which really fails in this case. We have a list of pointers to |
| 31 | // objects that we sort, then want to load. |
| 32 | // |
| 33 | // Uhhh, no, this is correct. We give kazlib a pointer to the |
| 34 | // object, it gives the compare function a pointer to that pointer. |
| 35 | // |
| 36 | // qsort() below sorts pointers to objects, and does the same. |
| 37 | |
| 38 | atacMatch **matchPointers = new atacMatch * [L->numberOfMatches()]; |
| 39 | for (uint32 i=0; i<L->numberOfMatches(); i++) |
| 40 | matchPointers[i] = L->getMatch(i); |
| 41 | |
| 42 | // Choose a comparison function based on the side we want |
| 43 | |
| 44 | int (*sortMatches)(const void *, const void *) = sortMatches1; |
| 45 | if (side == 1) |
| 46 | sortMatches = sortMatches2; |
| 47 | |
| 48 | // Sort |
| 49 | |
| 50 | qsort(matchPointers, L->numberOfMatches(), sizeof(atacMatch *), sortMatches); |
| 51 | |
| 52 | // Load the tree (use DICTCOUNT_T_MAX for max nodes) |
| 53 | |
| 54 | _tree = dict_create(L->numberOfMatches(), sortMatches); |
| 55 | dict_allow_dupes(_tree); |
| 56 | |
| 57 | dict_load_begin(&_load, _tree); |
| 58 | |
| 59 | for (uint32 i=0; i<L->numberOfMatches(); i++) { |
| 60 | dnode_t *node = (dnode_t *)malloc(sizeof(dnode_t)); |
| 61 | dnode_init(node, 0L); |
| 62 | dict_load_next(&_load, node, matchPointers[i]); |
| 63 | } |
| 64 | |
| 65 | dict_load_end(&_load); |
| 66 | |
| 67 | // Clean up |
| 68 | delete [] matchPointers; |
| 69 | } |