DataMatrix.mm 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #import "DataMatrix.h"
  2. @implementation DataMatrix
  3. - (id)initWith:(int)dimension {
  4. if ([super init]) {
  5. self->dim = dimension;
  6. self->data = (bool**)malloc(sizeof(bool*) * self->dim);
  7. for (int y=0; y<self->dim; y++) {
  8. self->data[y] = (bool*)malloc(sizeof(bool) * self->dim);
  9. if (self->data[y]==NULL) {
  10. NSLog(@"null!");
  11. }
  12. }
  13. }
  14. return self;
  15. }
  16. - (int)dimension {
  17. return self->dim;
  18. }
  19. - (void)set:(bool)value x:(int)x y:(int)y {
  20. self->data[y][x] = value;
  21. }
  22. - (bool)valueAt:(int)x y:(int)y {
  23. return self->data[y][x];
  24. }
  25. - (NSString*)toString {
  26. NSString* string = [NSString string];
  27. for (int y=0; y<self->dim; y++) {
  28. for (int x=0; x<self->dim; x++) {
  29. bool value = self->data[y][x];
  30. string = [string stringByAppendingFormat:@"%d", value];
  31. }
  32. string = [string stringByAppendingString:@"\n"];
  33. }
  34. return string;
  35. }
  36. - (void)dealloc {
  37. for (int y=0; y<self->dim; y++) {
  38. free(self->data[y]);
  39. }
  40. free(self->data);
  41. [super dealloc];
  42. }
  43. @end