📄 mplex.ccp
字号:
}void <T>MPlex::cache(int idx) const{ const <T>MChunk* tail = (<T>MChunk*)tl(); const <T>MChunk* t = ch; while (idx >= t->fence_index()) { if (t == tail) index_error(); t = ((<T>MChunk*)(t->next())); } while (idx < t->low_index()) { if (t == (<T>MChunk*)hd) index_error(); t = ((<T>MChunk*)(t->prev())); } if (!t-><T>MChunk::valid_index(idx)) index_error(); set_cache(t);}void <T>MPlex::cache(const <T>* p) const{ const <T>MChunk* old = ch; const <T>MChunk* t = ch; while (!t->actual_pointer(p)) { t = ((<T>MChunk*)(t->next())); if (t == old) index_error(); } if (!t-><T>MChunk::valid_pointer(p)) index_error(); set_cache(t);}int <T>MPlex::owns(Pix px) const{ <T>* p = (<T>*)px; const <T>MChunk* old = ch; const <T>MChunk* t = ch; while (!t->actual_pointer(p)) { t = ((<T>MChunk*)(t->next())); if (t == old) return 0; } set_cache(t); return t-><T>MChunk::valid_pointer(p);}int <T>MPlex::add_high(const <T&> elem){ <T>MChunk* t = ((<T>MChunk*) tl()); if (!t->can_grow_high()) { <T>* data = new <T> [csize]; t = (new <T>MChunk(data, fnc,fnc,fnc,fnc+csize)); t->link_to_prev(tl()); } *((t-><T>MChunk::grow_high())) = elem; set_cache(t); return fnc++;}int <T>MPlex::add_low (const <T&> elem){ <T>MChunk* t = ((<T>MChunk*) hd); if (!t->can_grow_low()) { <T>* data = new <T> [csize]; hd = new <T>MChunk(data, lo-csize, lo, lo, lo); hd->link_to_next(t); t = ((<T>MChunk*) hd); } *((t-><T>MChunk::grow_low())) = elem; set_cache(t); return --lo;}void <T>MPlex::adjust_bounds(){ <T>MChunk* t = ((<T>MChunk*) tl()); // clean up tail t->reset_high(); while (t-><T>MChunk::empty() && !one_chunk()) { <T>MChunk* pred = (<T>MChunk*)(t->prev()); del_chunk(t); pred->reset_high(); t = (pred); } if (one_chunk()) t->reset_high(); int oldfnc = fnc; fnc = t->fence_index(); unused -= oldfnc - fnc; // and head.. t = ((<T>MChunk*) hd); t->reset_low(); while (t-><T>MChunk::empty() && !one_chunk()) { hd = (<T>MChunk*)(t->next()); del_chunk(t); t = ((<T>MChunk*) hd); t->reset_low(); } int oldlo = lo; lo = t->low_index(); unused -= lo - oldlo; set_cache(t);}int <T>MPlex::del_high (){ if (empty()) empty_error(); <T>MChunk* t = ((<T>MChunk*) tl()); while (t-><T>MChunk::empty() && !one_chunk()) // possible stragglers { <T>MChunk* pred = (<T>MChunk*)(t->prev()); del_chunk(t); pred->reset_high(); t = (pred); } t-><T>MChunk::shrink_high(); while (t-><T>MChunk::empty() && !one_chunk()) { <T>MChunk* pred = (<T>MChunk*)(t->prev()); del_chunk(t); pred->reset_high(); t = (pred); } int oldfnc = fnc; fnc = t->fence_index(); unused -= oldfnc - fnc - 1; set_cache(t); return fnc - 1;}int <T>MPlex::del_low (){ if (empty()) empty_error(); <T>MChunk* t = ((<T>MChunk*) hd); while (t-><T>MChunk::empty() && !one_chunk()) { hd = (<T>MChunk*)(t->next()); del_chunk(t); t = ((<T>MChunk*) hd); t->reset_low(); } t-><T>MChunk::shrink_low(); while (t-><T>MChunk::empty() && !one_chunk()) { hd = (<T>MChunk*)(t->next()); del_chunk(t); t = ((<T>MChunk*) hd); t->reset_low(); } int oldlo = lo; lo = t->low_index(); unused -= lo - oldlo - 1; set_cache(t); return lo;}int <T>MPlex::add(const <T&> elem){ if (unused == 0) return add_high(elem); for(<T>MChunk* t = ch; t->unused_indices() == 0; t = (<T>MChunk*)(t->prev())) ; int i = t->unused_index(); set_cache(t); undel_index(i); (*this)[i] = elem; return i;}int <T>MPlex::unused_index() const{ if (unused == 0) index_error(); for(<T>MChunk* t = ch; t->unused_indices() == 0; t = (<T>MChunk*)(t->prev())) ; set_cache(t); return t->unused_index();}Pix <T>MPlex::unused_Pix() const{ if (unused == 0) return 0; for(<T>MChunk* t = ch; t->unused_indices() == 0; t = (<T>MChunk*)(t->prev())) ; set_cache(t); return t->pointer_to(t->unused_index()); }int <T>MPlex::del_index(int idx){ if (idx < lo || idx >= fnc) index_error(); if (<T>MPlex::valid(idx)) { ++unused; ch-><T>MChunk::del(idx); return 1; } else return 0;}int <T>MPlex::dopred(int idx) const{ if (idx >= fnc) idx = fnc; if (idx <= lo) return lo - 1; const <T>MChunk* t = ch; while (idx > t->fence_index()) { t = ((<T>MChunk*)(t->next())); } while (idx <= t->low_index()) { t = ((<T>MChunk*)(t->prev())); } int i = t-><T>MChunk::pred(idx); while (i < t->low_index() && i >= lo) { t = ((<T>MChunk*)(t->prev())); i = t-><T>MChunk::last_index(); } set_cache(t); return i;}int <T>MPlex::dosucc(int idx) const{ if (idx < lo) idx = lo; if (idx >= fnc - 1) return fnc; const <T>MChunk* t = ch; while (idx >= t->fence_index()) { t = ((<T>MChunk*)(t->next())); } while (idx < t->low_index()) { t = ((<T>MChunk*)(t->prev())); } int i = t-><T>MChunk::succ(idx); while (i >= t->fence_index() && i < fnc) { t = (<T>MChunk*)(t->next()); i = t-><T>MChunk::first_index(); } set_cache(t); return i;}void <T>MPlex::prev(Pix& i) const{ if (i == 0) return; <T>* p = (<T>*) i; const <T>MChunk* old = ch; const <T>MChunk* t = ch; while (!t->actual_pointer(p)) { t = ((<T>MChunk*)(t->prev())); if (t == old) { i = 0; return; } } <T>* q = t-><T>MChunk::pred(p); while (q == 0 && t != (<T>MChunk*)hd) { t = ((<T>MChunk*)(t->prev())); q = t-><T>MChunk::last_pointer(); } i = Pix(q); set_cache(t); return; }void <T>MPlex::next(Pix& i) const{ if (i == 0) return; <T>* p = (<T>*) i; const <T>MChunk* tail = (<T>MChunk*)(tl()); const <T>MChunk* old = ch; const <T>MChunk* t = ch; while (!t->actual_pointer(p)) { t = ((<T>MChunk*)(t->next())); if (t == old) { i = 0; return; } } <T>* q = t-><T>MChunk::succ(p); while (q == 0 && t != tail) { t = ((<T>MChunk*)(t->next())); q = t-><T>MChunk::first_pointer(); } i = Pix(q); set_cache(t); return; } void <T>MPlex::undel_index(int idx){ if (idx < lo || idx >= fnc) index_error(); <T>MChunk* t = ch; while (idx >= t->fence_index()) { t = ((<T>MChunk*)(t->next())); } while (idx < t->low_index()) { t = ((<T>MChunk*)(t->prev())); } int was_present = t-><T>MChunk::undel(idx); if (!was_present) { --unused; } set_cache(t); return;}void <T>MPlex::clear(){ if (fnc != lo) { <T>MChunk* t = ((<T>MChunk*)tl()); while (t != hd) { <T>MChunk* prv = (<T>MChunk*)(t->prev()); del_chunk(t); t = prv; } t-><T>MChunk::clear(lo); set_cache(t); fnc = lo; unused = 0; }}int <T>MPlex::OK () const{ int v = hd != 0; // at least one chunk int found_ch = 0; // to make sure ch is in list; int count = 0; // to count unused slots const <T>MChunk* t = (<T>MChunk*)(hd); int gap = t->low_index() - lo; v &= gap == 0; // hd lo not less than lo. count += gap; for (;;) { if (t == ch) ++found_ch; v &= t-><T>MChunk::OK(); // each chunk is OK count += t->unused_indices(); if (t == (<T>MChunk*)(tl())) break; else // and has indices less than succ { gap = t->next()->base_index() - t->top_index(); v &= gap == 0; count += gap; if (t != (<T>MChunk*)hd) // internal chunks can't grow v &= !t->can_grow_low() && !t->can_grow_high(); t = (const <T>MChunk*)(t->next()); } } gap = fnc - t->fence_index(); v &= gap == 0; count += gap; v &= count == unused; // chunk counts agree with plex v &= found_ch == 1; if (!v) error("invariant failure"); return v;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -