线程安全的skiplist,lockfree,CAS,c11版

#ifndef _SKIPLIST_H
#define _SKIPLIST_H

#include <iostream>
#include<atomic>

#define DEBUG 1

namespace microdb
{

template<typename Key>
class SkipList
{
public:
	static const unsigned int MAXLEVEL = 4;

	class Node
	{
	public:
		explicit Node(const Key& key);
		bool setNext(Node* expected, Node* node, unsigned int level);
		void setNextWithoutAtomic(Node* node, unsigned int level);
		Node* getNext(unsigned int level);
		const Key& key() const;
	private:
		Key key_;
		std::atomic<Node*> next_[MAXLEVEL];	
	};

	class Iterator
	{
	public:
		Iterator();
		explicit Iterator(Node* node);	
		Iterator next(unsigned int level);
		Node* operator->();
		Node& operator*();
		bool operator==(Iterator& itr) const;
		bool operator!=(Iterator& itr) const;
		const Key& key() const;
		Node* node() const;
	private:
		Node* node_;
	};
	
	explicit SkipList(const Key& key);
	void put(const Key& key);
	bool get(const Key& key);
	void remove(const Key& key);
	void print();
	Iterator& begin();
	Iterator& end();

#if DEBUG
	struct _debug_counter
	{
		std::atomic<unsigned int> inserted_counter;
		std::atomic<unsigned int> not_inserted_counter;
		std::atomic<unsigned int> in_list_counter;
	}debug_counter;

	void inListCount();
#endif
private:
	int random();
 	SkipList<Key>::Iterator lowerBound(const Key& key, unsigned int level);
	SkipList<Key>::Iterator upperBound(const Key& key, unsigned int level);

	Node* head_;
	Iterator begin_;
	Iterator end_;
};

template<typename Key>
SkipList<Key>::N
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值