A bug fix and optimizations in CI_Table
diff --git a/system/libraries/Table.php b/system/libraries/Table.php
index 1d43208..58016e9 100644
--- a/system/libraries/Table.php
+++ b/system/libraries/Table.php
@@ -294,21 +294,20 @@
 	{
 		// The table data can optionally be passed to this function
 		// either as a database result object or an array
-		if ($table_data !== NULL)
+		if ( ! empty($table_data))
 		{
-			if (is_object($table_data))
+			if ($table_data instanceof CI_DB_result)
 			{
-				$this->_set_from_object($table_data);
+				$this->_set_from_db_result($table_data);
 			}
 			elseif (is_array($table_data))
 			{
-				$set_heading = (count($this->heading) !== 0 OR $this->auto_heading !== FALSE);
-				$this->_set_from_array($table_data, $set_heading);
+				$this->_set_from_array($table_data);
 			}
 		}
 
 		// Is there anything to display? No? Smite them!
-		if (count($this->heading) === 0 && count($this->rows) === 0)
+		if (empty($this->heading) && empty($this->rows))
 		{
 			return 'Undefined table data';
 		}
@@ -316,8 +315,11 @@
 		// Compile and validate the template date
 		$this->_compile_template();
 
-		// set a custom cell manipulation function to a locally scoped variable so its callable
-		$function = $this->function;
+		// Validate a possibly existing custom cell manipulation function
+		if ($this->function !== FALSE && ! is_callable($this->function))
+		{
+			$this->function = FALSE;
+		}
 
 		// Build the table!
 
@@ -326,11 +328,11 @@
 		// Add any caption here
 		if ($this->caption)
 		{
-			$out .= $this->newline.'<caption>'.$this->caption.'</caption>'.$this->newline;
+			$out .= '<caption>'.$this->caption.'</caption>'.$this->newline;
 		}
 
 		// Is there a table heading to display?
-		if (count($this->heading) > 0)
+		if ( ! empty($this->heading))
 		{
 			$out .= $this->template['thead_open'].$this->newline.$this->template['heading_row_start'].$this->newline;
 
@@ -353,7 +355,7 @@
 		}
 
 		// Build the table rows
-		if (count($this->rows) > 0)
+		if ( ! empty($this->rows))
 		{
 			$out .= $this->template['tbody_open'].$this->newline;
 
@@ -389,9 +391,9 @@
 					{
 						$out .= $this->empty_cells;
 					}
-					elseif ($function !== FALSE && is_callable($function))
+					elseif ($this->function !== FALSE)
 					{
-						$out .= call_user_func($function, $cell);
+						$out .= call_user_func($this->function, $cell);
 					}
 					else
 					{
@@ -435,34 +437,20 @@
 	/**
 	 * Set table data from a database result object
 	 *
-	 * @param	object
+	 * @param	CI_DB_result	$db_result	Database result object
 	 * @return	void
 	 */
-	protected function _set_from_object($query)
+	protected function _set_from_db_result($object)
 	{
-		if ( ! is_object($query))
-		{
-			return;
-		}
-
 		// First generate the headings from the table column names
-		if (count($this->heading) === 0)
+		if ($this->auto_heading === TRUE && empty($this->heading))
 		{
-			if ( ! is_callable(array($query, 'list_fields')))
-			{
-				return;
-			}
-
-			$this->heading = $this->_prep_args($query->list_fields());
+			$this->heading = $this->_prep_args($object->list_fields());
 		}
 
-		// Next blast through the result array and build out the rows
-		if ($query->num_rows() > 0)
+		foreach ($object->result_array() as $row)
 		{
-			foreach ($query->result_array() as $row)
-			{
-				$this->rows[] = $this->_prep_args($row);
-			}
+			$this->rows[] = $this->_prep_args($row);
 		}
 	}
 
@@ -471,29 +459,19 @@
 	/**
 	 * Set table data from an array
 	 *
-	 * @param	array
-	 * @param	bool
+	 * @param	array	$data
 	 * @return	void
 	 */
-	protected function _set_from_array($data, $set_heading = TRUE)
+	protected function _set_from_array($data)
 	{
-		if ( ! is_array($data) OR count($data) === 0)
+		if ($this->auto_heading === TRUE && empty($this->heading))
 		{
-			return FALSE;
+			$this->heading = $this->_prep_args(array_shift($data));
 		}
 
-		$i = 0;
-		foreach ($data as $row)
+		foreach ($data as &$row)
 		{
-			// If a heading hasn't already been set we'll use the first row of the array as the heading
-			if ($i++ === 0 && count($data) > 1 && count($this->heading) === 0 && $set_heading === TRUE)
-			{
-				$this->heading = $this->_prep_args($row);
-			}
-			else
-			{
-				$this->rows[] = $this->_prep_args($row);
-			}
+			$this->rows[] = $this->_prep_args($row);
 		}
 	}