tags:

views:

422

answers:

5

What is the most complicated, complex block of code you've ever written for a legitimate purpose in a real project (up to 20 lines)?

A: 

I once wrote some microcode to create an assembly language. That was pretty complex as I didn't understand modularity in those days nearly as well I do now.

Preet Sangha
A: 

Implementing a priority queue in C++ where the priorities could be changed. I no longer have access to that, though, and I couldn't post it if I did.

David Thornley
A: 

Probably doesn't count as code, but what the hell.

Back when I was a COBOL programmer I rendered in the header comments a wonderfully complex ASCII diagram of the application data flow.

Martin
A: 

This is the most complicated code I needed at one point:

function MakeProcInstance(M: TMethod): Pointer; 
begin 
  // allocate memory 
  GetMem(Result, 15); 
  asm 
    // MOV ECX,  
    MOV BYTE PTR [EAX], $B9 
    MOV ECX, M.Data 
    MOV DWORD PTR [EAX+$1], ECX 
    // POP EDX 
    MOV BYTE PTR [EAX+$5], $5A 
    // PUSH ECX 
    MOV BYTE PTR [EAX+$6], $51 
    // PUSH EDX 
    MOV BYTE PTR [EAX+$7], $52 
    // MOV ECX,  
    MOV BYTE PTR [EAX+$8], $B9 
    MOV ECX, M.Code 
    MOV DWORD PTR [EAX+$9], ECX 
    // JMP ECX 
    MOV BYTE PTR [EAX+$D], $FF 
    MOV BYTE PTR [EAX+$E], $E1 
  end; 
end;

What this Delphi code does is - in short - the following: It creates a executable block of memory that, when called, will push the pointer to the object that is referenced in the TMethod argument on the stack. After that it will call the method.

The reason I needed that was that I once had to call an API method and give a callback method pointer to it. That API did not know about the 'self' pointer used in delphi. I could only provide 'static' methods. This way I could not identify the source object when the callback came in.

With that code I could dynamically create a certain method for each object that used this API - even in multiple threads - and always the correct object could be called.

Sebastian P.R. Gingter
A: 

Actually, that's a tough one as I don't tend to allow my code to get complicated. That's a failure of the coder.

However, sometimes I have something rather complicated to accomplish. For example, there is this code which performs a picewise linear interpolation of the value for a given coordinate from a two-dimensional array of coordinate values. Note how long the comment is. With me, that's a bad sign. Also note that this call is recursive.

   --------------------------------------------------------------------------------
   -- Compute the interpolated value for the given input value base on the
   -- values read into the table.
   --
   -- The actual algorithm used is a piecewise linear interpolation. There is an
   -- explanation of this algorithm using hypercubes in the SDF. Scaling factors
   -- were added to support the general case of non-equal sides.
   --
   -- This routine is built for speed. But I do see two things that could be done
   -- to further speed it up if nessecary.
   -- o  Change the Table parameter to an "in out". The compiler may be able to
   --    do a much better job optimizing the code when it is not dealing with
   --    an aliased object. This would require the function to be changed to a
   --    a procedure though. Yuk!
   -- o  Change the "zone search algorithm". Currently it uses a kind of linear
   --    (although the top level is binary). A full binary search could speed
   --    things up when the correct zone is far from the last zone. However, if the
   --    zones don''t change much (which is probably the case) then a binary
   --    search could actually slow it down.
   --------------------------------------------------------------------------------
   function Interpolate( X,Y   : in     Float;
                         Table : access Instance )
      return  Float is

      X_Independent : Float := X;
      Y_Independent : Float := Y;

   begin

      -- Make sure the table has been initialized
      if (Table.X_Count = 0 or Table.Y_Count = 0) then
         raise Invalid_Table;
      end if;

      -- Find the lower points of the enclosing zones for the given coordinates
      Zone_Index (Independents => Table.Independent_X(1..Table.X_Count),
                  Value        => X_Independent,
                  Old_Zone     => Table.Start_X_Zone,
                  New_Zone     => Table.Start_X_Zone);

      Zone_Index (Independents => Table.Independent_Y(1..Table.Y_Count),
                  Value        => Y_Independent,
                  Old_Zone     => Table.Start_Y_Zone,
                  New_Zone     => Table.Start_Y_Zone);

      return Interpolate
        (Input_X => X_Independent,
         Input_Y => Y_Independent,
         X_Start => Table.Start_X_Zone,
         Y_Start => Table.Start_Y_Zone,
         Table   => Table.all
         );

   end Interpolate;
T.E.D.