Wordsearch c# – Word class

I am just doing a test/comparsion between the different ways of getting a similar code syntax to work in different languages for a similar project idea. The main project idea is wordsearch grid with words inserted onto the grid, I have already done a basic php version and here is the word class in php word. Below there is a full class version of whole class, but also some parts that I have expanded to try and explain in more detail.

This code will return a String array from a ArrayList type. The ToArray changes the ArrayList to type of string then the “as string[]” returns the list of string as a string array.

			return (arrayL.ToArray(typeof(string)) as string[]);

Here is the word class in c#, I have added allot of code comments to give details of what is happening.

using System;
using System.Xml;
using System.Collections;
 
namespace wordsearchcsharp
{
	class Word 
	{
		private String[] _words;
 
	 	public  Word() : this("words.xml", 5) {}
 
		public Word(string wordsOrFilename, int maxSearchNum)
		{
			this._words = new String[maxSearchNum];
			// just load from a file the words to search, instead of input as well.
			String[] loadedWords = loadWords(wordsOrFilename);
 
			// create the searchable words from the loadedwords array.
			this._words = this.searchableWords(loadedWords, maxSearchNum);
		}
 
		private String[] loadWords(string filename)
		{
			XmlTextReader fileReader = new XmlTextReader(filename);
 
			ArrayList fileArray = new ArrayList();
 
			while (fileReader.Read())
			{
				// get the NodeType from the XML reader, different types
				// Element = name of element
				// Text = the actual text/word
				// End Element = end of the element name
			    switch (fileReader.NodeType) 
			    {
					// if there is a word in the file e.g. not a end/element
					  case XmlNodeType.Text: 
							fileArray.Add(fileReader.Value);
				            break;
			    }
			}
			// change the ArrayList into a String[] array using the ToArray method
			return (fileArray.ToArray(typeof(string)) as string[]);
		}
 
		// create a list of words from the words passed in and also 
		// the maximum number is the maxSearchNum.
		private String[] searchableWords(String[] words, int maxSearchNum)
		{
			ArrayList returnWords = new ArrayList();
 
			// if the maxsearch value is greater or equal to the number of words to search for 
			// just return the words string[].
			if (words.Length <= maxSearchNum)
			{
				return words;
			}
			else
			{
				// create a good seed for the random generator.
				Random randGen = new Random((int)DateTime.Now.Ticks);
				int randomNum;
				// randomly pick out words from the array
				for (int i = 0; i < maxSearchNum; i++)
				{
					// pick a random number
					randomNum = randGen.Next(0, words.Length);
					// add to the array list to return
					returnWords.Add(words[randomNum]);
					// rebuild the array with removing the random number generated.
					words = rebuildArray(words, randomNum);
				}
			}
			// convert back to the String[] 
			return (returnWords.ToArray(typeof(string)) as string[]);
		}
 
		private String[] rebuildArray(String[] rebuildSt, int numberToTakeOut)
		{
			ArrayList arrayL = new ArrayList();
			// out of range error.
			if (rebuildSt.Length < numberToTakeOut)
			{
				return rebuildSt;
			}
			else
			{
				for (int i =0; i < rebuildSt.Length; i++)
				{
					// only add in the words that are not the
					// numberToTakeOut word from the array
					if (i != numberToTakeOut)
					{
						arrayL.Add(rebuildSt[i]);
					}
				}
			}
			return (arrayL.ToArray(typeof(string)) as string[]);
		}
 
		public void printOutWords()
		{
			Console.Write("Words : (");
			for (int i = 0; i < this._words.Length; i++)
			{
				Console.Write("  " + i + " = " + this._words[i]);
			}
			Console.Write(")\n");
		}
 
		public String[] returnWords()
		{
			return this._words;
		}
	}
 
	class MainClass
	{
		public static void Main(string[] args)
		{
			Word word = new Word("words.xml",3);
                        word.printOutWords();
		}
	}
}

and a output would be

Words : (  0 = he  1 = old  2 = sole)

Monodevelop – system.xml error

I use monodevelop to create my c# applications etc and just noticed that if you want to have more references to assemblies like System.Xml then you will have to add them via the GUI.

For example, if you get the error.

Description=The type or namespace name `Xml' does not exist in the namespace `System'. Are you missing an assembly reference?(CS0234)

then you will need to add the System.Xml assembly reference into the GUI solutions manager to compile in that assembly.

If you goto Solutions box -> References -> (Right click) Edit References. In the packages tab scroll down in the list and find the System.Xml and add it to the solution. Compile again and it will work :).. well you may get other errors but at least this one is sorted.

If you do not have the Solutions box, goto the main monodevelop menu View -> Solution.

Wordsearch – c# Grid class

When coding in c#/csharp, I use monodevelop. Here is the wordsearch base Grid class in c#, I am coding in different languages just for fun as such, but here is the main idea for the wordsearch. I have already done a basic version in php which is at the present state in creating a grid (but without any intersection of words with others). But here is the Grid class in php, and below is the similar setup in csharp.

The main class, is where the application executes from, it is very similar to c++ in that there is a main function

public static void Main(string[] args)

which takes the string[] args as a parameter, which is what is passed in on the command line.

Anyway, here is the code.. I have added in some comments to what the code is doing.

using System;
 
namespace wordsearchcsharp
{
	class Grid {
		protected int _size = 0;
		protected char[][] _grid;
 
		// the ": this(11)" means call the Grid(int size) constructor with 11 as a parameter
		public Grid() : this(11)
		{
			// default size of a grid = 11
		}
 
		public Grid(int size)
		{
			this._size = size;
			// create a new row (Y direction)
			this._grid = new char[this._size][];
			for (int i =0; i < this._size; i++)
			{
				// create new X direction row.
				this._grid[i] = createGridRow();
			}
		}
 
		// createGridRow, this can be inherited and thus altered to create any type of character in the grid 
		public virtual char[] createGridRow()
		{
			// need to insert a good seed into the random generator.. else same characters
			Random random = new Random((int)DateTime.Now.Ticks);
 
			// create a new row (X direction) and place characters into it.
			char[] grid_insert = new char[this._size];
			for (int i = 0; i < this._size; i++)
			{
				// 0 - 25 = alphabet, and also +97 is the ascii start for the character 'a'
				grid_insert[i] = (char)(random.Next(0,25)+97);
			}
			return grid_insert;
		}
 
		// printOutGrid, will print out the grid created above.
		// i = Y direction, and j = X direction.
		public void printOutGrid()
		{
			Console.WriteLine("The Grid");
			for (int i = 0; i < this._size; i++)
			{
				Console.Write(i + " ..");
				for (int j = 0; j < this._size; j++)
				{
					Console.Write(j + " = " + this._grid[i][j]);
				}
				Console.WriteLine("");
			}
		}
 
		public int returnSize()
		{
			return this._size;
		}
 
		// insert a character into the grid at position X, Y and the character 
		public void insertCharIntoGrid(int pX, int pY, char character)
		{
			this._grid[pY][pX] = character;
		}
 
		//  return the grid reference 
		public char[][] returnGrid()
		{
			return this._grid;
		}
 
	}
 
	class MainClass
	{
		public static void Main(string[] args)
		{
			Grid grid = new Grid();
			grid.printOutGrid();
		}
	}
}

And here is the output.

The Grid
0 ..0 = m1 = y2 = p3 = d4 = d5 = y6 = g7 = p8 = m9 = i10 = t
1 ..0 = k1 = c2 = y3 = n4 = i5 = m6 = q7 = f8 = g9 = p10 = j
2 ..0 = o1 = r2 = v3 = q4 = a5 = i6 = y7 = h8 = k9 = y10 = c
3 ..0 = h1 = u2 = f3 = s4 = v5 = r6 = p7 = g8 = h9 = j10 = b
4 ..0 = n1 = j2 = y3 = u4 = v5 = q6 = n7 = b8 = y9 = w10 = g
5 ..0 = g1 = m2 = i3 = x4 = r5 = a6 = e7 = a8 = w9 = i10 = f
6 ..0 = m1 = c2 = c3 = a4 = r5 = y6 = c7 = v8 = o9 = u10 = k
7 ..0 = f1 = e2 = k3 = c4 = n5 = j6 = s7 = t8 = m9 = g10 = j
8 ..0 = l1 = t2 = f3 = e4 = n5 = i6 = q7 = p8 = e9 = t10 = p
9 ..0 = e1 = w2 = n3 = h4 = j5 = r6 = h7 = o8 = b9 = e10 = o
10 ..0 = y1 = x2 = u3 = j4 = n5 = g6 = n7 = h8 = m9 = t10 = a

I use linux and thus mono is my csharp virtual machine setup, but here is a output of the file command in linux which tells me that the wordsearchcsharp.exe if a mono assembly and thus if you ./wordsearchcsharp.exe will goto the mono virtual machine.. it will link/expand to mono wordsearchcsharp.exe on the command line.

file wordsearchcsharp.exe
wordsearchcsharp.exe: PE32 executable for MS Windows (console) Intel 80386 32-bit Mono/.Net assembly

Wordsearch php – The words inserted into the grid

The WordToSearchGrid class, it extends the grid class to add in the words to search for class.

The constructor from base class Grid creates the basic grid, I have added in a testing createGridRow() in this class to build just a empty grid (this is a great thing with class inheritance / extends etc, so that you can just re-write one aspect of the base class if you want it done a different way and it still works), and the output below is of the testing createGridRow() function so it makes it easier to see where the words was placed.

The direction of the words are like a compass directions, north, south, east and west and the north east, south east, north west and south west. Their are 8 directions in total that a word can take.

The getWords function gets the words to search for.

Here is a break down of the createSearchGrid function, which is the make aspect of placement of words.

    public function createSearchGrid()
    {
	// get the size of the grid
	$size = parent::returnSize();
 
	// loop thought the words to insert
	for ($i = 0; $i < count($this->_words); $i++)
	{

Loop thought the amount of words that are going to be inserted (tried to be)

	  $wordLen = strlen($this->_words[$i]);
	  // if the word is larger than the size of the grid, it cannot be inserted.
 
	  // NOTE : at present there is no intersection (crossing search words) shall do in the next version
 
	  if ($wordLen < $size) 
	  {

As NOTED in the code, there is no intersection at present, shall do this at a later stage, just the basics of insertion at present.

Also if the word to be inserted is longer in length than the actual grid then there is no point trying to insert it, because it will not fit.

	      //need to pick a direction and also a point on the grid.
	      // also need to try and place the word onto the grid x amount of times else break out.
	      $direction = rand(1,8);
	      $pointX = rand(1, $size);
	      $pointY = rand(1, $size);
 
	      // the remainder of the subtracting the points from the size of grid will basically say how much space is required
	      $space = $size - $wordLen;

Get a randomize direction, there are 8 directions and also randomized points to insert into the grid from 1 to the maximum size of the grid

	      // check against the direction and the size of the word against where it is on the poistion in the grid
	      // 1 = north , 2 = north east, 3 = east, 4 = south east, 5 = south, 6 = south west, 7 = west, 8= north west
	      // from the points point of view.
 
	      $spaceY = $size - $pointY;
	      $spaceX = $size - $pointX;

Here is when the fun starts, the space X/Y left is basically how much space is left from the size of the grid to where the point X/Y are placed on the grid.

	      // if the direction is not east or west, and there is not enought space, move the insertion pointY difference
	      // north and south
	      if (!(($direction == 7) || ($direction == 3)))
	      {
		  if ($spaceY > $space) 
		      $pointY+= ($spaceY - $space);
	      }

If the direction is not going west or east then we are going north or south. The next question is, how much space is there to insert the word (we are always working in a north/west direction, but can move the word points around for south and east) and is the space left to insert the word greater than the space left required to insert the word.

E.g.. the size of the grid is 11 and the pointY = 2 and the word to insert is “merry” which has a word length of 5.
The space required ($space) = 11 – 5 which equals 6
The space left ($spaceY) = 11 – 2 which equals 9
and since we are always heading in a north/west direction as a default (if we are heading in any direction, e.g. sometimes it would be north or west or both).
then if the space left (9) is greater than the space required (6) which it is then increment the pointY (space left) the difference between the space left ($spaceY) and the space required ($space) which equals 3.

So from the equation this would be

if ( $spaceY (9) > $space (6) )
$pointY (2) increment by 3 which makes $pointY = 5 which is the length of the word (could just make it equal the length of the word, but I prefer the equation 🙂 )

The below is the same as the north/south test, apart from east and west.

	      // if the direction is not north or south and there is not enought space, move the insertion pointX difference
	      // east and west.
	      if (!(($direction == 1) || ($direction == 5)))
	      {
		  if ($spaceX > $space)
		      $pointX+= ($spaceX - $space);
	      }
 
	      $this->insertIntoGrid($pointX, $pointY, $this->_words[$i], $direction);
	  }
	  else
	  {
	      // no need to tell the user to search for something that was not inserted
	      echo "Word ". $this->_words[$i] . " was too long for the wordsearch";
	      unset($this->_words[$i]);
	  }
	}
    }

The insertIntoGrid function, inserts the word that has a pointX and pointY coordinates and the direction.

    // insert into the grid at the positions x, y, the word to insert with the direction
    public function insertIntoGrid($pX, $pY, $word, $direction)
    {
	$wordLen = strlen($word);

Since we are based in north/west direction if the direction is south/east then correct the starting point to reflect the word direction.

	// move the starting point of the word to the correct place within the grid
	// the default is north and west, so will need to correct for south and east directions.
	if ($direction >=4 && $direction <=6) $pY-= ($wordLen-1);
	if ($direction >=2 && $direction <=4) $pX-= ($wordLen-1);
 
	// 1 = north , 2 = north east, 3 = east, 4 = south east, 5 = south, 6 = south west, 7 = west, 8= north west
	// process each letter of the word and move the position to insert in the correct direction
	for ($i = 0; $i < $wordLen; $i++)
	{
	    parent::insertCharIntoGrid($pX, $pY, $word[$i]);
	    // move to the next grid position to insert the character
	    switch($direction)
	    {
	      case 1 : $pY--; break;
	      case 2 : $pY--; $pX++; break;
	      case 3 : $pX++; break;
	      case 4 : $pY++; $pX++; break;
	      case 5 : $pY++; break;
	      case 6 : $pY++; $pX--; break;
	      case 7 : $pX--; break;
	      case 8 : $pY--; $pX--; break;
	      default : break;
	    }
	}
    }

Here is the full class structure, and also a small demo. You will need to have the other classes in the same php file, the classes grid and the words.

class WordToSearchGrid extends Grid {
 
    private $_words;
 
    /* create a blank grid  - testing -- just need to comment out this function to create the real grid*/
    public function createGridRow()
    {
	$retArray = array();
	for ($i = 1; $i <= parent::returnSize(); $i++)
	{
	    $retArray[$i] = " ";
	}
	return array();
    }
 
    /* Gets the words to search for */
    public function getWords($wordsArray)
    {
	if (isset($wordsArray) && is_array($wordsArray))
	{
	  $this->_words = $wordsArray;
	}
    }
 
    // createSearchGrid, will insert the words onto a blank grid (for testing).
    // in random places and directions.
    public function createSearchGrid()
    {
	// get the size of the grid
	$size = parent::returnSize();
 
	// loop thought the words to insert
	for ($i = 0; $i < count($this->_words); $i++)
	{
	  $wordLen = strlen($this->_words[$i]);
	  // if the word is larger than the size of the grid, it cannot be inserted.
 
	  // NOTE : at present there is no intersection (crossing search words) shall do in the next version
 
	  if ($wordLen < $size) 
	  {
	      //need to pick a direction and also a point on the grid.
	      // also need to try and place the word onto the grid x amount of times else break out.
	      $direction = rand(1,8);
	      $pointX = rand(1, $size);
	      $pointY = rand(1, $size);
 
	      // the remainder of the subtracting the points from the size of grid will basically say how much space is required
	      $space = $size - $wordLen;
 
	      // check against the direction and the size of the word against where it is on the poistion in the grid
	      // 1 = north , 2 = north east, 3 = east, 4 = south east, 5 = south, 6 = south west, 7 = west, 8= north west
	      // from the points point of view.
 
	      $spaceY = $size - $pointY;
	      $spaceX = $size - $pointX;
 
	      // if the direction is not east or west, and there is not enought space, move the insertion pointY difference
	      // north and south
	      if (!(($direction == 7) || ($direction == 3)))
	      {
		  if ($spaceY > $space) 
		      $pointY+= ($spaceY - $space);
	      }
 
	      // if the direction is not north or south and there is not enought space, move the insertion pointX difference
	      // east and west.
	      if (!(($direction == 1) || ($direction == 5)))
	      {
		  if ($spaceX > $space)
		      $pointX+= ($spaceX - $space);
	      }
 
	      $this->insertIntoGrid($pointX, $pointY, $this->_words[$i], $direction);
	  }
	  else
	  {
	      // no need to tell the user to search for something that was not inserted
	      echo "Word ". $this->_words[$i] . " was too long for the wordsearch";
	      unset($this->_words[$i]);
	  }
	}
    }
 
    // print out the words to search for that have been inserted into the grid
    public function printTheWordsToSearch()
    {
	foreach ($this->_words as $key => $value)
	{
	    echo "<br/>$key = $value";
	}
    }
 
    // insert into the grid at the positions x, y, the word to insert with the direction
    public function insertIntoGrid($pX, $pY, $word, $direction)
    {
	$wordLen = strlen($word);
 
	// move the starting point of the word to the correct place within the grid
	// the default is north and west, so will need to correct for south and east directions.
	if ($direction >=4 && $direction <=6) $pY-= ($wordLen-1);
	if ($direction >=2 && $direction <=4) $pX-= ($wordLen-1);
 
	// 1 = north , 2 = north east, 3 = east, 4 = south east, 5 = south, 6 = south west, 7 = west, 8= north west
	// process each letter of the word and move the position to insert in the correct direction
	for ($i = 0; $i < $wordLen; $i++)
	{
	    parent::insertCharIntoGrid($pX, $pY, $word[$i]);
	    // move to the next grid position to insert the character
	    switch($direction)
	    {
	      case 1 : $pY--; break;
	      case 2 : $pY--; $pX++; break;
	      case 3 : $pX++; break;
	      case 4 : $pY++; $pX++; break;
	      case 5 : $pY++; break;
	      case 6 : $pY++; $pX--; break;
	      case 7 : $pX--; break;
	      case 8 : $pY--; $pX--; break;
	      default : break;
	    }
	}
    }
}
 
 
$GridWords = new WordToSearchGrid(11);
 
$WordsToSearchFor = new Word("words.xml",3);
 
$GridWords->getWords($WordsToSearchFor->wordsArray());
 
$GridWords->createSearchGrid();
 
$GridWords->printOut();
 
$GridWords->printTheWordsToSearch();

Here is the output from the above, you may get something different since it is based on randomized numbers.

The grid
1 .. 1 = 2 = 3 = 4 = 5 = s 6 = 7 = 8 = 9 = 10 = 11 =
2 .. 1 = 2 = 3 = 4 = 5 = a 6 = 7 = 8 = 9 = 10 = 11 =
3 .. 1 = 2 = 3 = y 4 = 5 = w 6 = 7 = 8 = 9 = o 10 = 11 =
4 .. 1 = 2 = 3 = r 4 = 5 = 6 = 7 = 8 = 9 = 10 = l 11 =
5 .. 1 = 2 = 3 = r 4 = 5 = 6 = 7 = 8 = 9 = 10 = 11 = d
6 .. 1 = 2 = 3 = e 4 = 5 = 6 = 7 = 8 = 9 = 10 = 11 =
7 .. 1 = 2 = 3 = m 4 = 5 = 6 = 7 = 8 = 9 = 10 = 11 =
8 .. 1 = 2 = 3 = 4 = 5 = 6 = 7 = 8 = 9 = 10 = 11 =
9 .. 1 = 2 = 3 = 4 = 5 = 6 = 7 = 8 = 9 = 10 = 11 =
10 .. 1 = 2 = 3 = 4 = 5 = 6 = 7 = 8 = 9 = 10 = 11 =
11 .. 1 = 2 = 3 = 4 = 5 = 6 = 7 = 8 = 9 = 10 = 11 =
 
0 = merry
1 = was
2 = old

BAR 15 – BAR 1 – no parent – nvidia graphics card does not work

I have come up with a fix for the problem that I was having regarding upgrading my 2GB of RAM to 4GB of RAM, when the nvidia was trying to place its memory request into the new system RAM place, as described in more detail here.

This was the error that I was getting, where the BAR 1 for pci 0000:01:00.0 means the nvidia geforce 7600 256MB graphics card.

[    0.397452] pci 0000:00:01.0: BAR 15: address space collision on of bridge [0xb0000000-0xbfffffff]
[    0.397452] pci 0000:00:01.0: BAR 15: cant allocate resource start :0xb0000000-0xbfffffff
[    0.397452] pci 0000:01:00.0: BAR 1: no parent found for of device [0xb0000000-0xbfffffff]

Also the problem with trying to apply the nvnews fix, which meant just blocking out the 0xc0000000-0xd0000000 range was that there was other devices using that and thus they was gaining that memory range before the nvidia could try to access it. What I have done is to hard code (shall try and do a better fix next), so that if anything tries to gain the memory resource 0xc0000000-0xcfffffff it will block the request and they will re-assign the memory in a later process (great linux kernel there 🙂 ). Then if there is a request for the ranges 0xb0000000 – 0xbfffffff (which is where the ACER BIOS tries to say to put the memory for the nvidia graphics card), move that to the 0xc0000000-0xcfffffff. I have placed at the bottom how the memory is organized before and after the update, where the pci devices that was in the 0xc0 -0xcf ranges are now placed further down in the memory allocation table.

So what I have done is to alter the linux-kernel-source-code /arch/x86/pci/i386.c file, by adding into the function pcibios_allocate_bus_resources come code. I have included the full code for the function below and also just the bit that I have updated further down, it is the bit just after the /* Ian Porter */ 🙂

