Skip to content

Commit bed40f3

Browse files
committed
fixed GFastaIndex bug
1 parent 27957fd commit bed40f3

4 files changed

Lines changed: 27 additions & 9 deletions

File tree

gclib/GBase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#ifndef G_BASE_DEFINED
22
#define G_BASE_DEFINED
3-
#define GCLIB_VERSION "0.12.1"
3+
#define GCLIB_VERSION "0.12.2"
44

55
#ifdef HAVE_CONFIG_H
66
#include "config.h"

gclib/GFastaIndex.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ void GFastaIndex::addRecord(const char* seqname, uint seqlen, off_t foffs, int l
2020
else {
2121
farec=new GFastaRec(seqlen,foffs,llen,llen_full);
2222
records.Add(seqname,farec);
23-
//farec->seqname=records.getLastKey();
24-
farec->seqname=seqname;
23+
farec->seqname=records.getLastKey();
2524
}
2625
}
2726

@@ -53,7 +52,7 @@ int GFastaIndex::loadIndex(const char* finame) { //load record info from existin
5352
sscanf(p, "%d%ld%d%d", &len, &offset, &line_len, &line_blen);
5453
#else
5554
long long offset=-1;
56-
sscanf(p, "%d%lld%d%d", &len, &offset, &line_len, &line_blen);
55+
sscanf(p, "%u%lld%d%d", &len, &offset, &line_len, &line_blen);
5756
#endif
5857
if (len==0 || line_len==0 || line_blen==0 || line_blen<line_len)
5958
GError(ERR_FAIDXLINE,p);

gclib/GHashMap.hh

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,23 +129,30 @@ public:
129129
// if you don't want that (keys are shared), just use GHashSet<const char*> instead
130130
template <class Hash=GHashKey_xxHash<const char*>, class Eq=GHashKey_Eq<const char*>, typename khInt_t=uint64_t>
131131
class GStrSet: public GHashSet<const char*, Hash, Eq, khInt_t> {
132+
protected:
133+
const char* lastKey=NULL;
132134
public:
133135
inline int Add(const char* ky) { // return -1 if the key already exists
134136
int absent=-1;
135137
khInt_t i=this->put(ky, &absent);
136138
if (absent==1) {//key was actually added
137139
const char* s=Gstrdup(ky);
138140
this->key(i)=s; //store a copy of the key string
141+
lastKey=s;
139142
return i;
140143
}
141144
//key was already there
142145
return -1;
143146
}
144147

148+
inline const char* getLastKey() { return lastKey; }
149+
145150
int Remove(const char* ky) { //return index being removed, or -1 if no such key exists
146151
khInt_t i=this->get(ky);
147152
if (i!=this->end()) {
148-
GFREE(this->key(i)); //free string copy
153+
const char* s=this->key(i);
154+
if (s==lastKey) lastKey=NULL;
155+
GFREE(s); //free string copy
149156
this->del(i);
150157
return i;
151158
}
@@ -159,6 +166,7 @@ template <class Hash=GHashKey_xxHash<const char*>, class Eq=GHashKey_Eq<const ch
159166
//deallocate string copy
160167
GFREE(this->key(i));
161168
}
169+
lastKey=NULL;
162170
this->clear(); //does not shrink !
163171
}
164172

@@ -175,6 +183,7 @@ template <class Hash=GHashKey_xxHash<const char*>, class Eq=GHashKey_Eq<const ch
175183
};
176184

177185
//generic hash map where keys and values can be of any type
186+
//Warning: keys are always copied (shared), including const char* keys -- no deep copy!
178187
template <class K, class V, class Hash=GHashKey_xxHash<K>, class Eq=GHashKey_Eq<K>, typename khInt_t=uint64_t>
179188
class GHashMap:public std::conditional< is_char_ptr<K>::value,
180189
klib::KHashMapCached< K, V, Hash, Eq, khInt_t>,
@@ -370,7 +379,7 @@ public:
370379
template <class V, class Hash=GHashKey_xxHash<const char*>, class Eq=GHashKey_Eq<const char*>, typename khInt_t=uint64_t >
371380
class GHash:public GHashMap<const char*, V, Hash, Eq, khInt_t> {
372381
protected:
373-
382+
const char* lastKey=NULL;
374383
public:
375384
GHash(bool doFree=true) {
376385
this->freeItems=doFree;
@@ -383,17 +392,23 @@ public:
383392
if (absent==1) { //key was actually added
384393
const char* s=Gstrdup(ky);
385394
this->key(i)=s; //store a copy of the key string
395+
lastKey=s;
386396
this->value(i)=val; //value is always copied
387397
return i;
388398
}
389399
return -1;
390400
}
401+
402+
inline const char* getLastKey() { return lastKey; }
403+
391404
template <typename T=V> inline
392405
typename std::enable_if< std::is_pointer<T>::value, int>::type
393406
Remove(const char* ky) { //return index being removed
394407
khInt_t i=this->get(ky);
395408
if (i!=this->end()) {
396-
GFREE(this->key(i)); //free string copy
409+
const char* s=this->key(i);
410+
if (s==lastKey) lastKey=NULL;
411+
GFREE(s); //free string copy
397412
if (this->freeItems) delete this->value(i);
398413
this->del(i);
399414
return i;
@@ -406,7 +421,9 @@ public:
406421
Remove(const char* ky) { //return index being removed
407422
khInt_t i=this->get(ky);
408423
if (i!=this->end()) {
409-
GFREE(this->key(i)); //free string copy
424+
const char* s=this->key(i);
425+
if (s==lastKey) lastKey=NULL;
426+
GFREE(s); //free string copy
410427
this->del(i);
411428
return i;
412429
}
@@ -422,6 +439,7 @@ public:
422439
if (this->freeItems) delete this->value(i);
423440
GFREE(this->key(i));
424441
}
442+
lastKey=NULL;
425443
this->clear();
426444
}
427445

@@ -433,6 +451,7 @@ public:
433451
if (!this->__kh_used(this->used, i)) continue;
434452
GFREE(this->key(i));
435453
}
454+
lastKey=NULL;
436455
this->clear();
437456
}
438457

gclib/gff.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3415,7 +3415,7 @@ char getOvlCode(GffObj& m, GffObj& r, int& ovlen, bool stricterMatch, int trange
34153415
//if (ref_intron_poking>0 && )
34163416
//we just need to have no intron poking going on
34173417
if (!intron_conflict && ref_intron_poking<4
3418-
&& qry_intron_poking<4) return 'm';
3418+
&& qry_intron_poking<4) return 'm';
34193419
return 'n';
34203420
}
34213421
if (junct_match) return 'j';

0 commit comments

Comments
 (0)