Skip to content

Commit 376ab9b

Browse files
committed
misc: Update README.md
1 parent 866d055 commit 376ab9b

1 file changed

Lines changed: 168 additions & 10 deletions

File tree

README.md

Lines changed: 168 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,172 @@
1-
# lsm-tree
1+
# byte_span
22

3-
This is the lsm-tree project.
3+
`byte_span` is a lightweight view class for efficient handling of byte data. Based on the concept of `std::span`, it provides specialized functionality for byte data manipulation.
4+
5+
## Features
6+
7+
- Non-owning view type
8+
- Unified handling of byte-like types (`std::byte`, `char`, `unsigned char`)
9+
- View trivially copyable data types as byte sequences
10+
- Type-safe design using C++20 concepts
11+
- Zero-overhead abstraction
12+
13+
## Basic Usage
14+
15+
### 1. Creating from Byte Arrays
16+
17+
The most basic usage is creating a `byte_span` from a byte array:
18+
19+
```cpp
20+
std::byte data[4] = {
21+
std::byte{0x00}, std::byte{0x01},
22+
std::byte{0x02}, std::byte{0x03}
23+
};
24+
byte_span view{data}; // Deduced as byte_span<std::byte, 4>
25+
```
26+
27+
### 2. Creating from std::array
28+
29+
Easy creation from `std::array`:
30+
31+
```cpp
32+
std::array<std::byte, 4> arr = {
33+
std::byte{0x00}, std::byte{0x01},
34+
std::byte{0x02}, std::byte{0x03}
35+
};
36+
byte_span view{arr}; // Deduced as byte_span<std::byte, 4>
37+
```
38+
39+
### 3. Creating from std::vector
40+
41+
You can create views from any contiguous container, including `std::vector`:
42+
43+
```cpp
44+
// Integer data
45+
std::vector<int> numbers = {1, 2, 3, 4};
46+
byte_span view{numbers}; // View integers as bytes
47+
48+
// Access underlying bytes
49+
auto first_byte = view[0];
50+
auto size_in_bytes = view.size(); // size = sizeof(int) * numbers.size()
51+
```
52+
53+
### 4. Dynamic Size Views
54+
55+
For runtime-sized views, use `dynamic_extent`:
56+
57+
```cpp
58+
std::vector<std::byte> vec(100);
59+
byte_span view{vec}; // Deduced as byte_span<std::byte, dynamic_extent>
60+
```
61+
62+
## Advanced Usage
63+
64+
### 1. Using byte_view and cbyte_view
65+
66+
The library provides two convenient type aliases:
67+
- `byte_view`: Mutable byte span (alias for `byte_span<std::byte>`)
68+
- `cbyte_view`: Immutable byte span (alias for `byte_span<const std::byte>`)
69+
70+
Example usage in functions:
71+
72+
```cpp
73+
// Function accepting read-only byte view
74+
void print_hex_dump(cbyte_view data) {
75+
for (auto b : data) {
76+
std::cout << std::format("{:02x} ", std::to_integer<int>(b));
77+
}
78+
}
79+
80+
// Function accepting mutable byte view
81+
void fill_pattern(byte_view data) {
82+
for (size_t i = 0; i < data.size(); ++i) {
83+
data[i] = std::byte{static_cast<unsigned char>(i & 0xFF)};
84+
}
85+
}
86+
87+
// Usage
88+
std::vector<std::byte> buffer(100);
89+
fill_pattern(buffer); // Modify buffer
90+
print_hex_dump(buffer); // Read buffer
91+
```
92+
93+
### 2. Trivially Copyable Types as Byte Views
94+
95+
Any trivially copyable type can be viewed as bytes:
96+
97+
```cpp
98+
struct Point {
99+
float x;
100+
float y;
101+
};
102+
103+
Point p{1.0f, 2.0f};
104+
byte_span view{std::span{&p, 1}}; // View Point's bytes
105+
```
106+
107+
### 3. String Data Handling
108+
109+
String data can be easily handled:
110+
111+
```cpp
112+
std::string str = "Hello, World!";
113+
byte_span view{str}; // View string data as bytes
114+
115+
// Convert to string_view
116+
auto sv = as_sv(view); // Get as std::string_view
117+
```
118+
119+
### 4. Implicit Conversion in Function Arguments
120+
121+
Functions can accept various container types through implicit conversion:
122+
123+
```cpp
124+
void process_bytes(byte_span view) {
125+
// Process byte data
126+
}
127+
128+
// Various ways to call
129+
std::vector<std::byte> vec(100);
130+
process_bytes(vec); // OK
131+
132+
std::array<char, 10> arr;
133+
process_bytes(arr); // OK
134+
135+
unsigned char raw_data[20];
136+
process_bytes(raw_data); // OK
137+
```
138+
139+
### 5. Data Type Conversions
140+
141+
Safe data type conversions:
142+
143+
```cpp
144+
std::vector<int> numbers = {1, 2, 3, 4};
145+
byte_span view{numbers};
146+
147+
// Read as span of ints
148+
auto int_span = as_span<int>(view);
149+
// Get first value
150+
auto& first_int = as_value<int>(view);
151+
```
152+
153+
### 6. Creating Sub-views
154+
155+
Create views of specific ranges:
156+
157+
```cpp
158+
std::vector<std::byte> data(100);
159+
byte_span view{data};
160+
161+
auto first_10 = view.first(10); // First 10 bytes
162+
auto last_10 = view.last(10); // Last 10 bytes
163+
auto sub_view = view.subspan(5, 20); // 20 bytes starting at offset 5
164+
```
165+
166+
## Requirements
167+
168+
- C++20 or later
169+
- Compiler with concepts support
4170

5171
# Building and installing
6172

@@ -9,11 +175,3 @@ See the [BUILDING](BUILDING.md) document.
9175
# Contributing
10176

11177
See the [CONTRIBUTING](CONTRIBUTING.md) document.
12-
13-
# Licensing
14-
15-
<!--
16-
Please go to https://choosealicense.com/licenses/ and choose a license that
17-
fits your needs. The recommended license for a project of this type is the
18-
Boost Software License 1.0.
19-
-->

0 commit comments

Comments
 (0)