/ Published in: C#
Define a two-dimensional boolean array. Minimize the memory allocated. Write a function to set a value to an element of this array.
Expand |
Embed | Plain Text
sing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace BooleanMatrix { class BooleanMatrix { private const int BITSPERWORD = 32; private const int SHIFT = 5; private const int MASK = 0x1F; private int width; private int height; private int length; private int[] bitset; public BooleanMatrix(int width, int height) { this.width = width; this.height = height; this.length = width * height; } public void Set(int x, int y) { int index = getIndex(x, y); bitset[index >> SHIFT] |= (1 << (index & MASK)); } public void Clear(int x, int y) { int index = getIndex(x, y); bitset[index >> SHIFT] &= ~(1 << (index & MASK)); } public int Test(int x, int y) { int index = getIndex(x, y); return bitset[index >> SHIFT] & (1 << (index & MASK)); } private int getIndex(int x, int y) { return x * this.width + y; } public int Height { get { return height; } } public int Width { get { return width; } } } }
You need to login to post a comment.
