Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
F
flatbuffers
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
flatbuffers
Commits
c8f1682e
Commit
c8f1682e
authored
May 18, 2015
by
Zach Reizner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use std::bsearch in LookupByKey for binary search
parent
f64e0408
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
21 deletions
+21
-21
flatbuffers.h
include/flatbuffers/flatbuffers.h
+21
-21
No files found.
include/flatbuffers/flatbuffers.h
View file @
c8f1682e
...
@@ -305,28 +305,17 @@ public:
...
@@ -305,28 +305,17 @@ public:
}
}
template
<
typename
K
>
return_type
LookupByKey
(
K
key
)
const
{
template
<
typename
K
>
return_type
LookupByKey
(
K
key
)
const
{
auto
span
=
size
();
std
::
size_t
count
=
size
();
uoffset_t
start
=
0
;
void
*
search_result
=
std
::
bsearch
(
&
key
,
Data
(),
count
,
// Perform binary search for key.
IndirectHelper
<
T
>::
element_stride
,
KeyCompare
<
K
>
);
while
(
span
)
{
// Compare against middle element of current span.
if
(
!
search_result
)
{
auto
middle
=
span
/
2
;
return
nullptr
;
// Key not found.
auto
table
=
Get
(
start
+
middle
);
auto
comp
=
table
->
KeyCompareWithValue
(
key
);
if
(
comp
>
0
)
{
// Greater than. Adjust span and try again.
span
=
middle
;
}
else
if
(
comp
<
0
)
{
// Less than. Adjust span and try again.
middle
++
;
start
+=
middle
;
span
-=
middle
;
}
else
{
// Found element.
return
table
;
}
}
}
return
nullptr
;
// Key not found.
const
uint8_t
*
data
=
reinterpret_cast
<
const
uint8_t
*>
(
search_result
);
return
IndirectHelper
<
T
>::
Read
(
data
,
0
);
}
}
protected
:
protected
:
...
@@ -335,6 +324,17 @@ protected:
...
@@ -335,6 +324,17 @@ protected:
Vector
();
Vector
();
uoffset_t
length_
;
uoffset_t
length_
;
private
:
template
<
typename
K
>
static
int
KeyCompare
(
const
void
*
ap
,
const
void
*
bp
)
{
const
K
*
key
=
reinterpret_cast
<
const
K
*>
(
ap
);
const
uint8_t
*
data
=
reinterpret_cast
<
const
uint8_t
*>
(
bp
);
auto
table
=
IndirectHelper
<
T
>::
Read
(
data
,
0
);
// std::bsearch compares with the operands transposed, so we negate the
// result here.
return
-
table
->
KeyCompareWithValue
(
*
key
);
}
};
};
// Convenient helper function to get the length of any vector, regardless
// Convenient helper function to get the length of any vector, regardless
...
...
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