[Section] slices instead of ArrayLists

It will need to be only indexed and interated anyway.
This commit is contained in:
Motiejus Jakštys 2022-03-02 07:15:08 +02:00 committed by Motiejus Jakštys
parent 9721e1be7f
commit 4fc54e5b65

View File

@ -28,8 +28,8 @@ const Corpus = struct {
uid2user: AutoHashMap(u32, *const User),
name2group: StringHashMap(*const Group),
gid2group: AutoHashMap(u32, *const Group),
groupname2users: StringHashMap(ArrayListUnmanaged(*const User)),
username2groups: StringHashMap(ArrayListUnmanaged(*const Group)),
groupname2users: StringHashMap([]*const User),
username2groups: StringHashMap([]*const Group),
pub const initErr = Allocator.Error ||
error{InvalidUtf8} ||
@ -82,8 +82,12 @@ const Corpus = struct {
res2.value_ptr.* = group;
}
var groupname2users = StringHashMap(ArrayListUnmanaged(*const User)).init(allocator);
var username2groups = StringHashMap(ArrayListUnmanaged(*const Group)).init(allocator);
var groupname2users = StringHashMap(
ArrayListUnmanaged(*const User),
).init(baseAllocator);
var username2groups = StringHashMap(
ArrayListUnmanaged(*const Group),
).init(baseAllocator);
for (groups) |*group| {
var members = try ArrayListUnmanaged(*const User).initCapacity(
allocator,
@ -122,6 +126,20 @@ const Corpus = struct {
sort.sort(*const Group, userGroups.items, {}, cmpGroupPtr);
}
var groupname2users_final = StringHashMap([]*const User).init(allocator);
for (groups) |group| {
const groupUsers = groupname2users.get(group.name).?.toOwnedSlice(allocator);
try groupname2users_final.put(group.name, groupUsers);
}
groupname2users.deinit();
var username2groups_final = StringHashMap([]*const Group).init(allocator);
for (users) |user| {
const userGroups = username2groups.get(user.name).?.toOwnedSlice(allocator);
try username2groups_final.put(user.name, userGroups);
}
username2groups.deinit();
return Corpus{
.arena = arena,
.users = users,
@ -130,8 +148,8 @@ const Corpus = struct {
.uid2user = uid2user,
.name2group = name2group,
.gid2group = gid2group,
.groupname2users = groupname2users,
.username2groups = username2groups,
.groupname2users = groupname2users_final,
.username2groups = username2groups_final,
};
}
@ -288,13 +306,13 @@ test "test corpus" {
try testing.expectEqual(corpus.gid2group.get(42), null);
try testing.expectEqual(corpus.gid2group.get(1000).?.gid, 1000);
const membersOfAll = corpus.groupname2users.get("all").?.items;
const membersOfAll = corpus.groupname2users.get("all").?;
try testing.expectEqualStrings(membersOfAll[0].name, "Name" ** 8);
try testing.expectEqualStrings(membersOfAll[1].name, "svc-bar");
try testing.expectEqualStrings(membersOfAll[2].name, "vidmantas");
try testing.expectEqual(corpus.groupname2users.get("404"), null);
const groupsOfVidmantas = corpus.username2groups.get("vidmantas").?.items;
const groupsOfVidmantas = corpus.username2groups.get("vidmantas").?;
try testing.expectEqual(groupsOfVidmantas[0].gid, 1000);
try testing.expectEqual(groupsOfVidmantas[1].gid, 1234);
try testing.expectEqual(groupsOfVidmantas[2].gid, 9999);