Home AMX User Forum AMX Technical Discussion

best way to search an integer array

so I have written code to search char array for something, find it, remove it, and adjust the char array
(using find_string remove _string, etc). What is the best way to do this on an integer array.

Example I am storing numbers in an integer array, I want to search the array for a number, remove it and shift everything up to replace it. Any ideas?

Comments

  • ericmedleyericmedley Posts: 4,177
    samos wrote: »
    so I have written code to search char array for something, find it, remove it, and adjust the char array
    (using find_string remove _string, etc). What is the best way to do this on an integer array.

    Example I am storing numbers in an integer array, I want to search the array for a number, remove it and shift everything up to replace it. Any ideas?

    Well, I'd do a For Loop, search for the desired number. If found, then set a flag in the loop to start shifting the numbers down.

    You didn't say if you wanted to just remove the first instance of the offending number or all of them. That would also have to be determined.
  • PhreaKPhreaK Posts: 966
    Create a new array, iterate across the elements of the array you're function has been passed and only add the elements of interest to the new array, then return this. The main drawback is that if you don't know the size on the incoming array you will need to err towards your highest expected input size for the internal copy. It's not the most memory efficient way of doing things, but if you're got a bit of stack space to spare it shouldn't cause any issues.
  • As there is little known on the actual way you want to use this, my suggestion might help or not...

    However, if I had to keep an integer array just as a "storage" facility, having no particular connection between an array index and a particular role of the stored value, then I would probably choose a solution similar to a csv file. Store the values as delimited strings in a char array separated with a "," (or whatever).

    When stored/reused, ITOA/ATOI will do the job, and you can take advantage of the string manipulation and parsing.
  • Spire_JeffSpire_Jeff Posts: 1,917
    Depending on the purpose, I would probably create a structure that consists of the the previous index, the next index, and the value. Then parse through the array until I find the desired value. When you find the value, adjust the pointers on the previous and next records and 0 out the current records pointers. You will also have to track the start of the array and figure out how you terminate the array.

    If you are doing a lot of removing from the array, I think that the overhead involved in this method will be a lot less than just recreating the array or shifting values continuously. Of course this all depends on how large the array is. If you are dealing with a maximum of 10 values, you might as well just shift the values.

    Jeff
  • AuserAuser Posts: 506
    // Name   : ==== IntegerCollection_Find ====
    // Purpose: 
    // Params : 
    // Returns: 
    // Notes  : 
    //
    define_function integer IntegerCollection_Find(integer _nCollection[], integer _nRequestedItem, integer _nInstance)
    {
    	stack_var integer		_nCurrentItem
    	stack_var integer		_nCurrentInstance
    	
    	_nCurrentInstance = 0
    	for(_nCurrentItem = 1; _nCurrentItem <= length_array(_nCollection); _nCurrentItem++)
    	{
    		if(_nCollection[_nCurrentItem] = _nRequestedItem)
    		{
    			_nCurrentInstance++
    			if(_nCurrentInstance = _nInstance)
    				return _nCurrentItem
    		}
    	}
    	return 0
    }
    
    
    // Name   : ==== IntegerCollection_RemoveByIndex ====
    // Purpose: Removes an item from the collection at the specified index and repacks the collection.
    // Params : _oCollection - the collection to remove from
    //          _nIndex - index of the item to remove
    // Returns: TRUE if found and removed, FALSE otherwise
    // Notes  : 
    //
    define_function char IntegerCollection_RemoveByIndex(integer _oCollection[], integer _nIndex)
    {
      stack_var		integer		_nLoop
      _oCollection[_nIndex] = 0
    
      // Shuffle all collection items up by one position
      if(_nIndex <= length_array(_oCollection))
      {
        for(_nLoop = _nIndex; _nLoop < length_array(_oCollection); _nLoop++)
        {
          _oCollection[_nLoop] = _oCollection[_nLoop+1];
        }
        set_length_array(_oCollection, length_array(_oCollection)-1);
        return TRUE
      }
      else
      {
        return FALSE
      }
    }
    
    // Name   : ==== IntegerCollectionRemoveByKey ====
    // Purpose: Removes a specific integer from the collection
    // Params : Collection, plus an integer to match
    // Returns: Returns TRUE if an item was removed, FALSE otherwise
    // Notes  : Item not removed if not found.
    //
    define_function char IntegerCollection_RemoveByKey(integer _oCollection[], integer _oKey, integer _nInstance)
    {
      stack_var integer _nIndex
      _nIndex = IntegerCollection_Find(_oCollection, _oKey, _nInstance)
    
      if(_nIndex)
      {
        IntegerCollection_RemoveByIndex(_oCollection, _nIndex)
        return TRUE;
      }
      return FALSE;
    }
    

    Note that your integer array has to have its length set. If it is populated at run time, you'll need to use set_length_array to resize it as you add items.
Sign In or Register to comment.