M
Matthew Daly
I'm playing around with a class design using delegates and have gotten
stuck. I'm not certain if cooler heads can prevail or if it is forces
that man was not intended to tamper with.
The LazyArray class below is for processor-intensive sequences, like
recursive functions. It doesn't calculate any values that you don't
ask for and remembers the ones that it has calculated in the past so as
to not do it again. (In practice, the integer array would be replaced
with a Hashtable, I suppose, but I wanted to keep things simple for the
moment.)
The program does not compile, alas, since the static functions are
referencing items that are not in their context. I can't figure out
how to bring everything under the same tent, though. The thing that's
getting under my skin is that this DOES work if I make F and M
unrelated classes with all-static members, but as soon as I start
gathering their common functionality under a base class I'm socked with
this problem or trying to make a static virtual function.
Is there a way to untie this knot, or am I stuck?
Thanks,
Matthew
--
class Program2
{
public class LazyArray
{
public delegate int Calculate(int n);
private Calculate do_it;
private int[] known = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1 };
public LazyArray(int val_at_zero, Calculate theFunction)
{
known[0] = val_at_zero;
do_it = theFunction;
}
public int this[int n]
{
get
{
if (known[n] == -1) { known[n] = do_it(n); }
return known[n];
}
}
}
public static int f_at_n(int n) { return M[F[n - 1]]; }
public static int m_at_n(int n) { return F[M[n - 1]]; }
static void Main(string[] args)
{
LazyArray F = new LazyArray(0, f_at_n);
LazyArray M = new LazyArray(1, m_at_n);
Console.WriteLine(F[10]);
}
}
stuck. I'm not certain if cooler heads can prevail or if it is forces
that man was not intended to tamper with.
The LazyArray class below is for processor-intensive sequences, like
recursive functions. It doesn't calculate any values that you don't
ask for and remembers the ones that it has calculated in the past so as
to not do it again. (In practice, the integer array would be replaced
with a Hashtable, I suppose, but I wanted to keep things simple for the
moment.)
The program does not compile, alas, since the static functions are
referencing items that are not in their context. I can't figure out
how to bring everything under the same tent, though. The thing that's
getting under my skin is that this DOES work if I make F and M
unrelated classes with all-static members, but as soon as I start
gathering their common functionality under a base class I'm socked with
this problem or trying to make a static virtual function.
Is there a way to untie this knot, or am I stuck?
Thanks,
Matthew
--
class Program2
{
public class LazyArray
{
public delegate int Calculate(int n);
private Calculate do_it;
private int[] known = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1 };
public LazyArray(int val_at_zero, Calculate theFunction)
{
known[0] = val_at_zero;
do_it = theFunction;
}
public int this[int n]
{
get
{
if (known[n] == -1) { known[n] = do_it(n); }
return known[n];
}
}
}
public static int f_at_n(int n) { return M[F[n - 1]]; }
public static int m_at_n(int n) { return F[M[n - 1]]; }
static void Main(string[] args)
{
LazyArray F = new LazyArray(0, f_at_n);
LazyArray M = new LazyArray(1, m_at_n);
Console.WriteLine(F[10]);
}
}