potzko
potzko
Explore posts from servers
CC#
Created by potzko on 3/9/2024 in #help
help with generic interface
im writing a small binary tree library for fun and ran into a small problem I made a small interface for Node and Tree: however the Node interface can only reference itself if that makes sense? public interface Node<T> where T : IComparable<T>
public interface Node<T> where T : IComparable<T>
{
T GetValue();
Node<T>? Getleft();
Node<T>? GetRight();
}

public class SplayTreeNode<T> : Node<T> where T : IComparable<T>
{
private T data;
private SplayTreeNode<T>? left;
private SplayTreeNode<T>? right;
private SplayTreeNode<T>? parent;

public SplayTreeNode(T value)
{
data = value;
}

public T GetValue()
{
return data;
}

public SplayTreeNode<T>? Getleft()
{
return left;
}

public SplayTreeNode<T>? GetRight()
{
return right;
}
}
public interface Node<T> where T : IComparable<T>
{
T GetValue();
Node<T>? Getleft();
Node<T>? GetRight();
}

public class SplayTreeNode<T> : Node<T> where T : IComparable<T>
{
private T data;
private SplayTreeNode<T>? left;
private SplayTreeNode<T>? right;
private SplayTreeNode<T>? parent;

public SplayTreeNode(T value)
{
data = value;
}

public T GetValue()
{
return data;
}

public SplayTreeNode<T>? Getleft()
{
return left;
}

public SplayTreeNode<T>? GetRight()
{
return right;
}
}
75 replies
CC#
Created by potzko on 1/2/2024 in #help
C# enum deconstruction and assosiated data
hi, I want to follow the book crafting interpreters and I don't want to use the exact languadge he uses (java) to avoid copy pasting code without understanding it, I am looking at maybe using C# (I know... super far away from java), however I can't find a feature Ill prob want to use, is there a way to do rust style enum types with associated data? think something like (super super psudo code)
enum a{
twoDimantionalVector: (int, int)
position: (int, int, int)
cat: (string, string, int)
}
a value = ...
match a {
case twoDimantionalVector(X, Y);
case position(X, Y, Z) {...}
case cat(name, color, age);
}
enum a{
twoDimantionalVector: (int, int)
position: (int, int, int)
cat: (string, string, int)
}
a value = ...
match a {
case twoDimantionalVector(X, Y);
case position(X, Y, Z) {...}
case cat(name, color, age);
}
8 replies
CC#
Created by potzko on 5/12/2023 in #help
❔ help with self referential class typing
I was trying to make a node class I can inherit from and ran into a ton of trouble, in my original solution the "next" value was still a node rather then the child class, I looked around and people were saying to use a self referential generic class signature so I used this:
public class Node<Self, T>
where Self: Node<Self, T>
{
public T value;
public Self? next;
}
public class Node<Self, T>
where Self: Node<Self, T>
{
public T value;
public Self? next;
}
and started writing, I even got some inheritance to work,
public class CmpNode<T>: Node<CmpNode<T>, T>
where T: IComparable
{
public CmpNode(T value) : base(value) { }
public CmpNode(T value, CmpNode<T> next) : base(value, next) { }

public CmpNode<T> getMaxNode(){
var max = this;
var point = this;
while (point!.hasNext()){
if (max.value.CompareTo(point.value) == -1){
max = point;
}
point = point.next;
}
if (max.value.CompareTo(point.value) == -1){
max = point;
}
return max;
}
}
public class CmpNode<T>: Node<CmpNode<T>, T>
where T: IComparable
{
public CmpNode(T value) : base(value) { }
public CmpNode(T value, CmpNode<T> next) : base(value, next) { }

public CmpNode<T> getMaxNode(){
var max = this;
var point = this;
while (point!.hasNext()){
if (max.value.CompareTo(point.value) == -1){
max = point;
}
point = point.next;
}
if (max.value.CompareTo(point.value) == -1){
max = point;
}
return max;
}
}
however I ran into a few problems in some of the functions:
public static Self? fromArr(T[] arr){
if (arr.Length == 0){
return null;
}
var start = new Self(arr[0]);
var point = start;
foreach (T item in arr.Skip(1))
{
point.next = new Self(item);
}
return start;
}
public static Self? fromArr(T[] arr){
if (arr.Length == 0){
return null;
}
var start = new Self(arr[0]);
var point = start;
foreach (T item in arr.Skip(1))
{
point.next = new Self(item);
}
return start;
}
in this function for example, how can I make a new "Self" object? as I do not know what constructors it has. and if I try to build a node I cant set it as next as I expect a Self object, am I missing a trick to get this to work?
9 replies
CC#
Created by potzko on 3/23/2023 in #help
❔ id love to get a code review if possible
this is a segmented sieve code I wrote. it is meant to be as fast as I can get it, however id mostly like to get coding conventions for cs down
using System.Collections;
using System.Collections.Generic;

