如何用bitset储存未知长度的序列?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用bitset储存未知长度的序列?相关的知识,希望对你有一定的参考价值。

我有一段未知长度的字符串(DNA序列),只含有ATGC四种字符。为了节省内存,我想用bitset中的2个bit表示一个字符。但是bitset的位数需要在编译时就确定,我在程序运行时,才会知道输入序列的 长度,请问该怎么解决这个问题呢??
看下面程序,我本来想在构造函数中确定size大小,进而得到bitset的,但是bitset后面的长度值要求是常量或静态变量。不能通过编译啊。
class bitseq
public:
int size;
bitset <size> seq;
bitseq (int &sequence_size)
size = sequence_size;

class bitseq
public:
int size;
bitset <2> seq; //这个<>中要填的是每个元素的二进制长度,不是字符串的长度
bitseq (int &sequence_size)
size = sequence_size;

参考技术A /**
* 请不要用于商业行为
**/

#ifndef SEQ_HH
#define SEQ_HH

#include <vector>
#include <deque>
#include <ext/hash_map>

template< typename ValueType,
typename HashFcn = __gnu_cxx::hash< ValueType >,
typename EqualKey = std::equal_to< ValueType >
>
class Sequence
public:
typedef ValueType value_type;
typedef HashFcn hasher;
typedef EqualKey key_equal;

private:
struct node;
typedef __gnu_cxx::hash_map< value_type, node*, hasher, key_equal > nodemap;

struct node
value_type* v;
node* parent;
nodemap childs;
size_t refcnt;

static node* create_node(node* p)
node* n = new node;
n->v = NULL; n->parent = p; n->refcnt = 0;
return n;

static void remove_node(node* n)
if(n && n->refcnt == 0 && n->childs.empty())
node* p = n->parent;
if(p) assert(n->v); p->remove(*(n->v));
delete n;
if(p) remove_node(p);
else Sequence::mgr.root = NULL;


void remove(value_type const & v)
assert(childs.find(v) != childs.end());
childs.erase(v);

node* next(value_type const& v)
typename nodemap::iterator i = childs.find(v);
if(i != childs.end()) return i->second;
node* n = create_node(this);
n->v = &(childs.insert(typename nodemap::value_type(v,n)).first->first);
return n;

;

struct manager
node* root;

manager() : root(NULL)
node* create(value_type const& v)
if(!root) root = node::create_node(NULL);
return root->next(v);

;

static manager mgr;

node* current;
size_t size;

typedef std::vector<node*> iterationpool;
mutable iterationpool iteration;
void update_iterationpool() const
size_t sz = iteration.size();
if(sz == size) return;
while(sz > size) iteration.pop_back(); --sz;
if(sz < size)
iteration.resize(size, NULL);
node* n = current;
size_t z = size;
while(sz < z)
iteration[z-1] = n;
n = n->parent;
--z;



static const size_t end_pos = (size_t)-1;
public:
Sequence() : current(NULL), size(0)
Sequence(value_type const & v)
: current(mgr.create(v)), size(1) ++current->refcnt;
Sequence(value_type const & v, Sequence const & s)
: current(s.current->next(v)), size(s.size + 1) ++current->refcnt;
typedef std::deque<value_type> deque;
Sequence(deque const& q)
: current(NULL), size(q.size())
for (size_t i = 0; i < size; ++i)
current = current ? current->next(q[i]) : mgr.create(q[i]);

++current->refcnt;

Sequence(Sequence const& o)
: current(o.current), size(o.size) ++current->refcnt;
Sequence& operator=(Sequence const& o)
if(current != o.current)
if(current) clear();
if(o.current) current = o.current; size = o.size; ++current->refcnt;

return *this;

~Sequence() clear();

void clear() if(current) --current->refcnt; node::remove_node(current); current = NULL; size = 0;
void push(value_type const& v)
if(!current) current = mgr.create(v); ++size; ++current->refcnt;
else node* n = current->next(v); --current->refcnt;
current = n; ++current->refcnt; ++size;

void pop()
assert(current);
node* n = current->parent; --current->refcnt;
++n->refcnt; node::remove_node(current);
current = n; --size;

size_t length() const return size;

class const_iterator
friend class Sequence;
void update()
seq->update_iterationpool();
if(pos >= seq->size && pos != end_pos) pos = end_pos;

protected:
const_iterator(const Sequence* s, size_t p) : seq(s), pos(p) update();
const Sequence* seq;
size_t pos;
public:
typedef const value_type const_value_type;
typedef value_type & reference;
typedef const value_type & const_reference;
typedef value_type * pointer;
typedef const value_type * const_pointer;

const_reference operator *() const assert(seq->iteration.size() > pos);
return *(seq->iteration[pos]->v);
const_pointer operator->() const return &(**this);

bool operator == (const_iterator const & o) const return seq == o.seq && pos == o.pos;
bool operator != (const_iterator const & o) const return !(*this == o);
bool operator < (const_iterator const & o) const assert(seq == o.seq); return pos < o.pos;

const_iterator operator ++(int)
size_t p = pos;
++pos; update();
return const_iterator(seq, pos);

const_iterator operator --(int)
size_t p = pos;
if(pos == end_pos) pos = seq->size;
--pos; update();
return const_iterator(seq, pos);

const_iterator& operator ++() ++pos; update(); return *this;
const_iterator& operator --() if(pos == end_pos) pos = seq->size;
--pos; update(); return *this;
;

const_iterator begin() const return const_iterator(this, 0);
const_iterator end() const return const_iterator(this, end_pos);

bool operator == (Sequence const& s) const return current == s.current;
bool operator != (Sequence const& s) const return !(*this == s);

void clear_cache() const iteration.clear();
const value_type& operator[] (size_t const & i) const update_iterationpool(); return *(iteration[i]->v);
size_t hash() const return (size_t)current;
size_t count(value_type const & v) const
update_iterationpool();
size_t res = 0;
for (size_t i = 0; i < iteration.size(); ++i)
if(*(iteration[i]->v) == v) ++res;

return res;

;

template< typename V, typename H, typename E >
typename Sequence< V, H, E >::manager
Sequence< V, H, E >::mgr;

namespace __gnu_cxx

template< typename V, typename H, typename E >
struct hash< Sequence<V,H,E> >
size_t operator()(Sequence<V,H,E> const& s) const return s.hash();
;

#endif

如何用php把一个未知结构的数组写入到一个文本文件,

如何用php把一个未知结构的数组写入到一个文本文件,然后从文本文件再恢复出来!

参考技术A // 文件名称
$cache_file = dirname(__FILE__).'/data.txt';
$data = array(5=>5,9=>8);
// 创建数据文件写入信息
$cache_handle = fopen($cache_file,'w+');
$result = fwrite($cache_handle,serialize($data));
fclose($cache_handle);
// 读取数据文件内容输入
$cache = file_get_contents($cache_file);
print_r(unserialize($cache));本回答被提问者采纳

以上是关于如何用bitset储存未知长度的序列?的主要内容,如果未能解决你的问题,请参考以下文章

解压缩具有可变长度的未知序列化格式

如何用 pandas 处理不同时间序列的变长?

如何用相同长度的唯一数字替换特定长度的字符

如何用SQL字符长度查询?

达梦如何用dm管理工具箱修改字段长度

如何用固定长度和许多元素编写这个正则表达式