Skip to main content
Fixed bug in code
Source Link
XiaoChuan Yu
  • 1.8k
  • 12
  • 15

Do you need to index things with anything other than an integer? If all you need to do is something like:

a[1] = ... 

Then there's no reason to use hash table. In this case arrays will be more efficient than hash table in all cases assuming you don't delete stuff in the middle and shift.

Assuming what you want is store last X frames, make an array of size X.

Assume initially a[0] is most recent frame, a[X-1] is least recent frame.

Make a counter c that points the least recent frame. c = X -1 initially

For each new frame to add,

a[c] = curFrame //replace least recent frame with most recent c-- //update counter if(c < 0) //need to wrap c around if it goes negative c = X-1 

To iterate from most recent to least recent:

for(int i = 1; i <= X ; i ++) //starts at c + 1, the most recent frame a[(c + i)%X] ... // % (modulo) wraps it around 

If you need to do something like:

h["lol"] = .... 

Then yea, you might need to use a hash table.

Do you need to index things with anything other than an integer? If all you need to do is something like:

a[1] = ... 

Then there's no reason to use hash table. In this case arrays will be more efficient than hash table in all cases assuming you don't delete stuff in the middle and shift.

Assuming what you want is store last X frames, make an array of size X.

Assume initially a[0] is most recent frame, a[X-1] is least recent frame.

Make a counter c that points the least recent frame. c = X -1 initially

For each new frame to add,

a[c] = curFrame //replace least recent frame with most recent c-- //update counter if(c < 0) //need to wrap c around if it goes negative c = X 

To iterate from most recent to least recent:

for(int i = 1; i <= X ; i ++) //starts at c + 1, the most recent frame a[(c + i)%X] ... // % (modulo) wraps it around 

If you need to do something like:

h["lol"] = .... 

Then yea, you might need to use a hash table.

Do you need to index things with anything other than an integer? If all you need to do is something like:

a[1] = ... 

Then there's no reason to use hash table. In this case arrays will be more efficient than hash table in all cases assuming you don't delete stuff in the middle and shift.

Assuming what you want is store last X frames, make an array of size X.

Assume initially a[0] is most recent frame, a[X-1] is least recent frame.

Make a counter c that points the least recent frame. c = X -1 initially

For each new frame to add,

a[c] = curFrame //replace least recent frame with most recent c-- //update counter if(c < 0) //need to wrap c around if it goes negative c = X-1 

To iterate from most recent to least recent:

for(int i = 1; i <= X ; i ++) //starts at c + 1, the most recent frame a[(c + i)%X] ... // % (modulo) wraps it around 

If you need to do something like:

h["lol"] = .... 

Then yea, you might need to use a hash table.

added 433 characters in body
Source Link
XiaoChuan Yu
  • 1.8k
  • 12
  • 15

Do you need to index things with anything other than an integer? If all you need to do is something like:

a[1] = ... 

Then there's no reason to use hash table. In this case arrays will be more efficient than hash table in all cases assuming you don't delete stuff in the middle and shift.

Assuming what you want is store last X frames, make an array of size X.

Assume initially a[0] is most recent frame, a[X-1] is least recent frame.

Make a counter c that points the least recent frame. c = X -1 initially

For each new frame to add,

a[c] = curFrame //replace least recent frame with most recent c-- //update counter if(c < 0) //need to wrap c around if it goes negative c = X 

To iterate from most recent to least recent:

for(int i = 1; i <= X ; i ++) //starts at c + 1, the most recent frame a[(c + i)%X] ... // % (modulo) wraps it around 

If you need to do something like:

h["lol"] = .... 

Then yea, you might need to use a hash table.

Do you need to index things with anything other than an integer? If all you need to do is something like:

a[1] = ... 

Then there's no reason to use hash table. In this case arrays will be more efficient than hash table in all cases assuming you don't delete stuff in the middle and shift.

If you need to do something like:

h["lol"] = .... 

Then yea, you might need to use a hash table.

Do you need to index things with anything other than an integer? If all you need to do is something like:

a[1] = ... 

Then there's no reason to use hash table. In this case arrays will be more efficient than hash table in all cases assuming you don't delete stuff in the middle and shift.

Assuming what you want is store last X frames, make an array of size X.

Assume initially a[0] is most recent frame, a[X-1] is least recent frame.

Make a counter c that points the least recent frame. c = X -1 initially

For each new frame to add,

a[c] = curFrame //replace least recent frame with most recent c-- //update counter if(c < 0) //need to wrap c around if it goes negative c = X 

To iterate from most recent to least recent:

for(int i = 1; i <= X ; i ++) //starts at c + 1, the most recent frame a[(c + i)%X] ... // % (modulo) wraps it around 

If you need to do something like:

h["lol"] = .... 

Then yea, you might need to use a hash table.

Post Undeleted by XiaoChuan Yu
Post Deleted by XiaoChuan Yu
Source Link
XiaoChuan Yu
  • 1.8k
  • 12
  • 15

Do you need to index things with anything other than an integer? If all you need to do is something like:

a[1] = ... 

Then there's no reason to use hash table. In this case arrays will be more efficient than hash table in all cases assuming you don't delete stuff in the middle and shift.

If you need to do something like:

h["lol"] = .... 

Then yea, you might need to use a hash table.