[Date Prev][Date Next][Thread Prev][Thread Next][Author Index][Date Index][Thread Index]

Re: TableView, UnaryFn



a simple example is the use of a TableView to provide a restricted view of
a larger table.  Given that the backend returns this nice large orgl with
the entire contents of the document (say 5 KiloChars).  Now, I'm about to
display it, so I ask for a small section (which I've predetermined to be
a run), say, indices 34 to 83 inclusive.  As an FE person, I'll use this
subTable to build a c string, which I pass to a window system dependent
routine for display.  Oh, BTW, I also want to remap the indices to 0..49,
because those are the indices of the chars in a c string.
  Ok, as a table implementor, I could simply copy the elements into a new
table and be done.  But, being disgustingly efficiency minded (:->) I want
to do better.  So I simply return a TableView which, when I ask for index
0 I get the element at 34 in the original table.  (simple unaryFn mapping
of the indices).
  Now, the tableView could be built with a unaryFn that restricts the indices,
then transforms the index, and then uses that to fetch from the original table,
which is in a member variable of the UnaryFn.

As a derived class of TableView, an implementation would look like:
DEFINE_CLASS(smallerTableView,TableView);
Object * smallerTableView::fetch (AS(Object,Integer) * gen_index) {
	Integer * index = CAST(Integer,gen_index);
	IntegerVar idx;
	
	idx = index->asIntegerVar();
	return idx < 0 || idx > stop - start ? NULL : 
		aTable->fetch(idx + start);
}
IntegerVar smallerTableView::count() {
	return stop - start + 1;  // assume aTable is charming.
}
smallerTableView::smallerTableView (
		IntegerVar gen_start, 
		IntegerVar gen_stop, 
		ROTable * gen_aTable)
 : 
	start(gen_start), 
	stop(gen_stop), 
	aTable(gen_aTable)
{
}
// other stuff left out.

Or, I could build an object that had a unaryFn for the fetch, and another
for the count.  These would be invoked by the TableView object as appropriate.
CLASS(TableView,ROTable) {
...
private:
 UnaryFn * fetchFn;
 UnaryFn * countFn;
...
}
...
TableView::fetch (Position * idx) {
  return fetchFn->of (idx);
}
...
newView = tableView(mapFetch(34,83,bigTable), reCount(34,83));
where mapFetch and reCount construct UnaryFns.

I believe I've answered the original questiona anyway, but the example may
show something I not thinking about.
--Hugh