static void __init pcibios_allocate_bus_resources(struct list_head *bus_list)
{                                                                            
        struct pci_bus *bus;                                                 
        struct pci_dev *dev;                                                 
        int idx;                                                             
        struct resource *r;                                                  
 
        /* Depth-First Search on bus tree */
        list_for_each_entry(bus, bus_list, node) {
                if ((dev = bus->self)) {          
                        for (idx = PCI_BRIDGE_RESOURCES;
                            idx < PCI_NUM_RESOURCES; idx++) {
                                r = &dev->resource[idx];     
 
 
/*
Ian Porter added, to test for the nvidia problem
 
[0xb0000000-0xbfffffff] is where the BIOS is telling nvidia to get the memory,
but with 4GB this is where the system memory is.                              
 
a better way of doing this, would be to run the BIOS find memory process twice
and then if no resources can gain memory, e.g. nvidia in this case, flag it for the
second run to then give that the starting area and re-do the rest of them, because 
mainly it is the graphics card that needs the most memory..e.g. 256-512 etc.       
*/                                                                                 
                                if ((r->start >= 0xc0000000) && (r->end <= 0xcfffffff)) {
                                        dev_info(&dev->dev,                              
                                                 " not allocating resource 0xc - 0xcf %pR\n",
                                                 r);                                         
                                        /*                                                   
                                                stop any resources gaining the 0xc0000000 - 0xcfffffff
                                                region, the linux kernel will re-place them.          
                                        */                                                            
                                        r->flags = 0;                                                 
                                }                                                                     
 
                                /* where the nvidia is going and replace in the above region */
                                if ((r->start == 0xb0000000) && (r->end == 0xbfffffff)) {      
                                        r->start = 0xc0000000;                                 
                                        r->end = 0xcfffffff;                                   
                                }                                                              
/* stop insert */
 
                                if (!r->flags)
                                        continue;
                                if (!r->start || 
                                    pci_claim_resource(dev, idx) < 0) {
                                        dev_warn(&dev->dev, "BAR %d: can't allocate resource\n", idx);
                                        /*                                                            
                                         * Something is wrong with the region.                        
                                         * Invalidate the resource to prevent                         
                                         * child resource allocations in this                         
                                         * range.                                                     
                                         */                                                           
                                        r->flags = 0;                                                 
                                }                                                                     
                        }                                                                             
                }                                                                                     
                pcibios_allocate_bus_resources(&bus->children);                                       
        }                                                                                             
}

Basically just place this code

/*
Ian Porter added, to test for the nvidia problem
 
[0xb0000000-0xbfffffff] is where the BIOS is telling nvidia to get the memory,
but with 4GB this is where the system memory is.                              
 
a better way of doing this, would be to run the BIOS find memory process twice
and then if no resources can gain memory, e.g. nvidia in this case, flag it for the
second run to then give that the starting area and re-do the rest of them, because 
mainly it is the graphics card that needs the most memory..e.g. 256-512 etc.       
*/                                                                                 
                                if ((r->start >= 0xc0000000) && (r->end <= 0xcfffffff)) {
                                        dev_info(&dev->dev,                              
                                                 " not allocating resource 0xc - 0xcf %pR\n",
                                                 r);                                         
                                        /*                                                   
                                                stop any resources gaining the 0xc0000000 - 0xcfffffff
                                                region, the linux kernel will re-place them.          
                                        */                                                            
                                        r->flags = 0;                                                 
                                }                                                                     
 
                                /* where the nvidia is going and replace in the above region */
                                if ((r->start == 0xb0000000) && (r->end == 0xbfffffff)) {      
                                        r->start = 0xc0000000;                                 
                                        r->end = 0xcfffffff;                                   
                                }

after the

  r = &dev->resource[idx];

in the for loop

compile up the kernel as on this post, the basics are

cd /usr/src/
apt-get install fakeroot kernel-wedge build-essential makedumpfile
 
apt-get build-dep linux
 
apt-get build-dep linux-image-$(uname -r)
apt-get source linux-image-$(uname -r)
 
debian/rules updateconfigs
 
cd debian.master/scripts/misc 
chmod a+x *
cd -
debian/rules updateconfigs

make the changes as described in the file arch/x86/pci/i386.c as above, and then

fakeroot debian/rules clean
CONCURRENCY_LEVEL=2 AUTOBUILD=1 NOEXTRAS=1 fakeroot debian/rules binary-generic

this will create a linux image and headers debian file, after a long wait in the /usr/src directory there will be couple of deb files (Of course change the files names for the header and image that you may have)

cd ..
dpkg -i  linux-headers-2.6.31-17-generic_2.6.31-17.54_amd64.deb
dpkg -i linux-image-2.6.31-17-generic_2.6.31-17.54_amd64.deb

To stop the aptitude from trying to update the linux-image back to the stock one I just did (again change to the image and headers that you have)

aptitude hold linux-image-2.6.31-17-generic_2.6.31-17.54_amd64.deb 
aptitude hold  linux-headers-2.6.31-17-generic_2.6.31-17.54_amd64.deb

Also had to do

