Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
Monado
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
25
Issues
25
List
Boards
Labels
Milestones
Merge Requests
10
Merge Requests
10
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Container Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Monado
Monado
Commits
a36565c5
Commit
a36565c5
authored
Apr 06, 2019
by
Jakob Bornecrantz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
aux/util: Add string hashset
parent
b8e4a5f8
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
215 additions
and
0 deletions
+215
-0
src/xrt/auxiliary/CMakeLists.txt
src/xrt/auxiliary/CMakeLists.txt
+2
-0
src/xrt/auxiliary/util/u_hashset.cpp
src/xrt/auxiliary/util/u_hashset.cpp
+125
-0
src/xrt/auxiliary/util/u_hashset.h
src/xrt/auxiliary/util/u_hashset.h
+88
-0
No files found.
src/xrt/auxiliary/CMakeLists.txt
View file @
a36565c5
...
...
@@ -18,6 +18,8 @@ set(SOURCE_FILES
util/u_device.c
util/u_device.h
util/u_documentation.h
util/u_hashset.cpp
util/u_hashset.h
util/u_time.cpp
util/u_time.h
)
...
...
src/xrt/auxiliary/util/u_hashset.cpp
0 → 100644
View file @
a36565c5
// Copyright 2019, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0
/*!
* @file
* @brief Hashset struct header.
* @author Jakob Bornecrantz <jakob@collabora.com>
* @ingroup aux_util
*/
#include "util/u_hashset.h"
#include <cstring>
#include <unordered_map>
#include <vector>
/*
*
* Private structs and defines.
*
*/
struct
u_hashset
{
std
::
unordered_map
<
std
::
string
,
struct
u_hashset_item
*>
map
=
{};
};
/*
*
* "Exported" functions.
*
*/
extern
"C"
int
u_hashset_create
(
struct
u_hashset
**
out_hashset
)
{
auto
hs
=
new
u_hashset
;
*
out_hashset
=
hs
;
return
0
;
}
extern
"C"
int
u_hashset_destroy
(
struct
u_hashset
**
hs
)
{
delete
*
hs
;
*
hs
=
NULL
;
return
0
;
}
extern
"C"
int
u_hashset_find_str
(
struct
u_hashset
*
hs
,
const
char
*
str
,
size_t
length
,
struct
u_hashset_item
**
out_item
)
{
std
::
string
key
=
std
::
string
(
str
,
length
);
auto
search
=
hs
->
map
.
find
(
key
);
if
(
search
!=
hs
->
map
.
end
())
{
*
out_item
=
search
->
second
;
return
0
;
}
else
{
return
-
1
;
}
}
extern
"C"
int
u_hashset_find_c_str
(
struct
u_hashset
*
hs
,
const
char
*
c_str
,
struct
u_hashset_item
**
out_item
)
{
size_t
length
=
strlen
(
c_str
);
return
u_hashset_find_str
(
hs
,
c_str
,
length
,
out_item
);
}
extern
"C"
int
u_hashset_insert_item
(
struct
u_hashset
*
hs
,
struct
u_hashset_item
*
item
)
{
std
::
string
key
=
std
::
string
(
item
->
c_str
,
item
->
length
);
hs
->
map
[
key
]
=
item
;
return
0
;
}
extern
"C"
int
u_hashset_erase_item
(
struct
u_hashset
*
hs
,
struct
u_hashset_item
*
item
)
{
std
::
string
key
=
std
::
string
(
item
->
c_str
,
item
->
length
);
hs
->
map
.
erase
(
key
);
return
0
;
}
extern
"C"
int
u_hashset_erase_str
(
struct
u_hashset
*
hs
,
const
char
*
str
,
size_t
length
)
{
std
::
string
key
=
std
::
string
(
str
,
length
);
hs
->
map
.
erase
(
key
);
return
0
;
}
extern
"C"
int
u_hashset_erase_c_str
(
struct
u_hashset
*
hs
,
const
char
*
c_str
)
{
size_t
length
=
strlen
(
c_str
);
return
u_hashset_erase_str
(
hs
,
c_str
,
length
);
}
extern
"C"
void
u_hashset_clear_and_call_for_each
(
struct
u_hashset
*
hs
,
u_hashset_callback
cb
,
void
*
priv
)
{
std
::
vector
<
struct
u_hashset_item
*>
tmp
;
tmp
.
reserve
(
hs
->
map
.
size
());
for
(
auto
&
n
:
hs
->
map
)
{
tmp
.
push_back
(
n
.
second
);
}
hs
->
map
.
clear
();
for
(
auto
n
:
tmp
)
{
cb
(
n
,
priv
);
}
}
src/xrt/auxiliary/util/u_hashset.h
0 → 100644
View file @
a36565c5
// Copyright 2019, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0
/*!
* @file
* @brief Hashset struct header.
* @author Jakob Bornecrantz <jakob@collabora.com>
* @ingroup aux_util
*/
#pragma once
#include "xrt/xrt_compiler.h"
#ifdef __cplusplus
extern
"C"
{
#endif
/*!
* @struct u_hashset
* @ingroup aux_util
*
* Kind of bespoke hashset implementation, where the user is responsible for
* allocating and freeing the items themselves.
*
* This allows embedding the @ref u_hashset_item at the end of structs.
*/
struct
u_hashset
;
/*!
* A embeddable hashset item, note that the string directly follows the
* @ref u_hashset_item.
*
* @ingroup aux_util
*/
struct
u_hashset_item
{
size_t
hash
;
size_t
length
;
const
char
c_str
[];
};
typedef
void
(
*
u_hashset_callback
)(
struct
u_hashset_item
*
item
,
void
*
priv
);
int
u_hashset_create
(
struct
u_hashset
**
out_hashset
);
int
u_hashset_destroy
(
struct
u_hashset
**
hs
);
int
u_hashset_find_str
(
struct
u_hashset
*
hs
,
const
char
*
str
,
size_t
length
,
struct
u_hashset_item
**
out_item
);
int
u_hashset_find_c_str
(
struct
u_hashset
*
hs
,
const
char
*
c_str
,
struct
u_hashset_item
**
out_item
);
int
u_hashset_insert_item
(
struct
u_hashset
*
hs
,
struct
u_hashset_item
*
item
);
int
u_hashset_erase_item
(
struct
u_hashset
*
hs
,
struct
u_hashset_item
*
item
);
int
u_hashset_erase_str
(
struct
u_hashset
*
hs
,
const
char
*
str
,
size_t
length
);
int
u_hashset_erase_c_str
(
struct
u_hashset
*
hs
,
const
char
*
c_str
);
/*!
* First clear the hashset and then call the given callback with each item that
* was in the hashset.
*
* @ingroup aux_util
*/
void
u_hashset_clear_and_call_for_each
(
struct
u_hashset
*
hs
,
u_hashset_callback
cb
,
void
*
priv
);
#ifdef __cplusplus
}
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment