Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
TinySTL
提交
c42b7177
T
TinySTL
项目概览
OpenDocCN
/
TinySTL
通知
4
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TinySTL
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
c42b7177
编写于
1月 19, 2015
作者:
邹
邹晓航
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
完成迭代器
上级
c6250451
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
113 addition
and
4 deletion
+113
-4
TinySTL/Detail/Unordered_set.impl.h
TinySTL/Detail/Unordered_set.impl.h
+76
-0
TinySTL/Unordered_set.h
TinySTL/Unordered_set.h
+37
-4
未找到文件。
TinySTL/Detail/Unordered_set.impl.h
浏览文件 @
c42b7177
...
...
@@ -4,6 +4,53 @@
#include <functional>
namespace
TinySTL
{
namespace
Detail
{
template
<
class
Key
,
class
ListIterator
,
class
Hash
,
class
KeyEqual
,
class
Allocator
>
ust_iterator
<
Key
,
ListIterator
,
Hash
,
KeyEqual
,
Allocator
>::
ust_iterator
(
size_t
index
,
ListIterator
it
,
cntrPtr
ptr
)
:
bucket_index_
(
index
),
iterator_
(
it
),
container_
(
ptr
){}
template
<
class
Key
,
class
ListIterator
,
class
Hash
,
class
KeyEqual
,
class
Allocator
>
ust_iterator
<
Key
,
ListIterator
,
Hash
,
KeyEqual
,
Allocator
>&
ust_iterator
<
Key
,
ListIterator
,
Hash
,
KeyEqual
,
Allocator
>::
operator
++
(){
++
iterator_
;
//如果前进一位后到达了list的末尾,则需要跳转到下一个有item的bucket的list
if
(
iterator_
==
container_
->
buckets_
[
bucket_index_
].
end
()){
for
(;;){
if
(
bucket_index_
==
container_
->
buckets_
.
size
()
-
1
){
*
this
=
container_
->
end
();
break
;
}
else
{
++
bucket_index_
;
if
(
!
(
container_
->
buckets_
[
bucket_index_
].
empty
())){
//此list不为空
iterator_
=
container_
->
buckets_
[
bucket_index_
].
begin
();
break
;
}
}
}
}
return
*
this
;
}
template
<
class
Key
,
class
ListIterator
,
class
Hash
,
class
KeyEqual
,
class
Allocator
>
ust_iterator
<
Key
,
ListIterator
,
Hash
,
KeyEqual
,
Allocator
>
ust_iterator
<
Key
,
ListIterator
,
Hash
,
KeyEqual
,
Allocator
>::
operator
++
(
int
){
auto
res
=
*
this
;
++*
this
;
return
res
;
}
template
<
class
Key
,
class
ListIterator
,
class
Hash
,
class
KeyEqual
,
class
Allocator
>
bool
operator
==
(
const
ust_iterator
<
Key
,
ListIterator
,
Hash
,
KeyEqual
,
Allocator
>&
lhs
,
const
ust_iterator
<
Key
,
ListIterator
,
Hash
,
KeyEqual
,
Allocator
>&
rhs
){
return
lhs
.
bucket_index_
==
rhs
.
bucket_index_
&&
lhs
.
iterator_
==
rhs
.
iterator_
&&
lhs
.
container_
==
rhs
.
container_
;
}
template
<
class
Key
,
class
ListIterator
,
class
Hash
,
class
KeyEqual
,
class
Allocator
>
bool
operator
!=
(
const
ust_iterator
<
Key
,
ListIterator
,
Hash
,
KeyEqual
,
Allocator
>&
lhs
,
const
ust_iterator
<
Key
,
ListIterator
,
Hash
,
KeyEqual
,
Allocator
>&
rhs
){
return
!
(
lhs
==
rhs
);
}
}
//end of Detail namespace
template
<
class
Key
,
class
Hash
,
class
KeyEqual
,
class
Allocator
>
typename
Unordered_set
<
Key
,
Hash
,
KeyEqual
,
Allocator
>::
size_type
Unordered_set
<
Key
,
Hash
,
KeyEqual
,
Allocator
>::
size
()
const
{
...
...
@@ -78,6 +125,18 @@ namespace TinySTL{
return
prime_list_
[
i
];
}
template
<
class
Key
,
class
Hash
,
class
KeyEqual
,
class
Allocator
>
Unordered_set
<
Key
,
Hash
,
KeyEqual
,
Allocator
>::
Unordered_set
(
const
Unordered_set
&
ust
){
buckets_
=
ust
.
buckets_
;
size_
=
ust
.
size_
;
}
template
<
class
Key
,
class
Hash
,
class
KeyEqual
,
class
Allocator
>
Unordered_set
<
Key
,
Hash
,
KeyEqual
,
Allocator
>&
Unordered_set
<
Key
,
Hash
,
KeyEqual
,
Allocator
>::
operator
=
(
const
Unordered_set
&
ust
){
if
(
this
!=
&
ust
){
buckets_
=
ust
.
buckets_
;
size_
=
ust
.
size_
;
}
}
template
<
class
Key
,
class
Hash
,
class
KeyEqual
,
class
Allocator
>
Unordered_set
<
Key
,
Hash
,
KeyEqual
,
Allocator
>::
Unordered_set
(
size_type
bucket_count
){
bucket_count
=
next_prime
(
bucket_count
);
buckets_
.
resize
(
bucket_count
);
...
...
@@ -97,6 +156,23 @@ namespace TinySTL{
}
}
}
template
<
class
Key
,
class
Hash
,
class
KeyEqual
,
class
Allocator
>
typename
Unordered_set
<
Key
,
Hash
,
KeyEqual
,
Allocator
>::
iterator
Unordered_set
<
Key
,
Hash
,
KeyEqual
,
Allocator
>::
begin
(){
size_type
index
=
0
;
for
(;
index
!=
buckets_
.
size
();
++
index
){
if
(
!
(
buckets_
[
index
].
empty
()))
break
;
}
if
(
index
==
buckets_
.
size
())
return
end
();
return
iterator
(
index
,
buckets_
[
index
].
begin
(),
this
);
}
template
<
class
Key
,
class
Hash
,
class
KeyEqual
,
class
Allocator
>
typename
Unordered_set
<
Key
,
Hash
,
KeyEqual
,
Allocator
>::
iterator
Unordered_set
<
Key
,
Hash
,
KeyEqual
,
Allocator
>::
end
(){
return
iterator
(
buckets_
.
size
()
-
1
,
buckets_
[
buckets_
.
size
()
-
1
].
end
(),
this
);
}
}
#endif
\ No newline at end of file
TinySTL/Unordered_set.h
浏览文件 @
c42b7177
...
...
@@ -6,13 +6,42 @@
#include "Functional.h"
#include "List.h"
#include "Vector.h"
#include <list>
#include <vector>
namespace
TinySTL
{
template
<
class
Key
,
class
Hash
,
class
KeyEqual
,
class
Allocator
>
class
Unordered_set
;
namespace
Detail
{
template
<
class
Key
,
class
ListIterator
,
class
Hash
=
std
::
hash
<
Key
>,
class
KeyEqual
=
TinySTL
::
equal_to
<
Key
>
,
class
Allocator
=
TinySTL
::
allocator
<
Key
>>
class
ust_iterator
{
private:
typedef
Unordered_set
<
Key
,
Hash
,
KeyEqual
,
Allocator
>*
cntrPtr
;
size_t
bucket_index_
;
ListIterator
iterator_
;
cntrPtr
container_
;
public:
ust_iterator
(
size_t
index
,
ListIterator
it
,
cntrPtr
ptr
);
ust_iterator
&
operator
++
();
ust_iterator
operator
++
(
int
);
Key
&
operator
*
(){
return
*
iterator_
;
}
Key
*
operator
->
(){
return
&
(
operator
*
());
}
private:
template
<
class
Key
,
class
ListIterator
,
class
Hash
,
class
KeyEqual
,
class
Allocator
>
friend
bool
operator
==
(
const
ust_iterator
<
Key
,
ListIterator
,
Hash
,
KeyEqual
,
Allocator
>&
lhs
,
const
ust_iterator
<
Key
,
ListIterator
,
Hash
,
KeyEqual
,
Allocator
>&
rhs
);
template
<
class
Key
,
class
ListIterator
,
class
Hash
,
class
KeyEqual
,
class
Allocator
>
friend
bool
operator
!=
(
const
ust_iterator
<
Key
,
ListIterator
,
Hash
,
KeyEqual
,
Allocator
>&
lhs
,
const
ust_iterator
<
Key
,
ListIterator
,
Hash
,
KeyEqual
,
Allocator
>&
rhs
);
};
}
//end of namespace Detail
template
<
class
Key
,
class
Hash
=
std
::
hash
<
Key
>,
class
KeyEqual
=
TinySTL
::
equal_to
<
Key
>
,
class
Allocator
=
TinySTL
::
allocator
<
Key
>>
class
Unordered_set
{
private:
template
<
class
Key
,
class
ListIterator
,
class
Hash
,
class
KeyEqual
,
class
Allocator
>
friend
class
Detail
::
ust_iterator
;
public:
typedef
Key
key_type
;
typedef
Key
value_type
;
...
...
@@ -22,6 +51,7 @@ namespace TinySTL{
typedef
Allocator
allocator_type
;
typedef
value_type
&
reference
;
typedef
const
value_type
&
const_reference
;
typedef
Detail
::
ust_iterator
<
Key
,
typename
TinySTL
::
list
<
key_type
>::
iterator
,
Hash
,
KeyEqual
,
Allocator
>
iterator
;
private:
TinySTL
::
vector
<
TinySTL
::
list
<
key_type
>>
buckets_
;
size_type
size_
;
...
...
@@ -31,8 +61,8 @@ namespace TinySTL{
explicit
Unordered_set
(
size_t
bucket_count
);
template
<
class
InputIterator
>
Unordered_set
(
InputIterator
first
,
InputIterator
last
);
//
Unordered_set(const Unordered_set& ust);
//
Unordered_set& operator = (const Unordered_set& ust);
Unordered_set
(
const
Unordered_set
&
ust
);
Unordered_set
&
operator
=
(
const
Unordered_set
&
ust
);
size_type
size
()
const
;
bool
empty
()
const
;
...
...
@@ -41,6 +71,9 @@ namespace TinySTL{
size_type
bucket
(
const
key_type
&
key
)
const
;
float
load_factor
()
const
;
iterator
begin
();
iterator
end
();
haser
hash_function
()
const
;
key_equal
key_eq
()
const
;
allocator_type
get_allocator
()
const
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录