Patterns and datastructures

Status
Not open for further replies.

Aatos

Ars Tribunus Militum
2,639
Hi,

I'm supposed to do a thing on which values are inserted on a scale from 0 to for example 500. And I need to run flow of new values against the inserted values to detect if the original values are either higher or lower than than the new value.

I'm skating on a very thin ice here computing theory wise, I think some sort of tree algorhitm should be used, but no clue beyond that. The language used is Java.

Any hints, pointers to good books, etc?
 

Aatos

Ars Tribunus Militum
2,639
This is engine to monitor prices so that I can set "alerts" if price rises above certain point, or falls below a certain point.

Okay I have say 4 values, each tagged with certain id and type. 2 of those are type "I want to know when this value is exceeded", and 2 are type "I want to know when this value is fallen below(? bad english, anyway the negation of the 1st type).

I then have a source which produces values, which each are again tagged. And I want to compare the produced values to those in the above. When a match is made that a produced value with same id that one of the original values has either (according to original values above/below type) exceeded it or fallen below, a notification is made and the the matching original value is removed.
 

EgalitarianBovine

Ars Tribunus Angusticlavius
8,086
There are a bunch of options you could choose. A simple list that you search through to find matching IDs is an option, but that's rather inefficient. You could use a binary tree if you have access to a decent balanced binary tree algorithm.

I think the most appropriate thing to use here would be a hashtable, using the "id"s as the hash key. They'll probably give you the best lookup time.

-EB
 

Aatos

Ars Tribunus Militum
2,639
I was thinking of using two hashmaps, one for over the limit and one under the limit, so that they key is the id, and the value is an ordered list of alerts.

1st it would check whether the id has any alerts at all (get(id)!=null), and if it has, it would iterate through the list from 0 to new value when looking from "under" hashmap, and from list max value to new value when looking from "exceed" hashmap.

I was just wondering if there would be anything more efficient, like a binary tree you mentioned.
 

EgalitarianBovine

Ars Tribunus Angusticlavius
8,086
Ok, so you're saying there can be multiple "exceed" and "below" alerts per id? Do you signal only one alert, or multiple alerts, if they mach?

i.e.:
if you have id "foo" currently at "10" mapped to "alert when fall below 9" and "alert when fall below 5". And if the next value of "foo" is "4", would you signal both alerts?, or just the "alert when fall below 5" one?

-EB
 
Status
Not open for further replies.