Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
TinySTL
提交
d7380c5c
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 搜索 >>
提交
d7380c5c
编写于
1月 19, 2015
作者:
邹
邹晓航
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
完成rehash
上级
7727f398
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
41 addition
and
1 deletion
+41
-1
TinySTL/Detail/Unordered_set.impl.h
TinySTL/Detail/Unordered_set.impl.h
+32
-1
TinySTL/Unordered_set.h
TinySTL/Unordered_set.h
+9
-0
未找到文件。
TinySTL/Detail/Unordered_set.impl.h
浏览文件 @
d7380c5c
#ifndef _UNORDERED_SET_IMPL_H_
#define _UNORDERED_SET_IMPL_H_
#include <functional>
#include <functional>
//for bind
namespace
TinySTL
{
namespace
Detail
{
...
...
@@ -128,12 +128,14 @@ namespace TinySTL{
Unordered_set
<
Key
,
Hash
,
KeyEqual
,
Allocator
>::
Unordered_set
(
const
Unordered_set
&
ust
){
buckets_
=
ust
.
buckets_
;
size_
=
ust
.
size_
;
max_load_factor_
=
ust
.
max_load_factor_
;
}
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_
;
max_load_factor_
=
ust
.
max_load_factor_
;
}
}
template
<
class
Key
,
class
Hash
,
class
KeyEqual
,
class
Allocator
>
...
...
@@ -141,11 +143,13 @@ namespace TinySTL{
bucket_count
=
next_prime
(
bucket_count
);
buckets_
.
resize
(
bucket_count
);
size_
=
0
;
max_load_factor_
=
1.0
;
}
template
<
class
Key
,
class
Hash
,
class
KeyEqual
,
class
Allocator
>
template
<
class
InputIterator
>
Unordered_set
<
Key
,
Hash
,
KeyEqual
,
Allocator
>::
Unordered_set
(
InputIterator
first
,
InputIterator
last
){
size_
=
0
;
max_load_factor_
=
1.0
;
auto
len
=
last
-
first
;
buckets_
.
resize
(
next_prime
(
len
));
for
(;
first
!=
last
;
++
first
){
...
...
@@ -203,6 +207,8 @@ namespace TinySTL{
TinySTL
::
pair
<
typename
Unordered_set
<
Key
,
Hash
,
KeyEqual
,
Allocator
>::
iterator
,
bool
>
Unordered_set
<
Key
,
Hash
,
KeyEqual
,
Allocator
>::
insert
(
const
value_type
&
val
){
if
(
!
has_key
(
val
)){
if
(
load_factor
()
>
max_load_factor
())
rehash
(
next_prime
(
size
()));
auto
index
=
bucket_index
(
val
);
buckets_
[
index
].
push_front
(
val
);
++
size_
;
...
...
@@ -237,6 +243,31 @@ namespace TinySTL{
return
1
;
}
}
template
<
class
Key
,
class
Hash
,
class
KeyEqual
,
class
Allocator
>
float
Unordered_set
<
Key
,
Hash
,
KeyEqual
,
Allocator
>::
max_load_factor
()
const
{
return
max_load_factor_
;
}
template
<
class
Key
,
class
Hash
,
class
KeyEqual
,
class
Allocator
>
void
Unordered_set
<
Key
,
Hash
,
KeyEqual
,
Allocator
>::
max_load_factor
(
float
z
){
max_load_factor_
=
z
;
}
template
<
class
Key
,
class
Hash
,
class
KeyEqual
,
class
Allocator
>
void
Unordered_set
<
Key
,
Hash
,
KeyEqual
,
Allocator
>::
rehash
(
size_type
n
){
if
(
n
<=
buckets_
.
size
())
return
;
Unordered_set
temp
(
next_prime
(
n
));
for
(
auto
&
val
:
*
this
){
temp
.
insert
(
val
);
}
TinySTL
::
swap
(
*
this
,
temp
);
}
template
<
class
Key
,
class
Hash
,
class
KeyEqual
,
class
Allocator
>
void
swap
(
Unordered_set
<
Key
,
Hash
,
KeyEqual
,
Allocator
>&
lhs
,
Unordered_set
<
Key
,
Hash
,
KeyEqual
,
Allocator
>&
rhs
){
TinySTL
::
swap
(
lhs
.
buckets_
,
rhs
.
buckets_
);
TinySTL
::
swap
(
lhs
.
size_
,
rhs
.
size_
);
TinySTL
::
swap
(
lhs
.
max_load_factor_
,
rhs
.
max_load_factor_
);
}
}
#endif
\ No newline at end of file
TinySTL/Unordered_set.h
浏览文件 @
d7380c5c
...
...
@@ -59,6 +59,7 @@ namespace TinySTL{
private:
TinySTL
::
vector
<
TinySTL
::
list
<
key_type
>>
buckets_
;
size_type
size_
;
float
max_load_factor_
;
#define PRIME_LIST_SIZE 28
static
size_t
prime_list_
[
PRIME_LIST_SIZE
];
public:
...
...
@@ -74,6 +75,9 @@ namespace TinySTL{
size_type
bucket_size
(
size_type
i
)
const
;
size_type
bucket
(
const
key_type
&
key
)
const
;
float
load_factor
()
const
;
float
max_load_factor
()
const
;
void
max_load_factor
(
float
z
);
void
rehash
(
size_type
n
);
iterator
begin
();
iterator
end
();
...
...
@@ -96,7 +100,12 @@ namespace TinySTL{
size_type
next_prime
(
size_type
n
)
const
;
size_type
bucket_index
(
const
key_type
&
key
)
const
;
bool
has_key
(
const
key_type
&
key
);
public:
template
<
class
Key
,
class
Hash
,
class
KeyEqual
,
class
Allocator
>
friend
void
swap
(
Unordered_set
<
Key
,
Hash
,
KeyEqual
,
Allocator
>&
lhs
,
Unordered_set
<
Key
,
Hash
,
KeyEqual
,
Allocator
>&
rhs
);
};
}
//end of namespace TinySTL
#include "Detail\Unordered_set.impl.h"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录