echo linux-headers-2.6.31-17-generic hold | dpkg --set-selections
echo linux-image-2.6.31-17-generic hold | dpkg --set-selections

To stop the apt-get upgrade/update as well.

IOMEM of a none working 4GB graphics card, if you note that there is not enough chunck of memory to house the nvidia 256MB memory grab, but there is space enough if the items between the 0xc0000000 – 0xcfffffff are moved about then there is enough space.

00000000-00000fff : System RAM         
00001000-00005fff : reserved           
00006000-0009b3ff : System RAM         
0009b400-0009ffff : reserved           
000dc000-000dffff : reserved           
000e4000-000fffff : reserved           
00100000-bfe8ffff : System RAM         
  01000000-01530dd8 : Kernel code      
  01530dd9-018236af : Kernel data      
  018d5000-019e4ccb : Kernel bss       
  02000000-09ffffff : Crash kernel     
bfe90000-bfe99fff : ACPI Non-volatile Storage
bfe9a000-bfffffff : reserved                 
c0000000-c1ffffff : PCI Bus 0000:03          
c2000000-c3ffffff : PCI Bus 0000:05          
c4000000-c5ffffff : PCI Bus 0000:02          
  c4000000-c401ffff : 0000:02:00.0           
c6000000-c7ffffff : PCI Bus 0000:07          
c8000000-c9ffffff : PCI Bus 0000:03          
ca000000-cbffffff : PCI Bus 0000:05          
cc000000-cdffffff : PCI Bus 0000:02          
  cc000000-cc003fff : 0000:02:00.0           
    cc000000-cc003fff : sky2                 
ce000000-cfffffff : PCI Bus 0000:07          
  ce000000-ce000fff : 0000:07:00.0           
    ce000000-ce000fff : iwl3945              
d0000000-d1ffffff : PCI Bus 0000:01
  d0000000-d0ffffff : 0000:01:00.0
  d1000000-d1ffffff : 0000:01:00.0
d2000000-d20fffff : PCI Bus 0000:09
  d2000000-d2003fff : 0000:09:06.1
  d2004000-d2004fff : 0000:09:06.2
    d2004000-d2004fff : tifm_7xx1
  d2005000-d20057ff : 0000:09:04.0
    d2005000-d20057ff : saa7133[0]
  d2005800-d2005fff : 0000:09:06.1
    d2005800-d2005fff : ohci1394
  d2006000-d20060ff : 0000:09:06.3
    d2006000-d20060ff : mmc0
  d2007000-d2007fff : 0000:09:06.0
    d2007000-d2007fff : yenta_socket
d2300000-d2303fff : 0000:00:1b.0
  d2300000-d2303fff : ICH HD audio
d2304000-d23043ff : 0000:00:1d.7
  d2304000-d23043ff : ehci_hcd
d2304400-d23047ff : 0000:00:1f.2
  d2304400-d23047ff : ahci
d4000000-d7ffffff : PCI Bus 0000:09
  d4000000-d7ffffff : PCI CardBus 0000:0a
d8000000-dbffffff : PCI CardBus 0000:0a
e0000000-efffffff : PCI MMCONFIG 0 [00-ff]
  e0000000-efffffff : reserved
    e0000000-efffffff : pnp 00:01
fec00000-fec0ffff : reserved
  fec00000-fec00fff : IOAPIC 0
fed00000-fed003ff : HPET 0
  fed00000-fed003ff : reserved
fed14000-fed19fff : reserved
  fed14000-fed17fff : pnp 00:01
  fed18000-fed18fff : pnp 00:01
  fed19000-fed19fff : pnp 00:01
fed1c000-fed8ffff : reserved
  fed1c000-fed1ffff : pnp 00:01
  fed20000-fed3ffff : pnp 00:01
  fed45000-fed8ffff : pnp 00:01
fee00000-fee00fff : Local APIC
  fee00000-fee00fff : reserved
ff000000-ffffffff : reserved

Working IOMEM 4GB graphics nvidia geforce 7600 256MB card, the items that was in the 0xc0000000 – 0xcfffffff ranges are now placed further down in the memory allocations, if you look for sky2 , iwl3945 , which are kinder needed for the wireless capabilities of the laptop.

00000000-00000fff : System RAM               
00001000-00005fff : reserved                 
00006000-0009b3ff : System RAM               
0009b400-0009ffff : reserved                 
000dc000-000dffff : reserved                 
000e4000-000fffff : reserved                 
00100000-bfe8ffff : System RAM               
  01000000-01530f79 : Kernel code            
  01530f7a-018216ef : Kernel data            
  018d3000-019e2ccb : Kernel bss             
  02000000-09ffffff : Crash kernel           
