Skip to content

Commit

Permalink
Use anonymous generic impl parameters (#197)
Browse files Browse the repository at this point in the history
<!--- Please provide a general summary of your changes in the title
above -->

## Pull Request type

<!-- Please try to limit your pull request to one type; submit multiple
pull requests if needed. -->

Please check the type of change your PR introduces:

- [ ] Bugfix
- [ ] Feature
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no API changes)
- [ ] Build-related changes
- [ ] Documentation content changes
- [ ] Other (please describe):

## What is the current behavior?

<!-- Please describe the current behavior that you are modifying, or
link to a relevant issue. -->

Issue Number: N/A

## What is the new behavior?

<!-- Please describe the behavior or changes that are being added by
this PR. -->

-
-
-

## Does this introduce a breaking change?

- [ ] Yes
- [ ] No

<!-- If this does introduce a breaking change, please describe the
impact and migration path for existing applications below. -->

## Other information

<!-- Any other information that is important to this PR, such as
screenshots of how the component looks before and after the change. -->
  • Loading branch information
maciejka authored Oct 20, 2023
1 parent 6434ba2 commit 1965197
Show file tree
Hide file tree
Showing 19 changed files with 146 additions and 272 deletions.
54 changes: 27 additions & 27 deletions src/ascii/src/integer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ trait ToAsciiArrayTrait<T> {
// e.g. 123 -> [49, 50, 51]
impl ToAsciiArrayTraitImpl<
T,
impl TPartialOrd: PartialOrd<T>,
impl TDivRem: DivRem<T>,
impl TInto: Into<T, felt252>,
impl TryInto: TryInto<felt252, T>,
impl TTryIntoZero: TryInto<T, NonZero<T>>,
impl TZeroable: Zeroable<T>,
impl TDrop: Drop<T>,
impl TCopy: Copy<T>,
+PartialOrd<T>,
+DivRem<T>,
+Into<T, felt252>,
+TryInto<felt252, T>,
+TryInto<T, NonZero<T>>,
+Zeroable<T>,
+Drop<T>,
+Copy<T>,
> of ToAsciiArrayTrait<T> {
fn to_ascii_array(self: T) -> Array<felt252> {
let mut new_arr = self.to_inverse_ascii_array();
Expand Down Expand Up @@ -54,19 +54,19 @@ impl ToAsciiArrayTraitImpl<
}
}

// generic implementation for small integers <u128
// generic implementation for small integers <u128
// to transform its integers into a string represented as a single felt252
// e.g. 1000 -> "1000"
impl SmallIntegerToAsciiTraitImpl<
T,
impl TPartialOrd: PartialOrd<T>,
impl TDivRem: DivRem<T>,
impl TInto: Into<T, felt252>,
impl TTryInto: TryInto<felt252, T>,
impl TTryIntoZero: TryInto<T, NonZero<T>>,
impl TZeroable: Zeroable<T>,
impl TDrop: Drop<T>,
impl TCopy: Copy<T>,
+PartialOrd<T>,
+DivRem<T>,
+Into<T, felt252>,
+TryInto<felt252, T>,
+TryInto<T, NonZero<T>>,
+Zeroable<T>,
+Drop<T>,
+Copy<T>,
> of ToAsciiTrait<T, felt252> {
fn to_ascii(self: T) -> felt252 {
if self <= 9.try_into().unwrap() {
Expand All @@ -86,19 +86,19 @@ impl SmallIntegerToAsciiTraitImpl<
}
}

// generic implementation for big integers u128
// generic implementation for big integers u128
// to transform its integers into a string represented as multiple felt252 if there is overflow
// e.g. max_num + 123 -> ["max_num", "123"]
impl BigIntegerToAsciiTraitImpl<
T,
impl TPartialOrd: PartialOrd<T>,
impl TDivRem: DivRem<T>,
impl TInto: Into<T, felt252>,
impl TTryInto: TryInto<felt252, T>,
impl TTryIntoZero: TryInto<T, NonZero<T>>,
impl TZeroable: Zeroable<T>,
impl TDrop: Drop<T>,
impl TCopy: Copy<T>,
+PartialOrd<T>,
+DivRem<T>,
+Into<T, felt252>,
+TryInto<felt252, T>,
+TryInto<T, NonZero<T>>,
+Zeroable<T>,
+Drop<T>,
+Copy<T>,
> of ToAsciiTrait<T, Array<felt252>> {
fn to_ascii(self: T) -> Array<felt252> {
let mut data = ArrayTrait::new();
Expand Down Expand Up @@ -141,7 +141,7 @@ impl BigIntegerToAsciiTraitImpl<
// -------------------------------------------------------------------------- //
// for u256 //
// -------------------------------------------------------------------------- //
// have to implement separately for u256 because
// have to implement separately for u256 because
// it doesn't have the same implementations as the generic version
impl U256ToAsciiArrayTraitImpl of ToAsciiArrayTrait<u256> {
fn to_ascii_array(self: u256) -> Array<felt252> {
Expand Down
6 changes: 2 additions & 4 deletions src/bytes/src/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ fn keccak_u128s_be(mut input: Span<u128>, n_bytes: usize) -> u256 {

/// return the minimal value
/// support u8, u16, u32, u64, u128, u256
fn uint_min<T, impl TDrop: Drop<T>, impl TPartialOrd: PartialOrd<T>, impl TCopy: Copy<T>>(
l: T, r: T
) -> T {
fn uint_min<T, +Drop<T>, +PartialOrd<T>, +Copy<T>>(l: T, r: T) -> T {
if l <= r {
l
} else {
Expand Down Expand Up @@ -219,7 +217,7 @@ fn read_sub_u128(value: u128, value_size: usize, offset: usize, size: usize) ->
/// - right_size: the size of right part in bytes
/// Returns:
/// - value: the joined u128
/// Examples:
/// Examples:
/// u128_join(0x010203, 0xaabb, 2) -> 0x010203aabb
/// u128_join(0x010203, 0, 2) -> 0x0102030000
fn u128_join(left: u128, right: u128, right_size: usize) -> u128 {
Expand Down
108 changes: 38 additions & 70 deletions src/data_structures/src/array_ext.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,35 @@ trait ArrayTraitExt<T> {
fn append_all(ref self: Array<T>, ref arr: Array<T>);
fn pop_front_n(ref self: Array<T>, n: usize);
fn reverse(self: @Array<T>) -> Array<T>;
fn contains<impl TPartialEq: PartialEq<T>>(self: @Array<T>, item: T) -> bool;
fn contains<+PartialEq<T>>(self: @Array<T>, item: T) -> bool;
fn concat(self: @Array<T>, a: @Array<T>) -> Array<T>;
fn index_of<impl TPartialEq: PartialEq<T>>(self: @Array<T>, item: T) -> Option<usize>;
fn occurrences_of<impl TPartialEq: PartialEq<T>>(self: @Array<T>, item: T) -> usize;
fn min<impl TPartialEq: PartialEq<T>, impl TPartialOrd: PartialOrd<T>>(
self: @Array<T>
) -> Option<T>;
fn index_of_min<impl TPartialEq: PartialEq<T>, impl TPartialOrd: PartialOrd<T>>(
self: @Array<T>
) -> Option<usize>;
fn max<impl TPartialEq: PartialEq<T>, impl TPartialOrd: PartialOrd<T>>(
self: @Array<T>
) -> Option<T>;
fn index_of_max<impl TPartialEq: PartialEq<T>, impl TPartialOrd: PartialOrd<T>>(
self: @Array<T>
) -> Option<usize>;
fn dedup<impl TPartialEq: PartialEq<T>>(self: @Array<T>) -> Array<T>;
fn unique<impl TPartialEq: PartialEq<T>>(self: @Array<T>) -> Array<T>;
fn index_of<+PartialEq<T>>(self: @Array<T>, item: T) -> Option<usize>;
fn occurrences_of<+PartialEq<T>>(self: @Array<T>, item: T) -> usize;
fn min<+PartialEq<T>, +PartialOrd<T>>(self: @Array<T>) -> Option<T>;
fn index_of_min<+PartialEq<T>, +PartialOrd<T>>(self: @Array<T>) -> Option<usize>;
fn max<+PartialEq<T>, +PartialOrd<T>>(self: @Array<T>) -> Option<T>;
fn index_of_max<+PartialEq<T>, +PartialOrd<T>>(self: @Array<T>) -> Option<usize>;
fn dedup<+PartialEq<T>>(self: @Array<T>) -> Array<T>;
fn unique<+PartialEq<T>>(self: @Array<T>) -> Array<T>;
}

trait SpanTraitExt<T> {
fn pop_front_n(ref self: Span<T>, n: usize);
fn pop_back_n(ref self: Span<T>, n: usize);
fn reverse(self: Span<T>) -> Array<T>;
fn contains<impl TPartialEq: PartialEq<T>>(self: Span<T>, item: T) -> bool;
fn contains<+PartialEq<T>>(self: Span<T>, item: T) -> bool;
fn concat(self: Span<T>, a: Span<T>) -> Span<T>;
fn index_of<impl TPartialEq: PartialEq<T>>(self: Span<T>, item: T) -> Option<usize>;
fn occurrences_of<impl TPartialEq: PartialEq<T>>(self: Span<T>, item: T) -> usize;
fn min<impl TPartialEq: PartialEq<T>, impl TPartialOrd: PartialOrd<T>>(
self: Span<T>
) -> Option<T>;
fn index_of_min<impl TPartialEq: PartialEq<T>, impl TPartialOrd: PartialOrd<T>>(
self: Span<T>
) -> Option<usize>;
fn max<impl TPartialEq: PartialEq<T>, impl TPartialOrd: PartialOrd<T>>(
self: Span<T>
) -> Option<T>;
fn index_of_max<impl TPartialEq: PartialEq<T>, impl TPartialOrd: PartialOrd<T>>(
self: Span<T>
) -> Option<usize>;
fn dedup<impl TPartialEq: PartialEq<T>>(self: Span<T>) -> Array<T>;
fn unique<impl TPartialEq: PartialEq<T>>(self: Span<T>) -> Array<T>;
fn index_of<+PartialEq<T>>(self: Span<T>, item: T) -> Option<usize>;
fn occurrences_of<+PartialEq<T>>(self: Span<T>, item: T) -> usize;
fn min<+PartialEq<T>, +PartialOrd<T>>(self: Span<T>) -> Option<T>;
fn index_of_min<+PartialEq<T>, +PartialOrd<T>>(self: Span<T>) -> Option<usize>;
fn max<+PartialEq<T>, +PartialOrd<T>>(self: Span<T>) -> Option<T>;
fn index_of_max<+PartialEq<T>, +PartialOrd<T>>(self: Span<T>) -> Option<usize>;
fn dedup<+PartialEq<T>>(self: Span<T>) -> Array<T>;
fn unique<+PartialEq<T>>(self: Span<T>) -> Array<T>;
}

impl ArrayImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of ArrayTraitExt<T> {
impl ArrayImpl<T, +Copy<T>, +Drop<T>> of ArrayTraitExt<T> {
fn append_all(ref self: Array<T>, ref arr: Array<T>) {
match arr.pop_front() {
Option::Some(v) => {
Expand Down Expand Up @@ -77,7 +61,7 @@ impl ArrayImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of ArrayTraitExt<T>
self.span().reverse()
}

fn contains<impl TPartialEq: PartialEq<T>>(self: @Array<T>, item: T) -> bool {
fn contains<+PartialEq<T>>(self: @Array<T>, item: T) -> bool {
self.span().contains(item)
}

Expand All @@ -104,48 +88,40 @@ impl ArrayImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of ArrayTraitExt<T>
ret
}

fn index_of<impl TPartialEq: PartialEq<T>>(self: @Array<T>, item: T) -> Option<usize> {
fn index_of<+PartialEq<T>>(self: @Array<T>, item: T) -> Option<usize> {
self.span().index_of(item)
}

fn occurrences_of<impl TPartialEq: PartialEq<T>>(self: @Array<T>, item: T) -> usize {
fn occurrences_of<+PartialEq<T>>(self: @Array<T>, item: T) -> usize {
self.span().occurrences_of(item)
}

fn min<impl TPartialEq: PartialEq<T>, impl TPartialOrd: PartialOrd<T>>(
self: @Array<T>
) -> Option<T> {
fn min<+PartialEq<T>, +PartialOrd<T>>(self: @Array<T>) -> Option<T> {
self.span().min()
}

fn index_of_min<impl TPartialEq: PartialEq<T>, impl TPartialOrd: PartialOrd<T>>(
self: @Array<T>
) -> Option<usize> {
fn index_of_min<+PartialEq<T>, +PartialOrd<T>>(self: @Array<T>) -> Option<usize> {
self.span().index_of_min()
}

fn max<impl TPartialEq: PartialEq<T>, impl TPartialOrd: PartialOrd<T>>(
self: @Array<T>
) -> Option<T> {
fn max<+PartialEq<T>, +PartialOrd<T>>(self: @Array<T>) -> Option<T> {
self.span().max()
}

fn index_of_max<impl TPartialEq: PartialEq<T>, impl TPartialOrd: PartialOrd<T>>(
mut self: @Array<T>
) -> Option<usize> {
fn index_of_max<+PartialEq<T>, +PartialOrd<T>>(mut self: @Array<T>) -> Option<usize> {
self.span().index_of_max()
}

fn dedup<impl TPartialEq: PartialEq<T>>(mut self: @Array<T>) -> Array<T> {
fn dedup<+PartialEq<T>>(mut self: @Array<T>) -> Array<T> {
self.span().dedup()
}

fn unique<impl TPartialEq: PartialEq<T>>(mut self: @Array<T>) -> Array<T> {
fn unique<+PartialEq<T>>(mut self: @Array<T>) -> Array<T> {
self.span().unique()
}
}

impl SpanImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of SpanTraitExt<T> {
impl SpanImpl<T, +Copy<T>, +Drop<T>> of SpanTraitExt<T> {
fn pop_front_n(ref self: Span<T>, mut n: usize) {
loop {
if n == 0 {
Expand Down Expand Up @@ -183,7 +159,7 @@ impl SpanImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of SpanTraitExt<T> {
response
}

fn contains<impl TPartialEq: PartialEq<T>>(mut self: Span<T>, item: T) -> bool {
fn contains<+PartialEq<T>>(mut self: Span<T>, item: T) -> bool {
loop {
match self.pop_front() {
Option::Some(v) => { if *v == item {
Expand Down Expand Up @@ -216,7 +192,7 @@ impl SpanImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of SpanTraitExt<T> {
ret.span()
}

fn index_of<impl TPartialEq: PartialEq<T>>(mut self: Span<T>, item: T) -> Option<usize> {
fn index_of<+PartialEq<T>>(mut self: Span<T>, item: T) -> Option<usize> {
let mut index = 0_usize;
loop {
match self.pop_front() {
Expand All @@ -231,7 +207,7 @@ impl SpanImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of SpanTraitExt<T> {
}
}

fn occurrences_of<impl TPartialEq: PartialEq<T>>(mut self: Span<T>, item: T) -> usize {
fn occurrences_of<+PartialEq<T>>(mut self: Span<T>, item: T) -> usize {
let mut count = 0_usize;
loop {
match self.pop_front() {
Expand All @@ -243,9 +219,7 @@ impl SpanImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of SpanTraitExt<T> {
}
}

fn min<impl TPartialEq: PartialEq<T>, impl TPartialOrd: PartialOrd<T>>(
mut self: Span<T>
) -> Option<T> {
fn min<+PartialEq<T>, +PartialOrd<T>>(mut self: Span<T>) -> Option<T> {
let mut min = match self.pop_front() {
Option::Some(item) => *item,
Option::None => { return Option::None; },
Expand All @@ -260,9 +234,7 @@ impl SpanImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of SpanTraitExt<T> {
}
}

fn index_of_min<impl TPartialEq: PartialEq<T>, impl TPartialOrd: PartialOrd<T>>(
mut self: Span<T>
) -> Option<usize> {
fn index_of_min<+PartialEq<T>, +PartialOrd<T>>(mut self: Span<T>) -> Option<usize> {
let mut index = 0;
let mut index_of_min = 0;
let mut min: T = match self.pop_front() {
Expand All @@ -281,9 +253,7 @@ impl SpanImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of SpanTraitExt<T> {
}
}

fn max<impl TPartialEq: PartialEq<T>, impl TPartialOrd: PartialOrd<T>>(
mut self: Span<T>
) -> Option<T> {
fn max<+PartialEq<T>, +PartialOrd<T>>(mut self: Span<T>) -> Option<T> {
let mut max = match self.pop_front() {
Option::Some(item) => *item,
Option::None => { return Option::None; },
Expand All @@ -298,9 +268,7 @@ impl SpanImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of SpanTraitExt<T> {
}
}

fn index_of_max<impl TPartialEq: PartialEq<T>, impl TPartialOrd: PartialOrd<T>>(
mut self: Span<T>
) -> Option<usize> {
fn index_of_max<+PartialEq<T>, +PartialOrd<T>>(mut self: Span<T>) -> Option<usize> {
let mut index = 0;
let mut index_of_max = 0;
let mut max = match self.pop_front() {
Expand All @@ -319,7 +287,7 @@ impl SpanImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of SpanTraitExt<T> {
}
}

fn dedup<impl TPartialEq: PartialEq<T>>(mut self: Span<T>) -> Array<T> {
fn dedup<+PartialEq<T>>(mut self: Span<T>) -> Array<T> {
if self.len() == 0 {
return array![];
}
Expand All @@ -340,7 +308,7 @@ impl SpanImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of SpanTraitExt<T> {
ret
}

fn unique<impl TPartialEq: PartialEq<T>>(mut self: Span<T>) -> Array<T> {
fn unique<+PartialEq<T>>(mut self: Span<T>) -> Array<T> {
let mut ret = array![];
loop {
match self.pop_front() {
Expand Down
12 changes: 4 additions & 8 deletions src/data_structures/src/stack.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,14 @@ struct Felt252Stack<T> {
len: usize,
}

impl DestructFeltStack<
T, impl TDrop: Drop<T>, impl TFelt252DictValue: Felt252DictValue<T>
> of Destruct<Felt252Stack<T>> {
impl DestructFeltStack<T, +Drop<T>, +Felt252DictValue<T>> of Destruct<Felt252Stack<T>> {
fn destruct(self: Felt252Stack<T>) nopanic {
self.elements.squash();
}
}

impl Felt252StackImpl<
T, impl TCopy: Copy<T>, impl TDrop: Drop<T>, impl TFelt252DictValue: Felt252DictValue<T>,
T, +Copy<T>, +Drop<T>, +Felt252DictValue<T>,
> of StackTrait<Felt252Stack<T>, T> {
#[inline(always)]
/// Creates a new Stack instance.
Expand Down Expand Up @@ -116,15 +114,13 @@ struct NullableStack<T> {
len: usize,
}

impl DestructNullableStack<T, impl TDrop: Drop<T>> of Destruct<NullableStack<T>> {
impl DestructNullableStack<T, +Drop<T>> of Destruct<NullableStack<T>> {
fn destruct(self: NullableStack<T>) nopanic {
self.elements.squash();
}
}

impl NullableStackImpl<
T, impl TCopy: Copy<T>, impl TDrop: Drop<T>,
> of StackTrait<NullableStack<T>, T> {
impl NullableStackImpl<T, +Copy<T>, +Drop<T>,> of StackTrait<NullableStack<T>, T> {
#[inline(always)]
fn new() -> NullableStack<T> {
let elements: Felt252Dict<Nullable<T>> = Default::default();
Expand Down
Loading

0 comments on commit 1965197

Please sign in to comment.