mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 06:14:46 +00:00
Ran Zig fmt on dict.zig
This commit is contained in:
parent
2e1d1a7602
commit
f4a85160d1
1 changed files with 48 additions and 78 deletions
|
@ -26,37 +26,26 @@ pub fn RocDict(
|
|||
pub const Overflow = struct {
|
||||
entries: *[size]?*Entry,
|
||||
|
||||
// This function takes an
|
||||
// This function takes an
|
||||
// index, key, and value,
|
||||
// because presumably overflow
|
||||
// is only created once an
|
||||
// is only created once an
|
||||
// there is a collision between
|
||||
// two entries, and there is
|
||||
// therefore an extra entry
|
||||
// that needs to be stored
|
||||
pub fn new(
|
||||
index: u64,
|
||||
key_ptr: *Key,
|
||||
value_ptr: *Value
|
||||
) Overflow {
|
||||
var init_entries : [size]?*Entry = undefined;
|
||||
pub fn new(index: u64, key_ptr: *Key, value_ptr: *Value) Overflow {
|
||||
var init_entries: [size]?*Entry = undefined;
|
||||
|
||||
var new_overflow = Overflow {
|
||||
.entries = &init_entries
|
||||
};
|
||||
var new_overflow = Overflow{ .entries = &init_entries };
|
||||
|
||||
new_overflow.set_at_index(index, key_ptr, value_ptr);
|
||||
|
||||
return new_overflow;
|
||||
}
|
||||
|
||||
pub fn set_at_index(
|
||||
self: *Overflow,
|
||||
index: u64,
|
||||
key_ptr: *Key,
|
||||
value_ptr: *Value
|
||||
) void {
|
||||
var overflow = self.*;
|
||||
pub fn set_at_index(self: *Overflow, index: u64, key_ptr: *Key, value_ptr: *Value) void {
|
||||
var overflow = self.*;
|
||||
var new_entry = Entry.new(key_ptr, value_ptr);
|
||||
|
||||
overflow.entries.*[index] = &new_entry;
|
||||
|
@ -71,10 +60,10 @@ pub fn RocDict(
|
|||
};
|
||||
|
||||
pub fn new(key_ptr: *Key, value_ptr: *Value) Entry {
|
||||
return Entry {
|
||||
return Entry{
|
||||
.value_ptr = value_ptr,
|
||||
.key_ptr = key_ptr,
|
||||
.overflow = null
|
||||
.overflow = null,
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -112,21 +101,15 @@ pub fn RocDict(
|
|||
|
||||
return next_entry.get(key, next_level);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The bool this function returns
|
||||
// represents if an new entry was
|
||||
// added (true), or if an old one
|
||||
// was updated (false);
|
||||
pub fn insert_into_overflow(
|
||||
self: *Entry,
|
||||
key_ptr: *Key,
|
||||
value_ptr: *Value,
|
||||
level: u64
|
||||
) bool {
|
||||
pub fn insert_into_overflow(self: *Entry, key_ptr: *Key, value_ptr: *Value, level: u64) bool {
|
||||
const key = key_ptr.*;
|
||||
|
||||
const index = key_to_index_at_level(key, level);
|
||||
|
@ -135,7 +118,7 @@ pub fn RocDict(
|
|||
|
||||
// If there is no overflow, make a new OverFlow
|
||||
if (entry.overflow == null) {
|
||||
var new_overflow : Overflow = Overflow.new(index, key_ptr, value_ptr);
|
||||
var new_overflow: Overflow = Overflow.new(index, key_ptr, value_ptr);
|
||||
|
||||
entry.overflow = new_overflow;
|
||||
|
||||
|
@ -165,11 +148,7 @@ pub fn RocDict(
|
|||
overflow_entry.set_value(value_ptr);
|
||||
return false;
|
||||
} else {
|
||||
return overflow_entry.insert_into_overflow(
|
||||
key_ptr,
|
||||
value_ptr,
|
||||
level + 1
|
||||
);
|
||||
return overflow_entry.insert_into_overflow(key_ptr, value_ptr, level + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -189,12 +168,12 @@ pub fn RocDict(
|
|||
const maybe_entry = self.entries[index];
|
||||
|
||||
if (maybe_entry == null) {
|
||||
return Query { .maybe_entry = null };
|
||||
return Query{ .maybe_entry = null };
|
||||
} else {
|
||||
var entry = maybe_entry.?;
|
||||
|
||||
if (entry.key_ptr.* == key) {
|
||||
return Query { .maybe_entry = entry };
|
||||
return Query{ .maybe_entry = entry };
|
||||
} else {
|
||||
return self.query(key, level + 1);
|
||||
}
|
||||
|
@ -216,15 +195,13 @@ pub fn RocDict(
|
|||
entry.* = null;
|
||||
}
|
||||
|
||||
return Self {
|
||||
return Self{
|
||||
.len = 0,
|
||||
.entries = init_entries,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
pub fn get(self: Self, key: Key) ?*Value {
|
||||
|
||||
const q = self.query(key, 0);
|
||||
|
||||
if (q.maybe_entry == null) {
|
||||
|
@ -242,7 +219,7 @@ pub fn RocDict(
|
|||
}
|
||||
}
|
||||
|
||||
pub fn insert(self: *Self, key_ptr: *Key, value_ptr: *Value) void {
|
||||
pub fn insert(self: *Self, key_ptr: *Key, value_ptr: *Value) void {
|
||||
const level = 0;
|
||||
|
||||
const key = key_ptr.*;
|
||||
|
@ -252,13 +229,11 @@ pub fn RocDict(
|
|||
const index = key_to_index_at_level(key, level);
|
||||
|
||||
if (q.maybe_entry == null) {
|
||||
|
||||
var new_entry = Entry.new(value_ptr, key_ptr);
|
||||
|
||||
self.entries[index] = new_entry;
|
||||
|
||||
self.*.len += 1;
|
||||
|
||||
} else {
|
||||
var entry = q.maybe_entry.?;
|
||||
|
||||
|
@ -285,10 +260,10 @@ pub fn RocDict(
|
|||
return false;
|
||||
}
|
||||
|
||||
var levels_count : u64 = self.entries.len;
|
||||
var levels_count: u64 = self.entries.len;
|
||||
var are_same = true;
|
||||
|
||||
var i : u64 = 0;
|
||||
var i: u64 = 0;
|
||||
while ((i < size) and are_same) {
|
||||
const maybe_entry = self.entries[i];
|
||||
const maybe_other_entry = other.entries[i];
|
||||
|
@ -320,9 +295,9 @@ pub fn RocDict(
|
|||
}
|
||||
|
||||
test "RocDict.insert with hash collisions" {
|
||||
var dict = RocDict(u64,u64).init(testing.allocator);
|
||||
var dict = RocDict(u64, u64).init(testing.allocator);
|
||||
|
||||
var i : u64 = 0;
|
||||
var i: u64 = 0;
|
||||
|
||||
while (i < (size * 2)) {
|
||||
dict.insert(&i, &i);
|
||||
|
@ -341,16 +316,16 @@ test "RocDict.insert with hash collisions" {
|
|||
}
|
||||
|
||||
test "repeated RocDict.insert" {
|
||||
var dict = RocDict(u64,u64).init(testing.allocator);
|
||||
var dict = RocDict(u64, u64).init(testing.allocator);
|
||||
|
||||
var index : u64 = 0;
|
||||
var fst_val : u64 = 17;
|
||||
var snd_val : u64 = 49;
|
||||
var index: u64 = 0;
|
||||
var fst_val: u64 = 17;
|
||||
var snd_val: u64 = 49;
|
||||
|
||||
dict.insert(&index, &fst_val);
|
||||
dict.insert(&index, &snd_val);
|
||||
|
||||
var value_ptr : ?*u64 = dict.get(index);
|
||||
var value_ptr: ?*u64 = dict.get(index);
|
||||
|
||||
if (value_ptr == null) {
|
||||
unreachable;
|
||||
|
@ -361,76 +336,71 @@ test "repeated RocDict.insert" {
|
|||
}
|
||||
|
||||
test "RocDict.eq" {
|
||||
var fst = RocDict(u64,u64).init(testing.allocator);
|
||||
var snd = RocDict(u64,u64).init(testing.allocator);
|
||||
var fst = RocDict(u64, u64).init(testing.allocator);
|
||||
var snd = RocDict(u64, u64).init(testing.allocator);
|
||||
|
||||
var key : u64 = 0;
|
||||
var value : u64 = 30;
|
||||
var key: u64 = 0;
|
||||
var value: u64 = 30;
|
||||
|
||||
fst.insert(&key, &value);
|
||||
snd.insert(&key, &value);
|
||||
assert(fst.eq(snd));
|
||||
|
||||
var empty = RocDict(u64,u64).init(testing.allocator);
|
||||
var empty = RocDict(u64, u64).init(testing.allocator);
|
||||
assert(!fst.eq(empty));
|
||||
|
||||
var trd = RocDict(u64,u64).init(testing.allocator);
|
||||
var new_value : u64 = value + 1;
|
||||
var trd = RocDict(u64, u64).init(testing.allocator);
|
||||
var new_value: u64 = value + 1;
|
||||
trd.insert(&key, &new_value);
|
||||
|
||||
assert(!fst.eq(trd));
|
||||
}
|
||||
|
||||
|
||||
|
||||
test "RocDict.get_len" {
|
||||
var dict = RocDict(u64,u64).init(testing.allocator);
|
||||
var dict = RocDict(u64, u64).init(testing.allocator);
|
||||
|
||||
var key : u64 = 0;
|
||||
var key: u64 = 0;
|
||||
|
||||
var value : u64 = 16;
|
||||
var value: u64 = 16;
|
||||
dict.insert(&key, &value);
|
||||
|
||||
const expect_len : u64 = 1;
|
||||
const expect_len: u64 = 1;
|
||||
expectEqual(dict.get_len(), expect_len);
|
||||
|
||||
dict.insert(&key, &value);
|
||||
|
||||
expectEqual(dict.get_len(), expect_len);
|
||||
|
||||
var next_key : u64 = key + 1;
|
||||
var next_value : u64 = 3 ;
|
||||
var next_key: u64 = key + 1;
|
||||
var next_value: u64 = 3;
|
||||
dict.insert(&next_key, &next_value);
|
||||
|
||||
var next_expected_len : u64 = 2;
|
||||
var next_expected_len: u64 = 2;
|
||||
expectEqual(dict.get_len(), next_expected_len);
|
||||
}
|
||||
|
||||
|
||||
test "RocDict.insert" {
|
||||
var dict = RocDict(u64,u64).init(testing.allocator);
|
||||
var dict = RocDict(u64, u64).init(testing.allocator);
|
||||
|
||||
var index : u64 = 0;
|
||||
var value : u64 = 30;
|
||||
var index: u64 = 0;
|
||||
var value: u64 = 30;
|
||||
|
||||
dict.insert(&index, &value);
|
||||
|
||||
var result_value : ?*u64 = dict.get(index);
|
||||
var result_value: ?*u64 = dict.get(index);
|
||||
expectEqual(result_value.?.*, value);
|
||||
|
||||
var expect_len : u64 = 1;
|
||||
var expect_len: u64 = 1;
|
||||
expectEqual(dict.get_len(), expect_len);
|
||||
}
|
||||
|
||||
|
||||
test "RocDict.get" {
|
||||
const empty = RocDict(u64, u64).init(testing.allocator);
|
||||
|
||||
const expect : ?*u64 = null;
|
||||
const expect: ?*u64 = null;
|
||||
expectEqual(empty.get(29), expect);
|
||||
}
|
||||
|
||||
|
||||
test "RocDict.init" {
|
||||
const empty = RocDict(u64, u64).init(testing.allocator);
|
||||
expectEqual(empty.get_len(), 0);
|
||||
|
@ -441,6 +411,6 @@ test "RocDict.init" {
|
|||
|
||||
const empty_made_up = RocDict(u64, MadeUpType).init(testing.allocator);
|
||||
|
||||
const expect : u64 = 0;
|
||||
const expect: u64 = 0;
|
||||
expectEqual(empty_made_up.get_len(), expect);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue