-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
zig build system improvements, add some std API
* add std.buf_map.BufMap * add std.buf_set.BufSet * add std.mem.split * zig build system improvements (See #204) - automatically parses NIX_CFLAGS_COMPILE and NIX_LDFLAGS - add builder.addCIncludePath - add builder.addRPath - add builder.addLibPath - add exe.linkLibrary
- Loading branch information
Showing
9 changed files
with
297 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const HashMap = @import("hash_map.zig").HashMap; | ||
const mem = @import("mem.zig"); | ||
const Allocator = mem.Allocator; | ||
|
||
/// BufMap copies keys and values before they go into the map, and | ||
/// frees them when they get removed. | ||
pub const BufMap = struct { | ||
hash_map: BufMapHashMap, | ||
|
||
const BufMapHashMap = HashMap([]const u8, []const u8, mem.hash_slice_u8, mem.eql_slice_u8); | ||
|
||
pub fn init(allocator: &Allocator) -> BufMap { | ||
var self = BufMap { | ||
.hash_map = undefined, | ||
}; | ||
self.hash_map.init(allocator); | ||
return self; | ||
} | ||
|
||
pub fn deinit(self: &BufMap) { | ||
var it = self.hash_map.entryIterator(); | ||
while (true) { | ||
const entry = it.next() ?? break; | ||
self.free(entry.key); | ||
self.free(entry.value); | ||
} | ||
|
||
self.hash_map.deinit(); | ||
} | ||
|
||
pub fn set(self: &BufMap, key: []const u8, value: []const u8) -> %void { | ||
if (const entry ?= self.hash_map.get(key)) { | ||
const value_copy = %return self.copy(value); | ||
%defer self.free(value_copy); | ||
%return self.hash_map.put(key, value_copy); | ||
self.free(entry.value); | ||
} else { | ||
const key_copy = %return self.copy(key); | ||
%defer self.free(key_copy); | ||
const value_copy = %return self.copy(value); | ||
%defer self.free(value_copy); | ||
%return self.hash_map.put(key_copy, value_copy); | ||
} | ||
} | ||
|
||
pub fn delete(self: &BufMap, key: []const u8) { | ||
const entry = self.hash_map.remove(key) ?? return; | ||
self.free(entry.key); | ||
self.free(entry.value); | ||
} | ||
|
||
pub fn count(self: &const BufMap) -> usize { | ||
return self.hash_map.size; | ||
} | ||
|
||
pub fn iterator(self: &const BufMap) -> BufMapHashMap.Iterator { | ||
return self.hash_map.entryIterator(); | ||
} | ||
|
||
fn free(self: &BufMap, value: []const u8) { | ||
// remove the const | ||
const mut_value = @ptrcast(&u8, value.ptr)[0...value.len]; | ||
self.hash_map.allocator.free(mut_value); | ||
} | ||
|
||
fn copy(self: &BufMap, value: []const u8) -> %[]const u8 { | ||
const result = %return self.hash_map.allocator.alloc(u8, value.len); | ||
mem.copy(u8, result, value); | ||
return result; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const HashMap = @import("hash_map.zig").HashMap; | ||
const mem = @import("mem.zig"); | ||
const Allocator = mem.Allocator; | ||
|
||
pub const BufSet = struct { | ||
hash_map: BufSetHashMap, | ||
|
||
const BufSetHashMap = HashMap([]const u8, void, mem.hash_slice_u8, mem.eql_slice_u8); | ||
|
||
pub fn init(allocator: &Allocator) -> BufSet { | ||
var self = BufSet { | ||
.hash_map = undefined, | ||
}; | ||
self.hash_map.init(allocator); | ||
return self; | ||
} | ||
|
||
pub fn deinit(self: &BufSet) { | ||
var it = self.hash_map.entryIterator(); | ||
while (true) { | ||
const entry = it.next() ?? break; | ||
self.free(entry.key); | ||
} | ||
|
||
self.hash_map.deinit(); | ||
} | ||
|
||
pub fn put(self: &BufSet, key: []const u8) -> %void { | ||
if (self.hash_map.get(key) == null) { | ||
const key_copy = %return self.copy(key); | ||
%defer self.free(key_copy); | ||
%return self.hash_map.put(key_copy, {}); | ||
} | ||
} | ||
|
||
pub fn delete(self: &BufSet, key: []const u8) { | ||
const entry = self.hash_map.remove(key) ?? return; | ||
self.free(entry.key); | ||
} | ||
|
||
pub fn count(self: &const BufSet) -> usize { | ||
return self.hash_map.size; | ||
} | ||
|
||
pub fn iterator(self: &const BufSet) -> BufSetHashMap.Iterator { | ||
return self.hash_map.entryIterator(); | ||
} | ||
|
||
fn free(self: &BufSet, value: []const u8) { | ||
// remove the const | ||
const mut_value = @ptrcast(&u8, value.ptr)[0...value.len]; | ||
self.hash_map.allocator.free(mut_value); | ||
} | ||
|
||
fn copy(self: &BufSet, value: []const u8) -> %[]const u8 { | ||
const result = %return self.hash_map.allocator.alloc(u8, value.len); | ||
mem.copy(u8, result, value); | ||
return result; | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.