⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 skipbag.hp

📁 早期freebsd实现
💻 HP
字号:
// This may look like C code, but it is really -*- C++ -*-/* Copyright (C) 1991 Free Software FoundationThis file is part of the GNU C++ Library.  This library is freesoftware; you can redistribute it and/or modify it under the terms ofthe GNU Library General Public License as published by the FreeSoftware Foundation; either version 2 of the License, or (at youroption) any later version.  This library is distributed in the hopethat it will be useful, but WITHOUT ANY WARRANTY; without even theimplied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULARPURPOSE.  See the GNU Library General Public License for more details.You should have received a copy of the GNU Library General PublicLicense along with this library; if not, write to the Free SoftwareFoundation, 675 Mass Ave, Cambridge, MA 02139, USA.*//* * Bags implemented via William Pugh SkipList algorithms. * CACM, June 1990, p 668-676. * */#ifndef _<T>SkipBag_h#ifdef __GNUG__#pragma interface#endif#define _<T>SkipBag_h 1#include "<T>.Bag.h"#include <values.h>#include <MLCG.h>class <T>SkipBag;class <T>RealSkipBagNode;class <T>SkipBagNode{friend class <T>SkipBag;  private:    <T>RealSkipBagNode * * forward;    <T>SkipBagNode(int size);};class <T>RealSkipBagNode : public <T>SkipBagNode{friend class <T>SkipBag;  private:    <T>             item;    <T>RealSkipBagNode(<T&> h, int size);};typedef <T>RealSkipBagNode* <T>SkipBagNodePtr;inline <T>SkipBagNode::<T>SkipBagNode(int size): forward(new <T>SkipBagNodePtr[size+1]){}inline <T>RealSkipBagNode::<T>RealSkipBagNode(<T&> h, int size): item(h),  <T>SkipBagNode(size){}class <T>SkipBag : public <T>Bag{friend class <T>SkipBaginit;  protected:    <T>SkipBagNode*   header;    int               level;    int               max_levels;    int               randoms_left;    long              random_bits;        static MLCG*      gen;    int               random_level(void);        <T>SkipBagNodePtr leftmost(void);    <T>SkipBagNodePtr rightmost(void);    <T>SkipBagNodePtr pred(<T>SkipBagNodePtr t);    <T>SkipBagNodePtr succ(<T>SkipBagNodePtr t);    void              _kill(void);      private:    enum { BITS_IN_RANDOM = LONGBITS-1 };      public:    <T>SkipBag(long size=DEFAULT_INITIAL_CAPACITY);    <T>SkipBag(<T>SkipBag& a);    ~<T>SkipBag(void);        Pix           add(<T&> i);    void          del(<T&> i);    void          remove(<T&>i);    int           nof(<T&> i);    int           contains(<T&> i);        void          clear(void);        Pix           first(void);    void          next(Pix& i);    <T>&          operator () (Pix i);    Pix           seek(<T&> i, Pix from = 0);        Pix           last(void);    void          prev(Pix& i);        int           OK(void);};inline <T>SkipBagNodePtr <T>SkipBag::leftmost(void){    return header->forward[0];}inline <T>SkipBagNodePtr <T>SkipBag::succ(<T>SkipBagNodePtr t){    <T>SkipBagNodePtr result = 0;    if (t->forward[0]!=header) result = t->forward[0];    return result;}inline Pix <T>SkipBag::first(void){    return Pix(leftmost());}inline Pix <T>SkipBag::last(void){    return Pix(rightmost());}inline void <T>SkipBag::next(Pix& i){    if (i != 0) i = Pix(succ((<T>SkipBagNodePtr)i));}inline <T>& <T>SkipBag::operator () (Pix i){    if (i == 0) error("null Pix");    return ((<T>SkipBagNodePtr)i)->item;}inline void <T>SkipBag::prev(Pix& i){    if (i != 0) i = Pix(pred((<T>SkipBagNodePtr)i));}inline int <T>SkipBag::contains(<T&> key){    return seek(key) != 0;}inline <T>SkipBag::~<T>SkipBag(){    _kill();    delete header;}static class <T>SkipBaginit{  public:    <T>SkipBaginit();    ~<T>SkipBaginit();  private:    static int count;} <T>skipBaginit;#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -