Skip to content

Commit

Permalink
continuing layout example
Browse files Browse the repository at this point in the history
  • Loading branch information
thatstoasty committed Apr 10, 2024
1 parent 7af6372 commit c351b1c
Show file tree
Hide file tree
Showing 10 changed files with 471 additions and 126 deletions.
9 changes: 8 additions & 1 deletion examples/table/pokemon.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 110,14 @@ fn main() raises:
border_style.foreground("238")
# var table = mog.new_table()
var table = mog.Table(
style_function=make_style_func[style, header_style, selected_style, type_colors, dim_type_colors, data](),
style_function=make_style_func[
style,
header_style,
selected_style,
type_colors,
dim_type_colors,
data,
](),
border=rounded_border(),
border_style=border_style,
border_bottom=True,
Expand Down
25 changes: 20 additions & 5 deletions external/string_dict/ahasher.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 22,9 @@ fn read_small(data: DTypePointer[DType.uint8], length: Int) -> U128:
if length >= 4:
# len 4-8
var a = data.bitcast[DType.uint32]().load().cast[DType.uint64]()
var b = data.offset(length - 4).bitcast[DType.uint32]().load().cast[DType.uint64]()
var b = data.offset(length - 4).bitcast[DType.uint32]().load().cast[
DType.uint64
]()
return U128(a, b)
else:
var a = data.bitcast[DType.uint16]().load().cast[DType.uint64]()
Expand All @@ -35,13 37,19 @@ fn read_small(data: DTypePointer[DType.uint8], length: Int) -> U128:
else:
return U128(0, 0)


struct AHasher:
var buffer: UInt64
var pad: UInt64
var extra_keys: U128

fn __init__(inout self, key: U256):
var pi_key = key ^ U256(0x243f_6a88_85a3_08d3, 0x1319_8a2e_0370_7344, 0xa409_3822_299f_31d0, 0x082e_fa98_ec4e_6c89,)
var pi_key = key ^ U256(
0x243F_6A88_85A3_08D3,
0x1319_8A2E_0370_7344,
0xA409_3822_299F_31D0,
0x082E_FA98_EC4E_6C89,
)
self.buffer = pi_key[0]
self.pad = pi_key[1]
self.extra_keys = U128(pi_key[2], pi_key[3])
Expand Down Expand Up @@ -72,11 80,15 @@ struct AHasher:
self.buffer = (self.buffer length) * MULTIPLE
if length > 8:
if length > 16:
var tail = data.offset(length - 16).bitcast[DType.uint64]().load[width=2]()
var tail = data.offset(length - 16).bitcast[
DType.uint64
]().load[width=2]()
self.large_update(tail)
var offset = 0
while length - offset > 16:
var block = data.offset(offset).bitcast[DType.uint64]().load[width=2]()
var block = data.offset(offset).bitcast[
DType.uint64
]().load[width=2]()
self.large_update(block)
offset = 16
else:
Expand All @@ -87,6 99,7 @@ struct AHasher:
var value = read_small(data, length)
self.large_update(value)


@always_inline
fn ahash(s: String) -> UInt64:
var length = len(s)
Expand All @@ -97,7 110,9 @@ fn ahash(s: String) -> UInt64:
hasher.write(b, length)
else:
var value = read_small(b, length)
hasher.buffer = folded_multiply(value[0] ^ hasher.buffer, value[1] ^ hasher.extra_keys[1])
hasher.buffer = folded_multiply(
value[0] ^ hasher.buffer, value[1] ^ hasher.extra_keys[1]
)
hasher.pad = hasher.pad length

return hasher.finish()
12 changes: 6 additions & 6 deletions external/string_dict/keys_container.mojo
Original file line number Diff line number Diff line change
@@ -1,5 1,6 @@
from collections.vector import InlinedFixedVector


struct KeysContainer[KeyEndType: DType = DType.uint32](Sized):
var keys: DTypePointer[DType.int8]
var allocated_bytes: Int
Expand All @@ -9,11 10,11 @@ struct KeysContainer[KeyEndType: DType = DType.uint32](Sized):

fn __init__(inout self, capacity: Int):
constrained[
KeyEndType == DType.uint8 or
KeyEndType == DType.uint16 or
KeyEndType == DType.uint32 or
KeyEndType == DType.uint64,
"KeyEndType needs to be an unsigned integer"
KeyEndType == DType.uint8
or KeyEndType == DType.uint16
or KeyEndType == DType.uint32
or KeyEndType == DType.uint64,
"KeyEndType needs to be an unsigned integer",
]()
self.allocated_bytes = capacity << 3
self.keys = DTypePointer[DType.int8].alloc(self.allocated_bytes)
Expand Down Expand Up @@ -71,7 72,6 @@ struct KeysContainer[KeyEndType: DType = DType.uint32](Sized):
self.keys_end.store(self.count, new_end)
self.count = count


@always_inline
fn get(self, index: Int) -> StringRef:
if index < 0 or index >= self.count:
Expand Down
7 changes: 6 additions & 1 deletion external/string_dict/string_eq.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 7,12 @@ fn eq(a: StringRef, b: String) -> Bool:
var p2 = b._as_ptr()
var offset = 0
alias step = 16
while l - offset >= step and (p1.load[width=step](offset) == p2.load[width=step](offset)).reduce_and():
while (
l - offset >= step
and (
p1.load[width=step](offset) == p2.load[width=step](offset)
).reduce_and()
):
offset = step
if l - offset >= step:
return False
Expand Down
1 change: 1 addition & 0 deletions mog/__init__.mojo
Original file line number Diff line number Diff line change
@@ -1,3 1,4 @@
from .style import Style
from .border import Border
from .table import Table, new_table, new_string_data, default_styles
from .position import top, bottom, center, left, right
8 changes: 6 additions & 2 deletions mog/join.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 86,9 @@ fn join_horizontal(pos: Position, *strs: String) raises -> String:
_ = builder.write_string(block[i])

# Also make lines the same length
var spaces = repeat(" ", max_widths[j] - printable_rune_width(block[i]))
var spaces = repeat(
" ", max_widths[j] - printable_rune_width(block[i])
)
_ = builder.write_string(spaces)

if i < len(blocks[0]) - 1:
Expand Down Expand Up @@ -176,7 178,9 @@ fn join_horizontal(pos: Position, strs: List[String]) raises -> String:
_ = builder.write_string(block[i])

# Also make lines the same length
var spaces = repeat("", max_widths[j] - printable_rune_width(block[i]))
var spaces = repeat(
"", max_widths[j] - printable_rune_width(block[i])
)
_ = builder.write_string(spaces)

if i < len(blocks[0]) - 1:
Expand Down
Loading

0 comments on commit c351b1c

Please sign in to comment.