[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Modified Grid2d class...
From: |
Barry McMullin |
Subject: |
Modified Grid2d class... |
Date: |
Tue, 17 Sep 1996 13:44:04 +0000 |
Hi y'all -
The story behind this is that I tried to catch Manor out
and failed miserably (;-) but in the course of the conversation
he admitted that Grid2d could be improved in a certain way,
sketched out how to do it - and then left the remainder
as an exercise for the student (;-)
So, here it is. I think it's self explanatory. You'll find
both Grid2d.h and Grid2d.m; do with them as you see fit...
[Oh yeah: Grid2d.m has a least one very long line which may have
got MIME type QP encoded somewhere en route; there again it may not;
but look out in case. Someday, we're gonna have an *official standard*
for emailing smarm code, and then I'll conform...]
--------------- Grid2d.h ------------- cut here
// Swarm library. Copyright (C) 1996 Santa Fe Institute.
// This library is distributed without any warranty; without even the
// implied warranty of merchantability or fitness for a particular purpose.
// See file LICENSE for details and terms of copying.
// (beta) Change Log;
// 16th September 1996 <address@hidden>
//
// Semantics of -putObjectAtX:Y: modified to raise a warning
// on attempt to overwrite with nil; previously this was
// silently accepted - as the only way that an object could
// get "moved". The problem with that is that *accidental*
// overwrites with nil also get ignored; and since the only
// point of the Grid2d class is to provide some defense
// against that sort of this, we'd like to do better.
// Solution here is to add a new method, -clearObjectAtX:Y:
// which is *allowed* to overwrite with nil; so this
// method must now be used explicitly, where such a thing
// is legitimate, reducing the risk of accidental overwrites
// via -putObjectAtX:Y: - I hope (;-)
//
// THIS CHANGE WILL BREAK EXISTING APPS!!!
//
// "Break" in the sense than, until you replace legitimate
// overwrites with nil by invocations of clearObjectAtX:Y:,
// you'll get a spurious warning message every time such an
// overwrite occurs. The app will still run though. I think.
#import <space/Discrete2d.h>
@interface Grid2d: Discrete2d {
BOOL overwriteWarnings;
}
-setOverwriteWarnings: (BOOL) b;
-clearObjectAtX: (int) x Y: (int) y;
// Forces overwrite of existing object with nil.
// Raises a warning if overwriteWarnings is true and
// the existing object is already nil.
@end
---------------------------Grid2d.h ------------ cut here
---------------------------Grid2d.m ------------ cut here
// Swarm library. Copyright (C) 1996 Santa Fe Institute.
// This library is distributed without any warranty; without even the
// implied warranty of merchantability or fitness for a particular purpose.
// See file LICENSE for details and terms of copying.
// (beta) Change Log;
// 16th September 1996 <address@hidden>
//
// Modified semantics of -putObjectAtX:Y: and added new
// clearObjectAtX:Y: method. Details and rationale in
// .h file.
#import <space/Grid2d.h>
// this currently only allows one occupant per cell. Bad thing.
@implementation Grid2d
+createBegin: (id) aZone {
id r;
r = [super createBegin: aZone];
[r setOverwriteWarnings: 1];
return r;
}
// Note - we don't use the superclass method, we write our own.
-putObject: anObject atX: (int) x Y: (int) y {
// this warning isn't such a great idea, maybe. We need a better
// 2d space object, undoubtedly.
id objectThere;
objectThere = *discrete2dSiteAt(lattice, offsets, x, y);
if (overwriteWarnings &&
(objectThere != nil)) {
[WarningMessage raiseEvent:
"Grid2d: you're overwriting non-nil object %x at (%d,%d)\n with object
%x.\n Grid2d does not support two objects in one place.\n",
objectThere, x, y, anObject];
}
*discrete2dSiteAt(lattice, offsets, x, y) = anObject;
return self;
}
-setOverwriteWarnings: (BOOL) b {
overwriteWarnings = b;
return self;
}
-clearObjectAtX: (int) x Y: (int) y {
id objectThere;
objectThere = *discrete2dSiteAt(lattice, offsets, x, y);
if (overwriteWarnings &&
(objectThere == nil))
[WarningMessage raiseEvent:
"Grid2d: redundant clear of already nil object at (%d,%d)\n",
x, y];
*discrete2dSiteAt(lattice, offsets, x, y) = nil;
return self;
}
@end
---------------------------Grid2d.m ------------ cut here
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
| Barry McMullin, Autonomous Systems Group, | address@hidden |
| School of Electronic Engineering, | Voice: +353-1-704-5432 |
| Dublin City University, Dublin 9, IRELAND. | FAX: +353-1-704-5508 |
| http://www.eeng.dcu.ie/~mcmullin/home.html | |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Modified Grid2d class...,
Barry McMullin <=