logo
  • About us
  • What we do
  • Our product
  • Recent work
  • Contact

Feedback

In Focus

  • Flash XML slideshow
  • Database training

Negative % mod alternative

In writing this circular buffer class, I was faced with an interesting problem. I wanted to pass a negative offset (e.g. -2) to the get() method, but bound that within the range of allocated array cells (e.g. 0-3).

One option would be to rely on Java's mod function: e.g.

  • k = Lookup((next_free_slot + offset)%total_slots)

Alternatively, this negative mod could be replaced by a positive mod, by adding the total number of cells to the offset:

  • k = Lookup((next_free_slot + (total_slots+offset))%total_slots)

The attached code implements a Circular Buffer in Java, using this positive mod method. (CircleBuff.java has been renamed CircleBuff_java.txt for browser compatibility.)

  • Tech report
 

 

 

Copyright 2007 Lightenna Limited

  • About us
  • What we do
  • Our product
  • Recent work
  • Contact