Add copilot_serialization
dblume

dblume commited on 2023-11-14 14:51:58
Showing 3 changed files, with 38 additions and 1 deletions.

... ...
@@ -7,7 +7,7 @@ OBJDIR=obj
7 7
 
8 8
 SOURCES=main.cpp main_helper.cpp ui/widget.cpp scoped_set_adder.cpp \
9 9
 	thread_with_lambda.cpp thread_manager.cpp deadlock/deadlock.cpp \
10
-	next_power_of_two
10
+	next_power_of_two copilot_serialize.cpp
11 11
 # //OBJECTS=$(OBJDIR)$(SOURCES:.cpp=.o)
12 12
 OBJECTS=$(foreach bname, $(basename $(SOURCES)), $(OBJDIR)/$(bname).o)
13 13
 EXECUTABLE=testcode
... ...
@@ -0,0 +1,29 @@
1
+#include <vector>
2
+
3
+// GitHub CoPilot generated this
4
+
5
+// This code will serialize a number using the minimum number of bytes.
6
+// Each byte will contain 7 bits of the number and the 8th bit will be
7
+// used to indicate if there are more bytes to follow
8
+
9
+std::vector<unsigned char> serialize(unsigned long long value) {
10
+    std::vector<unsigned char> result;
11
+    do {
12
+        unsigned char byte = value & 0x7F;
13
+        value >>= 7;
14
+        if (value > 0) {
15
+            byte |= 0x80;
16
+        }
17
+        result.push_back(byte);
18
+    } while (value > 0);
19
+    return result;
20
+}
21
+
22
+unsigned long long deserialize(std::vector<unsigned char> const& bytes) {
23
+    unsigned long long result = 0;
24
+    for (auto it = bytes.rbegin(); it != bytes.rend(); ++it) {
25
+        result <<= 7;
26
+        result |= *it & 0x7F;
27
+    }
28
+    return result;
29
+}
... ...
@@ -0,0 +1,8 @@
1
+#pragma once
2
+#include <vector>
3
+
4
+// GitHub CoPilot generated the implementation
5
+
6
+std::vector<unsigned char> serialize(unsigned long long value);
7
+
8
+unsigned long long deserialize(std::vector<unsigned char> const& bytes);
0 9