-
Notifications
You must be signed in to change notification settings - Fork 3
Jhx set #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
F-TD5X
wants to merge
10
commits into
master
Choose a base branch
from
jhx_Set
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Jhx set #12
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5dc823b
add function
F-TD5X 3267b8f
update
F-TD5X 0906d1f
add test
F-TD5X ebf146f
fix swap
F-TD5X 4e9d10a
update
F-TD5X e5b8f27
add test
F-TD5X cb4f87a
temp
F-TD5X 7e70e4f
merge origin with local
F-TD5X 26d4573
remove merge log
F-TD5X fd73a22
modify retract
F-TD5X File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ check: | |
| @bin/deque_demo | ||
| @bin/list_demo | ||
| @bin/Rb_tree_test | ||
| @bin/set_test | ||
| clean: | ||
| @rm -r bin | ||
| distclean: clean | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| /** | ||
| * file: set.h | ||
| * date: 2018/05/16 | ||
| * author: F_TD5X(jhx) | ||
| **/ | ||
|
|
||
| #ifndef _CUSTL_SET_H | ||
| #define _CUSTL_SET_H | ||
|
|
||
| #include "tree.h" | ||
|
|
||
| namespace custl { | ||
|
|
||
| template <typename _Key, typename _Compare = std::less<_Key>, | ||
| typename _Alloc = _Key> | ||
| class set; | ||
|
|
||
| template <typename _Key, typename _Compare, class _Alloc> | ||
| class set { | ||
| public: | ||
| typedef _Key key_type; | ||
| typedef _Key value_type; | ||
| typedef _Compare key_compare; | ||
| typedef _Compare value_compare; | ||
|
|
||
| private: | ||
| typedef _Rb_tree<key_type, value_type, std::_Identity<value_type>, key_compare, | ||
| _Alloc> | ||
| _Rep_type; | ||
| _Rep_type _M_t; | ||
|
|
||
| public: | ||
| typedef typename _Rep_type::const_pointer pointer; | ||
| typedef typename _Rep_type::const_pointer const_pointer; | ||
| typedef typename _Rep_type::const_reference reference; | ||
| typedef typename _Rep_type::const_reference const_reference; | ||
| typedef typename _Rep_type::const_iterator iterator; | ||
| typedef typename _Rep_type::const_iterator const_iterator; | ||
| typedef typename _Rep_type::size_type size_type; | ||
| typedef typename _Rep_type::difference_type difference_type; | ||
| typedef typename _Rep_type::allocator_type allocator_type; | ||
| set() : _M_t(_Compare(), allocator_type()) {} | ||
| explicit set(const _Compare& __comp, | ||
| const allocator_type& __a = allocator_type()) | ||
| : _M_t(__comp, __a) {} | ||
|
|
||
| set(const set<_Key, _Compare, _Alloc>& __x) : _M_t(__x._M_t) {} | ||
| set<_Key, _Compare, _Alloc>& operator=( | ||
| const set<_Key, _Compare, _Alloc>& __x) { | ||
| _M_t = __x._M_t; | ||
| return *this; | ||
| } | ||
| template <typename _InputIterator> | ||
| set(_InputIterator __first, _InputIterator __last) | ||
| : _M_t(_Compare(), allocator_type()) { | ||
| _M_t.insert_unique(__first, __last); | ||
| } | ||
|
|
||
| template <typename _InputIterator> | ||
| set(_InputIterator __first, _InputIterator __last, const _Compare& __comp, | ||
| const allocator_type& __a = allocator_type()) | ||
| : _M_t(__comp, __a) { | ||
| _M_t.insert_unique(__first, __last); | ||
| } | ||
|
|
||
| key_compare key_comp() const { return _M_t.key_comp(); } | ||
| value_compare value_comp() const { return _M_t.key_comp(); } | ||
| allocator_type get_allocator() const { return _M_t.get_allocator(); } | ||
|
|
||
| iterator begin() const { return _M_t.begin(); } | ||
| iterator end() const { return _M_t.end(); } | ||
| //reverse_iterator rbegin() const { return _M_t.rbegin(); } | ||
| //reverse_iterator rend() const { return _M_t.rend(); } | ||
| bool empty() const { return _M_t.empty(); } | ||
| size_type size() const { return _M_t.size(); } | ||
| size_type max_size() const { return _M_t.max_size(); } | ||
| void swap(set<_Key, _Compare, _Alloc>& __x) { _M_t.swap(__x._M_t); } | ||
|
|
||
| std::pair<iterator, bool> insert(const value_type& __x) { | ||
| std::pair<typename _Rep_type::iterator, bool> __p = | ||
| _M_t.insert_unique(__x); | ||
| return std::pair<iterator, bool>(__p.first, __p.second); | ||
| } | ||
| iterator insert(iterator __position, const value_type& __x) { | ||
| typedef typename _Rep_type::iterator _Rep_iterator; | ||
| return _M_t.insert_unique((_Rep_iterator&)__position, __x); | ||
| } | ||
| template <class _InputIterator> | ||
| void insert(_InputIterator __first, _InputIterator __last) { | ||
| _M_t.insert_unique(__first, __last); | ||
| } | ||
| void erase(iterator __position) { | ||
| typedef typename _Rep_type::iterator _Rep_iterator; | ||
| _M_t.erase((_Rep_iterator&)__position); | ||
| } | ||
| size_type erase(const key_type& __x) { return _M_t.erase(__x); } | ||
| void erase(iterator __first, iterator __last) { | ||
| typedef typename _Rep_type::iterator _Rep_iterator; | ||
| _M_t.erase((_Rep_iterator&)__first, (_Rep_iterator&)__last); | ||
| } | ||
| void clear() { _M_t.clear(); } | ||
|
|
||
| iterator find(const key_type& __x) const { return _M_t.find(__x); } | ||
| size_type count(const key_type& __x) const { | ||
| return _M_t.find(__x) == _M_t.end() ? 0 : 1; | ||
| } | ||
| iterator lower_bound(const key_type& __x) const { | ||
| return _M_t.lower_bound(__x); | ||
| } | ||
| iterator upper_bound(const key_type& __x) const { | ||
| return _M_t.upper_bound(__x); | ||
| } | ||
| std::pair<iterator, iterator> equal_range(const key_type& __x) const { | ||
| return _M_t.equal_range(__x); | ||
| } | ||
| }; | ||
|
|
||
| template <typename _Key, typename _Compare, typename _Alloc> | ||
| inline bool operator==(const set<_Key, _Compare, _Alloc>& __x, | ||
| const set<_Key, _Compare, _Alloc>& __y) { | ||
| return __x._M_t == __y._M_t; | ||
| } | ||
|
|
||
| template <typename _Key, typename _Compare, typename _Alloc> | ||
| inline bool operator<(const set<_Key, _Compare, _Alloc>& __x, | ||
| const set<_Key, _Compare, _Alloc>& __y) { | ||
| return __x._M_t < __y._M_t; | ||
| } | ||
|
|
||
| template <typename _Key, typename _Compare, typename _Alloc> | ||
| inline bool operator!=(const set<_Key, _Compare, _Alloc>& __x, | ||
| const set<_Key, _Compare, _Alloc>& __y) { | ||
| return !(__x == __y); | ||
| } | ||
|
|
||
| template <typename _Key, typename _Compare, typename _Alloc> | ||
| inline bool operator>(const set<_Key, _Compare, _Alloc>& __x, | ||
| const set<_Key, _Compare, _Alloc>& __y) { | ||
| return __y < __x; | ||
| } | ||
|
|
||
| template <typename _Key, typename _Compare, typename _Alloc> | ||
| inline bool operator<=(const set<_Key, _Compare, _Alloc>& __x, | ||
| const set<_Key, _Compare, _Alloc>& __y) { | ||
| return !(__y < __x); | ||
| } | ||
|
|
||
| template <typename _Key, typename _Compare, typename _Alloc> | ||
| inline bool operator>=(const set<_Key, _Compare, _Alloc>& __x, | ||
| const set<_Key, _Compare, _Alloc>& __y) { | ||
| return !(__x < __y); | ||
| } | ||
|
|
||
| template <typename _Key, typename _Compare, typename _Alloc> | ||
| inline void swap(set<_Key, _Compare, _Alloc>& __x, | ||
| set<_Key, _Compare, _Alloc>& __y) { | ||
| __x.swap(__y); | ||
| } | ||
|
|
||
| } // namespace custl | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -427,26 +427,26 @@ inline _Rb_tree_node_base* _Rb_tree_rebalance_for_erase( | |
| template <typename _Key, typename _Val, typename _KeyOfValue, typename _Compare, | ||
| typename _Alloc = allocator<_Val>> | ||
| class _Rb_tree { | ||
| protected: | ||
| typedef _Rb_tree_node_base* _Base_ptr; | ||
| typedef _Rb_tree_color _Color_type; | ||
| protected: | ||
| typedef _Rb_tree_node_base* _Base_ptr; | ||
| typedef _Rb_tree_color _Color_type; | ||
| typedef _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc> _Self; | ||
|
|
||
| public: | ||
| typedef _Key key_type; | ||
| typedef _Val value_type; | ||
| typedef value_type* pointer; | ||
| typedef value_type& reference; | ||
| typedef const value_type* const_pointer; | ||
| typedef const value_type& const_reference; | ||
| typedef _Rb_tree_node<_Val>* _Link_type; | ||
| typedef size_t size_type; | ||
| typedef ptrdiff_t difference_type; | ||
| typedef _Alloc allocator_type; | ||
| public: | ||
| typedef _Key key_type; | ||
| typedef _Val value_type; | ||
| typedef value_type* pointer; | ||
| typedef value_type& reference; | ||
| typedef const value_type* const_pointer; | ||
| typedef const value_type& const_reference; | ||
| typedef _Rb_tree_node<_Val>* _Link_type; | ||
| typedef size_t size_type; | ||
| typedef ptrdiff_t difference_type; | ||
| typedef _Alloc allocator_type; | ||
|
|
||
| allocator_type get_allocator() const { return allocator_type(); } | ||
|
|
||
| protected: | ||
| protected: | ||
| _Link_type _M_header; | ||
| _Link_type _M_get_node() { | ||
| return allocator<_Rb_tree_node<_Val>>::allocate(1); | ||
|
|
@@ -475,7 +475,7 @@ class _Rb_tree { | |
| _M_put_node(__p); | ||
| } | ||
|
|
||
| protected: | ||
| protected: | ||
| size_type _M_node_count; | ||
| _Compare _M_key_compare; | ||
|
|
||
|
|
@@ -529,16 +529,16 @@ class _Rb_tree { | |
| return (_Link_type)_Rb_tree_node_base::_S_maximum(__x); | ||
| } | ||
|
|
||
| public: | ||
| public: | ||
| typedef _Rb_tree_iterator<_Val> iterator; | ||
| typedef _Rb_tree_const_iterator<_Val> const_iterator; | ||
|
|
||
| private: | ||
| private: | ||
| iterator _M_insert(_Base_ptr __x, _Base_ptr __y, const value_type& __v); | ||
| _Link_type _M_copy(_Link_type __x, _Link_type __p); | ||
| void _M_erase(_Link_type __x); | ||
|
|
||
| public: | ||
| public: | ||
| _Rb_tree() : _M_node_count(0), _M_key_compare() { | ||
| _M_header = _M_get_node(); | ||
| _M_empty_initialize(); | ||
|
|
@@ -570,31 +570,31 @@ class _Rb_tree { | |
| _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& operator=( | ||
| const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x); | ||
|
|
||
| private: | ||
| private: | ||
| void _M_empty_initialize() { | ||
| _S_color(_M_header) = red; | ||
| _M_root() = nullptr; | ||
| _M_rightmost() = _M_leftmost() = _M_header; | ||
| } | ||
|
|
||
| public: | ||
| public: | ||
| _Compare key_comp() const { return _M_key_compare; } | ||
| iterator begin() { return _M_leftmost(); } | ||
| iterator end() { return _M_header; } | ||
| const_iterator begin() const { return _M_leftmost(); } | ||
| const_iterator end() const { return _M_header; } | ||
|
|
||
| bool empty() { return _M_node_count == 0; } | ||
| bool empty() const { return _M_node_count == 0; } | ||
| size_type size() const { return _M_node_count; } | ||
| size_type max_size() const { return size_type(-1); } | ||
|
|
||
| void swap(_Self& __t) { | ||
| swap(_M_header, __t._M_header); | ||
| swap(_M_node_count, __t._M_node_count); | ||
| swap(_M_key_compare, __t._M_key_compare); | ||
| std::swap(_M_header, __t._M_header); | ||
| std::swap(_M_node_count, __t._M_node_count); | ||
| std::swap(_M_key_compare, __t._M_key_compare); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. std?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. std废话啊。。。又没人写 stl_utility 。 |
||
| } | ||
|
|
||
| public: | ||
| public: | ||
| std::pair<iterator, bool> insert_unique(const value_type& __x); | ||
| iterator insert_equal(const value_type& __x); | ||
| iterator insert_unique(iterator __pos, const value_type& __x); | ||
|
|
@@ -603,6 +603,10 @@ class _Rb_tree { | |
| void insert_unique(const value_type* __first, const value_type* __last); | ||
| void insert_equal(const_iterator __first, const_iterator __last); | ||
| void insert_equal(const value_type* __first, const value_type* __last); | ||
| template <typename _InsertIterator> | ||
| void insert_unique(_InsertIterator __first, _InsertIterator __last); | ||
| template <typename _InsertIterator> | ||
| void insert_equal(_InsertIterator __first, _InsertIterator __last); | ||
|
|
||
| void erase(iterator __pos); | ||
| size_type erase(const key_type& __x); | ||
|
|
@@ -845,25 +849,29 @@ _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::insert_equal( | |
| } | ||
| } | ||
|
|
||
| template <typename _Key, typename _Val, typename _KoV, typename _Cmp, typename _Alloc> | ||
| template <typename _Key, typename _Val, typename _KoV, typename _Cmp, | ||
| typename _Alloc> | ||
| void _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::insert_equal( | ||
| const _Val* __first, const _Val* __last) { | ||
| for (; __first != __last; ++__first) insert_equal(*__first); | ||
| } | ||
|
|
||
| template <typename _Key, typename _Val, typename _KoV, typename _Cmp, typename _Alloc> | ||
| template <typename _Key, typename _Val, typename _KoV, typename _Cmp, | ||
| typename _Alloc> | ||
| void _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::insert_equal( | ||
| const_iterator __first, const_iterator __last) { | ||
| for (; __first != __last; ++__first) insert_equal(*__first); | ||
| } | ||
|
|
||
| template <typename _Key, typename _Val, typename _KoV, typename _Cmp, typename _Alloc> | ||
| template <typename _Key, typename _Val, typename _KoV, typename _Cmp, | ||
| typename _Alloc> | ||
| void _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::insert_unique( | ||
| const _Val* __first, const _Val* __last) { | ||
| for (; __first != __last; ++__first) insert_unique(*__first); | ||
| } | ||
|
|
||
| template <typename _Key, typename _Val, typename _KoV, typename _Cmp, typename _Alloc> | ||
| template <typename _Key, typename _Val, typename _KoV, typename _Cmp, | ||
| typename _Alloc> | ||
| void _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::insert_unique( | ||
| const_iterator __first, const_iterator __last) { | ||
| for (; __first != __last; ++__first) insert_unique(*__first); | ||
|
|
@@ -891,7 +899,8 @@ _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::erase(const _Key& __x) { | |
| return __n; | ||
| } | ||
|
|
||
| template <typename _Key, typename _Val, typename _KoV, typename _Compare, typename _Alloc> | ||
| template <typename _Key, typename _Val, typename _KoV, typename _Compare, | ||
| typename _Alloc> | ||
| typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Link_type | ||
| _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_M_copy(_Link_type __x, | ||
| _Link_type __p) { | ||
|
|
@@ -1073,7 +1082,8 @@ _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::equal_range( | |
| return std::pair<iterator, iterator>(lower_bound(__k), upper_bound(__k)); | ||
| } | ||
|
|
||
| template <typename _Key, typename _Val, typename _KoV, typename _Compare, typename _Alloc> | ||
| template <typename _Key, typename _Val, typename _KoV, typename _Compare, | ||
| typename _Alloc> | ||
| inline std::pair< | ||
| typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::const_iterator, | ||
| typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::const_iterator> | ||
|
|
@@ -1129,6 +1139,22 @@ bool _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::__verify() const { | |
| return true; | ||
| } | ||
|
|
||
| template <typename _Key, typename _Val, typename _KeyOfValue, typename _Compare, | ||
| typename _Alloc> | ||
| template <typename _II> | ||
| void _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::insert_unique( | ||
| _II __first, _II __last) { | ||
| for (; __first != __last; ++__first) insert_unique(*__first); | ||
| } | ||
|
|
||
| template <typename _Key, typename _Val, typename _KeyOfValue, typename _Compare, | ||
| typename _Alloc> | ||
| template <typename _II> | ||
| void _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::insert_equal( | ||
| _II __first, _II __last) { | ||
| for (; __first != __last; ++__first) insert_equal(*__first); | ||
| } | ||
|
|
||
| template <typename _Key, typename _Val, typename _KeyOfValue, typename _Compare, | ||
| typename _Alloc = allocator<_Val>> | ||
| struct rb_tree : public _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc> { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std是什么鬼