Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
C
capnproto
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
capnproto
Commits
47194adc
Commit
47194adc
authored
May 31, 2013
by
Kenton Varda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move FixedArray and CappedArray to array.h.
parent
ad611c13
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
80 deletions
+66
-80
array.h
c++/src/kj/array.h
+66
-0
util.h
c++/src/kj/util.h
+0
-80
No files found.
c++/src/kj/array.h
View file @
47194adc
...
...
@@ -194,6 +194,11 @@ template <typename T> Array<T> heapArray(ArrayPtr<const T> content);
template
<
typename
T
,
typename
Iterator
>
Array
<
T
>
heapArray
(
Iterator
begin
,
Iterator
end
);
// Allocate a heap arary containing a copy of the given content.
template
<
typename
T
,
typename
Container
>
Array
<
T
>
heapArrayFromIterable
(
Container
&&
a
)
{
return
heapArray
(
a
.
begin
(),
a
.
end
());
}
template
<
typename
T
>
Array
<
T
>
heapArrayFromIterable
(
Array
<
T
>&&
a
)
{
return
mv
(
a
);
}
// =======================================================================================
// ArrayBuilder
...
...
@@ -319,6 +324,67 @@ inline ArrayBuilder<T> heapArrayBuilder(size_t size) {
internal
::
HeapArrayDisposer
::
instance
);
}
// =======================================================================================
// Inline Arrays
template
<
typename
T
,
size_t
fixedSize
>
class
FixedArray
{
// A fixed-width array whose storage is allocated inline rather than on the heap.
public
:
inline
size_t
size
()
const
{
return
fixedSize
;
}
inline
T
*
begin
()
{
return
content
;
}
inline
T
*
end
()
{
return
content
+
fixedSize
;
}
inline
const
T
*
begin
()
const
{
return
content
;
}
inline
const
T
*
end
()
const
{
return
content
+
fixedSize
;
}
inline
operator
ArrayPtr
<
T
>
()
{
return
arrayPtr
(
content
,
fixedSize
);
}
inline
operator
ArrayPtr
<
const
T
>
()
const
{
return
arrayPtr
(
content
,
fixedSize
);
}
inline
T
&
operator
[](
size_t
index
)
{
return
content
[
index
];
}
inline
const
T
&
operator
[](
size_t
index
)
const
{
return
content
[
index
];
}
private
:
T
content
[
fixedSize
];
};
template
<
typename
T
,
size_t
fixedSize
>
class
CappedArray
{
// Like `FixedArray` but can be dynamically resized as long as the size does not exceed the limit
// specified by the template parameter.
//
// TODO(someday): Don't construct elements past currentSize?
public
:
inline
constexpr
CappedArray
()
:
currentSize
(
fixedSize
)
{}
inline
explicit
constexpr
CappedArray
(
size_t
s
)
:
currentSize
(
s
)
{}
inline
size_t
size
()
const
{
return
currentSize
;
}
inline
void
setSize
(
size_t
s
)
{
currentSize
=
s
;
}
inline
T
*
begin
()
{
return
content
;
}
inline
T
*
end
()
{
return
content
+
currentSize
;
}
inline
const
T
*
begin
()
const
{
return
content
;
}
inline
const
T
*
end
()
const
{
return
content
+
currentSize
;
}
inline
operator
ArrayPtr
<
T
>
()
{
return
arrayPtr
(
content
,
currentSize
);
}
inline
operator
ArrayPtr
<
const
T
>
()
const
{
return
arrayPtr
(
content
,
currentSize
);
}
inline
T
&
operator
[](
size_t
index
)
{
return
content
[
index
];
}
inline
const
T
&
operator
[](
size_t
index
)
const
{
return
content
[
index
];
}
private
:
size_t
currentSize
;
T
content
[
fixedSize
];
};
// =======================================================================================
// Inline implementation details
...
...
c++/src/kj/util.h
View file @
47194adc
...
...
@@ -33,86 +33,6 @@
namespace
kj
{
// =======================================================================================
// Arrays
// TODO(cleanup): Move these elsewhere, maybe an array.h.
template
<
typename
T
,
size_t
fixedSize
>
class
FixedArray
{
// A fixed-width array whose storage is allocated inline rather than on the heap.
public
:
inline
size_t
size
()
const
{
return
fixedSize
;
}
inline
T
*
begin
()
{
return
content
;
}
inline
T
*
end
()
{
return
content
+
fixedSize
;
}
inline
const
T
*
begin
()
const
{
return
content
;
}
inline
const
T
*
end
()
const
{
return
content
+
fixedSize
;
}
inline
operator
ArrayPtr
<
T
>
()
{
return
arrayPtr
(
content
,
fixedSize
);
}
inline
operator
ArrayPtr
<
const
T
>
()
const
{
return
arrayPtr
(
content
,
fixedSize
);
}
inline
T
&
operator
[](
size_t
index
)
{
return
content
[
index
];
}
inline
const
T
&
operator
[](
size_t
index
)
const
{
return
content
[
index
];
}
private
:
T
content
[
fixedSize
];
};
template
<
typename
T
,
size_t
fixedSize
>
class
CappedArray
{
// Like `FixedArray` but can be dynamically resized as long as the size does not exceed the limit
// specified by the template parameter.
public
:
inline
constexpr
CappedArray
()
:
currentSize
(
fixedSize
)
{}
inline
explicit
constexpr
CappedArray
(
size_t
s
)
:
currentSize
(
s
)
{}
inline
size_t
size
()
const
{
return
currentSize
;
}
inline
void
setSize
(
size_t
s
)
{
currentSize
=
s
;
}
inline
T
*
begin
()
{
return
content
;
}
inline
T
*
end
()
{
return
content
+
currentSize
;
}
inline
const
T
*
begin
()
const
{
return
content
;
}
inline
const
T
*
end
()
const
{
return
content
+
currentSize
;
}
inline
operator
ArrayPtr
<
T
>
()
{
return
arrayPtr
(
content
,
currentSize
);
}
inline
operator
ArrayPtr
<
const
T
>
()
const
{
return
arrayPtr
(
content
,
currentSize
);
}
inline
T
&
operator
[](
size_t
index
)
{
return
content
[
index
];
}
inline
const
T
&
operator
[](
size_t
index
)
const
{
return
content
[
index
];
}
private
:
size_t
currentSize
;
T
content
[
fixedSize
];
};
template
<
typename
T
,
typename
Container
>
Array
<
T
>
iterableToArray
(
Container
&&
a
)
{
// Converts an arbitrary iterable container into an array of the given element type.
Array
<
T
>
result
=
heapArray
<
T
>
(
a
.
size
());
auto
i
=
a
.
iterator
();
auto
end
=
a
.
end
();
T
*
__restrict__
ptr
=
result
.
begin
();
while
(
i
!=
end
)
{
*
ptr
++
=
*
i
++
;
}
return
result
;
}
template
<
typename
T
>
Array
<
T
>
iterableToArray
(
Array
<
T
>&&
a
)
{
return
std
::
move
(
a
);
}
// =======================================================================================
// String stuff
...
...
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