2

Is it feasible to implement a sort of hash map in RPGLE? How would you begin thinking it?
Should I look at the Java source code and "copy" that style?

HashMap should ultimately be compatibile with every data type.

LppEdd
  • 20,274
  • 11
  • 84
  • 139
  • Why do you need one? Maybe you want some lookup, see http://www.rpgiv.info/mambo/index.php?option=com_content&task=view&id=65 for example –  Jun 09 '17 at 15:51
  • 2
    @RC. - Why? Why does a Java programmer want one? The link you provided is just an explanation of RPG's existing built-ins for plain old arrays. The %LOOKUPxx functions just return the index of a given element within an array. This is a far, far cry from a hash map. RPG's array lookup doesn't even use hashing. It uses binary search when ordering has been declared, and O(n) sequential search when ordering has not been declared. – John Y Jun 09 '17 at 16:09
  • I'm upvoting this question even though strictly speaking, it's perhaps too broad according to Stack Overflow guidelines. (A lot of IBM midrange questions are borderline off-topic for SO.) What are the boundaries of feasibility? RPG is Turing complete, so it's *possible*. It's got pointers and memory allocation/deallocation, so you could use it as an implementation language roughly analogous to C. But would the effort be worth it? Personally, I would recommend just writing Java code and running *that* on the i. If needed, RPG can call Java. This is quite commonly done. – John Y Jun 09 '17 at 16:22
  • 2
    A Java programmer wants a hash map for storing key/value pairs. You can emulate that in RPG by using an ordered data-structure array and `%lookup`. By using `%lookup' to search the key to get the index, then you can read the value in the remainder of the data structure. – jmarkmurphy Jun 09 '17 at 16:32
  • @JohnY my comment was not to be seen as some insult to RPG (FYI: I started as a Cobol/400 dev and did some RPG too) But let's face things there's no object in RPG only datastructures and lookup is the way to go to emulate an hashmap (I agree with jmarkmurphy on that). (and note that I said "maybe"). –  Jun 09 '17 at 17:15
  • @RC. - I didn't interpret your comment as an insult to RPG. I simply felt it missed significant portions of what makes hash maps desirable. To me, arrays are not even a close approximation of hash maps. It's like saying "C is object-oriented, because it has pointers and structs". – John Y Jun 09 '17 at 18:03
  • Hi guys. Thanks for your opinions. I'm asking about HashMap cause I recently did an ArrayList implementation using custom memory allocation (I will post the code on GitHub). It has been a pain in the ***, just for an "automatic" array. Now, %lookup is just a sequential scan, so no way near an hash map. – LppEdd Jun 09 '17 at 19:15
  • @LppEdd %lookup is a binary search for a sorted array, so it can perform quite well. If you are trying to implement a HashMap, you could do so, and may even be able to make a poor man's implementation using sorted arrays and %lookup. – jmarkmurphy Jun 09 '17 at 19:49
  • 1
    @jmarkmurphy - I guess it depends on how close you want to get to Java's HashMap. An RPG array, even with data structures, isn't what I would consider very close. You don't have the O(1) lookup. You don't have enforcement of unique keys. The built-in arrays don't have dynamic sizing (though I know there are third-party libraries for that). And I'm not sure you can handle *arbitrary* types with a data structure array, other than to use pointers (adding implementation complexity or API complexity or both). To me, these are pretty important reasons for choosing HashMap in the first place. – John Y Jun 09 '17 at 20:16
  • @LppEdd - Veering off-topic for this question, but: when you did your ArrayList, were you already aware of the one by [Mihael Schmidt](http://www.rpgnextgen.com/index.php?content=arraylist)? (I've used a known-to-be-old link because there you can see further links to lots of other cool stuff. The ArrayList project actually sort of has two homes now, Github (under the OSSILE project) and Bitbucket (seems more like Mihael's actual development repo). Both are reachable from that page. There's also one from [Aaron Bartell](http://mowyourlawn.com/RPGDynamicArrays.html) based on user spaces. – John Y Jun 09 '17 at 22:07
  • 1
    To finish off my previous comment - I don't mean to make you feel like you wasted your time. You may have had special requirements that the existing projects didn't meet. More importantly, I'm sure the experience of implementing your own was quite valuable. And you absolutely should still post yours to GitHub. – John Y Jun 09 '17 at 22:13
  • Hi guys. So after a bit of studying my mind is ready for the challenge. My question to you is: would you go with chaining or open addressing in case of collision? – LppEdd Jun 10 '17 at 12:43
  • @JohnY Look at https://github.com/lppedd/RPG for my version of HashMap – LppEdd Jun 16 '17 at 14:43

3 Answers3

2

I'd start here:Implementing a HashMap

Should be able to use C code as a basis for an RPGLE version.

Or you could just build the procedures in C and call it from RPGLE.

Charles
  • 21,637
  • 1
  • 20
  • 44
1

Depending on your needs (if you don't need a specific order of your elements) you could also use a tree based map which already exists, http://rpgnextgen.com/index.php?content=libtree . It uses the red-black-tree implementation from the libtree project on github (which is wonderfully compatible C code. congrats to the developer).

The project on RPG Next Gen provides wrappers for character and integer keys. You can store any value in it as you pass a pointer and a length for it.

And yes, there is a need for data structures like lists and maps and trees. I use them often for passing data between procedures where I don't know how many elements may be returned. And in most programming languages lists and maps and trees are part of the language or at least part of the runtime library. Sadly not so in RPG.

Mihael
  • 364
  • 2
  • 4
1

In the end I did my own implementation.
You can find it here: GitHub - HASHMAP.RPGLE

It is based on the JDK implementation, but the hash code is calculated from a SHA1 hash, and a module operation is used instead of bit shifting.

LppEdd
  • 20,274
  • 11
  • 84
  • 139