map和set的封装

map和set的底层是红黑树,我们可以对红黑树进行一下改造,同时满足map和set的需求

1.红黑树节点

template<class T>
struct RBTreeNode
{
	RBTreeNode<T>* _left;
	RBTreeNode<T>* _right;
	RBTreeNode<T>* _parent;

	T _data;
	Colour _col;
	RBTreeNode(const T& data)
		:_left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		, _data(data)
		, _col(RED)
	{}
};

红黑树节点的插入改成data插入,不论是map还是set都可以插入data;

2.迭代器的实现

template<class T,class Ref,class Ptr>
struct _RBTreeIterator
{
	typedef RBTreeNode<T> Node;
	typedef _RBTreeIterator<T, Ref, Ptr> Self;
	Node* _node;
	_RBTreeIterator(Node* node)
		:_node(node)
	{}
	Ref operator*()
	{
		return _node->_data;
	}
	Ptr operator->()
	{
		return &_node->_data;
	}
	bool operator!=(const Self& s)
	{
		return _node != s._node;
	}

	Self& operator++()
	{
		if (_node->_right)
		{
			//右树不为空,找到右树的最左节点
			Node* leftMin = _node->_right;
			while(leftMin->_left)
			{
				leftMin = leftMin->_left;
			}
			_node = leftMin;
		}
		else
		{
			//右树为空
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && cur == parent->_right)
			{
				cur = parent;
				parent = cur->_parent;
			}
			_node = parent;
		}
		return *this;
	}
};

对于红黑树,迭代器++是按照中序来走的

3.红黑树的插入

由于无论还map还是set,我们都插入一个值data,所以我们怎么比较大小呢?

用仿函数的方式实现

template<class K, class T,class KeyOfT>
class RBTree
{
	typedef RBTreeNode<T> Node;
public:
	typedef _RBTreeIterator<T, T&, T*> Iterator;
	Iterator begin()
	{
		Node* leftMin = _root;
		while (leftMin && leftMin->_left)
		{
			leftMin = leftMin->_left;
		}
		return Iterator(leftMin);
	}
	Iterator end()
	{
		return Iterator(nullptr);
	}
	
	bool Insert(const T& data)
	{
		if (_root == nullptr)
		{
			_root = new Node(data);
			_root->_col = BLACK;
			return true;
		}
		KeyOfT kot;
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (kot(cur->_data)< kot(data))
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (kot(cur->_data) > kot(data))
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}
		cur = new Node(data);
		cur->_col = RED;
		if (kot(parent->_data) < kot(data))
		{
			parent->_right = cur;
		}
		else
		{
			parent->_left = cur;
		}
		cur->_parent = parent;

		//调整颜色
		//parent不存在,或者parent颜色为黑,跳出循环
		while (parent && parent->_col == RED)
		{
			//关键看叔叔
			Node* grandfather = parent->_parent;
			if (parent == grandfather->_left)
			{
				Node* uncle = grandfather->_right;
				//叔叔存在且为红
				// 变色即可
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;
					//继续向上更新
					cur = grandfather;
					parent = cur->_parent;
				}
				else //叔叔不存在或者叔叔为黑
				{
					if (cur == parent->_left)
					{
						//右旋
						RotateR(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						//左右旋
						RotateL(parent);
						RotateR(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					break;
				}
			}
			else
			{
				Node* uncle = grandfather->_left;
				//叔叔存在且为红,变色即可
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;
					//继续向上更新
					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					//叔叔不存在,或者为黑
					if (cur == parent->_right)
					{
						//左旋
						RotateL(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;

					}
					else
					{
						//右左旋
						RotateR(parent);
						RotateL(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;

					}
					break;
				}
			}
		}
		_root->_col = BLACK;
		return true;
	}

然后在封装的map和sets实现对应的仿函数

struct MapKeyOfT
{
	const K& operator()(const pair<K, V>& kv)
	{
		return kv.first;
	}
};
struct SetKeyOfT
{
	const K& operator()(const K& key)
	{
		return key;
	}
};
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SolrTemplate中的query方法返回的结果是List<T>类型,可以通过遍历List来获取每个Document对象,然后再把每个Document对象的字段封装Map集合中。 以下是一个示例代码: ```java List<Map<String, Object>> resultList = new ArrayList<>(); String keyword = "apple"; Query query = new SimpleQuery(new Criteria("title").contains(keyword)); query.setRows(10); // 设置返回结果的最大数量为10 query.setSort(Sort.by("price").ascending()); // 设置按照价格升序排序 query.setFields("id", "title", "price"); // 设置要返回的字段 QueryResponse response = solrTemplate.query(query); for (SolrDocument document : response.getResults()) { Map<String, Object> resultMap = new HashMap<>(); resultMap.put("id", document.get("id")); resultMap.put("title", document.get("title")); resultMap.put("price", document.get("price")); resultList.add(resultMap); } ``` 在上面的代码中,我们先定义了一个List<Map<String, Object>>类型的结果集,然后创建了一个Query对象,设置了查询条件、返回结果数量、排序方式和返回字段等参数。通过调用solrTemplate.query方法执行查询操作,得到QueryResponse对象。然后遍历QueryResponse中的SolrDocument对象,将每个Document对象的id、title、price字段封装Map集合中,最终将Map集合添加到结果集中。 需要注意的是,SolrDocument中的字段值类型可能是Object类型,需要根据实际情况进行类型转换。如果有多个Document对象需要封装,建议使用Java8的Stream API来简化代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值