Logo

Programming-Idioms

  • C
  • Java
  • PHP
  • JS
  • C++

Idiom #20 Return two values

Implement a function search which looks for item x in a 2D matrix m.
Return indices i, j of the matching cell.
Think of the most idiomatic way in the language to return the two values at the same time.

#include <utility>
template<typename T, size_t len_x, size_t len_y>
std::pair<size_t, size_t> search (const T (&m)[len_x][len_y], const T &x) {
    for(size_t pos_x = 0; pos_x < len_x; ++pos_x) {
        for(size_t pos_y = 0; pos_y < len_y; ++pos_y) {
            if(m[pos_x][pos_y] == x) {
                return std::pair<size_t, size_t>(pos_x, pos_y);
            }
        }
    }

    // return an invalid value if not found
    return std::pair<size_t, size_t>(len_x, len_y);
}

This tries to be similar to the C variant. In a real world program the 2D matrix should offer an iterator.

The elements can be accessed via returnValue.first and returnValue.second.
bool _search(const Matrix& _m, const float _x, size_t* const out_i, size_t* const out_j) {
  for (size_t j = 0; j < _m.rows; j++) {
    for (size_t i = 0; i < _m.cols; i++) {
      if (_m.at(i, j) == _x) {
         *out_i = i;
         *out_j = j;
         return true;
      }
    }
  }
  return false;
}
#include <string.h>
#include <stdlib.h>
void search(void ***m,void *x,size_t memb_size,int len_x,int len_y,int *i,int *j)
{
	typedef void *m_type[len_x][len_y];
	m_type *m_ref=(m_type*)m;

	for(*i=0;*i<len_x;*i+=1)
	{
		for(*j=0;*j<len_y;*j+=1)
		{
			if(!memcmp((*m_ref)[*i][*j],x,memb_size))
			{
				return;
			}
		}
	}
	*i=*j=-1;
}

m is a matrix containing type (void *) pointing to the data (can be anything)

x is the pointer to the data to look for

memb_size is the size of one element in bytes (to be able to compare anything)

len_x and len_y are the dimensions

i and j are passed by reference and contain the values, or -1 if x was not found, after the function returned.

The typedef is to define the dimensions of the matrix m, this allows for subscript notation
static class Position{
	int i;
	int j;
}

Position search(int[][] m, int x){
	for(int i=0;i<m.length;i++)
		for(int j=0;j<m[i].length;j++)
			if(m[i][j] == x){
				Position pos= new Position();
				pos.i = i;
				pos.j = j;
				return pos;
			}
	return null;
}

A Java method returns 0 or 1 value only. So we have to create a custom class Position to hold the two values to return.
record Z(int a, int b) {}
<T> Z search(T x, T m[][]) {
    int i, j, M = m.length, N;
    for (i = 0; i < M; ++i) {
        N = m[i].length;
        for (j = 0; j < N; ++j)
            if (m[i][j] == x)
                return new Z(i, j);
    }
    return null;
}
function search($x, array $m): ?array
{
    for ($j = 0; $j < count($m); $j++) {
        if (($i = array_search($x, $m[$j])) !== false) {
            return [$i, $j];
        }
    }

    return null;
}
function search(m, x) {
    for (var i = 0; i < m.length; i++) {
        for (var j = 0; j < m[i].length; j++) {
            if (m[i][j] == x) {
                return [i, j];
            }
        }
    }
    return false;
}

Return an array if found, or false if not found.
let search = (x, m) => {
    let i, j, M = m.length, N
    for (i = 0; i < M; ++i) {
        N = m[i].length
        for (j = 0; j < N; ++j)
            if (m[i][j] == x)
                return [i, j]
    }
    return
}
type Matrix is array (Positive range <>, Positive range <>) of Integer;

function Search (M : Matrix; X : Integer; I, J : out Integer) return Boolean is
begin
   for K in M'Range (1) loop
      for L in M'Range (2) loop
         if M (K, L) = X then
            I := K;
            J := L;
            return True;
         end if;    
      end loop;
   end loop;

   return False;
end Search;

New implementation...
< >
programming-idioms.org