bfe90000-bfe99fff : ACPI Non-volatile Storage
bfe9a000-bfffffff : reserved                 
c0000000-cfffffff : PCI Bus 0000:01          
  c0000000-cfffffff : 0000:01:00.0           
d0000000-d1ffffff : PCI Bus 0000:01          
  d0000000-d0ffffff : 0000:01:00.0           
  d1000000-d1ffffff : 0000:01:00.0           
    d1000000-d1ffffff : nvidia               
d2000000-d20fffff : PCI Bus 0000:09          
  d2000000-d2003fff : 0000:09:06.1           
  d2004000-d2004fff : 0000:09:06.2           
    d2004000-d2004fff : tifm_7xx1            
  d2005000-d20057ff : 0000:09:04.0           
    d2005000-d20057ff : saa7133[0]
  d2005800-d2005fff : 0000:09:06.1
    d2005800-d2005fff : ohci1394
  d2006000-d20060ff : 0000:09:06.3
    d2006000-d20060ff : mmc0
  d2007000-d2007fff : 0000:09:06.0
    d2007000-d2007fff : yenta_socket
d2100000-d21fffff : PCI Bus 0000:02
  d2100000-d2103fff : 0000:02:00.0
    d2100000-d2103fff : sky2
d2200000-d22fffff : PCI Bus 0000:02
  d2200000-d221ffff : 0000:02:00.0
d2300000-d2303fff : 0000:00:1b.0
  d2300000-d2303fff : ICH HD audio
d2304000-d23043ff : 0000:00:1d.7
  d2304000-d23043ff : ehci_hcd
d2304400-d23047ff : 0000:00:1f.2
  d2304400-d23047ff : ahci
d2400000-d24fffff : PCI Bus 0000:07
  d2400000-d2400fff : 0000:07:00.0
    d2400000-d2400fff : iwl3945
d4000000-d7ffffff : PCI Bus 0000:09
  d4000000-d7ffffff : PCI CardBus 0000:0a
d8000000-dbffffff : PCI CardBus 0000:0a
e0000000-efffffff : PCI MMCONFIG 0 [00-ff]
  e0000000-efffffff : reserved
    e0000000-efffffff : pnp 00:01
fec00000-fec0ffff : reserved
  fec00000-fec00fff : IOAPIC 0
fed00000-fed003ff : HPET 0
  fed00000-fed003ff : reserved
fed14000-fed19fff : reserved
  fed14000-fed17fff : pnp 00:01
  fed18000-fed18fff : pnp 00:01
  fed19000-fed19fff : pnp 00:01
fed1c000-fed8ffff : reserved
  fed1c000-fed1ffff : pnp 00:01
  fed20000-fed3ffff : pnp 00:01
  fed45000-fed8ffff : pnp 00:01
fee00000-fee00fff : Local APIC
  fee00000-fee00fff : reserved
ff000000-ffffffff : reserved

hope this post helps other people to get around the nvidia laptop memory grab, hopefully I am going to write a pci kernel update so that it does not have to block out a set part of memory.

Wordsearch – Word Class iterator

An iterator is when a object can be used within a foreach loop and each data item can be looped through, with a couple of other functions declared as well e.g. key, next because these functions allow the foreach loop to work. For example, at the start of the object you will need to have a position indicator (set normally at 0) and a object (array) to go through, I am using the word class from the word search project. From the php.net website there is a more details of a iterator.

The foreach loop basically starts the iterator from the starting point (rewind function) and then using the next function call to increment the internal object to its next point whilst making sure that it has not come to its end point (checking against the valid function).

Here is the code that I have used to demonstrate a iterator interface (interface example).

Please note, extending from the word class from a previous post.

class WordIterator extends Word implements Iterator {
    private $_position = 0;
    private $_wordsIterator;
 
    public function __construct($wordsOrFilename = "words.xml", $maxSearchNum = 5) {
        $this->_position = 0;
	$this->_wordsIterator = array();
 
	parent::__construct($wordsOrFilename, $maxSearchNum);
 
	$this->_wordsIterator = parent::wordsArray();
    }
 
    function rewind() {
        $this->_position = 0;
    }
 
    function current() {
        return $this->_wordsIterator[$this->_position];
    }
 
    function key() {
        return $this->_position;
    }
 
    function next() {
        ++$this->_position;
    }
 
    function valid() {
        return isset($this->_wordsIterator[$this->_position]);
    }
}
 
 
$WordIt = new WordIterator;
 
foreach($WordIt as $key => $value) {
    echo $key . " " . $value;
    echo "\n";
}

Output would be

0 merry
1 old 
2 sole 
3 was 
4 he