public class Primes
{
static List<int> primes = new List<int>(new int[] { 2, 3, 5, 7 });
static int segment = 1;
public static void add_next_seg()
{
// use segmented sieve to add numbers to the prime list.

//the next primes for whom we will check the squared segment
int start = Primes.primes[Primes.segment];
int end = Primes.primes[Primes.segment + 1];

//create the bounds of the segment
int seg_start = start * start;
int seg_end = end * end;
//create the segment, false -> prime, true -> composite
bool[] seg = new bool[seg_end - seg_start];

//the max number is seg_end which is end**2 -> only need to check primes under and including end.
//no need to check 2, as we will jump by 2 when adding the numbers later
for (int i = 1; i < segment + 1; i++) {
int prime_num = Primes.primes[i];
//start at start of segment and mark all numbers devisible by the prime number
for (int ind = seg_start + (prime_num - (seg_start%prime_num))%prime_num; ind - seg_start < seg.Length; ind += prime_num)
{
seg[ind - seg_start] = true;
}
}

//no need to check 2, as we precoded first iteration, -> start != 2 -> seg_start is odd
for (int ind = seg_start; ind < seg_end; ind+=2)
{
if (!seg[ind - seg_start])
{
Primes.primes.Add(ind);
}
}
Primes.segment++;
}
public static int get_nth_prime(int ind){
//returns the nth prime
while (Primes.primes.Count <= ind){
Primes.add_next_seg();
}
return Primes.primes[ind];
}
}
using System.Collections;
using System.Collections.Generic;

public class Primes
{
static List<int> primes = new List<int>(new int[] { 2, 3, 5, 7 });
static int segment = 1;
public static void add_next_seg()
{
// use segmented sieve to add numbers to the prime list.

//the next primes for whom we will check the squared segment
int start = Primes.primes[Primes.segment];
int end = Primes.primes[Primes.segment + 1];

//create the bounds of the segment
int seg_start = start * start;
int seg_end = end * end;
//create the segment, false -> prime, true -> composite
bool[] seg = new bool[seg_end - seg_start];

//the max number is seg_end which is end**2 -> only need to check primes under and including end.
//no need to check 2, as we will jump by 2 when adding the numbers later
for (int i = 1; i < segment + 1; i++) {
int prime_num = Primes.primes[i];
//start at start of segment and mark all numbers devisible by the prime number
for (int ind = seg_start + (prime_num - (seg_start%prime_num))%prime_num; ind - seg_start < seg.Length; ind += prime_num)
{
seg[ind - seg_start] = true;
}
}

//no need to check 2, as we precoded first iteration, -> start != 2 -> seg_start is odd
for (int ind = seg_start; ind < seg_end; ind+=2)
{
if (!seg[ind - seg_start])
{
Primes.primes.Add(ind);
}
}
Primes.segment++;
}
public static int get_nth_prime(int ind){
//returns the nth prime
while (Primes.primes.Count <= ind){
Primes.add_next_seg();
}
return Primes.primes[ind];
}
}
248 replies