Primus2\Falcraft: New Data Type (set)


This code is obsolete and has been moved to another library.  It is now part of asherwunk/phabstractic.  You can also read the current blog post about this data type.

Primus2\Falcraft now has a new abstract data type: the Set.

Set – An Abstract Data Type

Although PHP has arrays they aren’t specifically designed to act as proper sets out of the box.  Sets as defined by Wikipedia are:

In computer science, a set is an abstract data type that can store certain values, without any particular order, and no repeated values. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests a value for membership in a set.

Like many things non-object oriented, we could implement a set using an array by putting in a unique value check every time we add an element to an array.  But, code duplication is unnecessary and unwieldy.  Instead, let’s take the interface and functionality of a set and put it in an object!  This way we can also tell between a Set type and an array type.  In addition, we can organize some additional functionality particular to a set.

The PHP Set that I have developed acts on arrays as passed.  If PHP is passing by reference, then it is the original array that is included in the set.  However, if PHP is not passing by reference, the elements of the array itself are still accessed and ‘transferred’ to the Set as references.  Theoretically then, you can add an element to a set, affect that element elsewhere and it will affect the set.  You can be sure to add a reference explicitly by using the ::addReference() method.

The following public methods are defined on the Set class:

The static methods equal, fold, filter, walk, and map are inspired and work very much like array functions of the same name. The static methods union, intersection, and difference are inspired and work very much like standard mathematical set operations. The subset function tests to see if one Set is a subset of another set (they must be of type Set) and returns true or false. The build static function allows you to build a Set, much like a factory.

These set operations are used quite a bit in the rest of the Falcraft code. In fact, I believe vertexes and publishers/observers use some of these set operations.

For more information on the array functions see: fold, filter (reduce), map, walk.

One thing to note is the class definition:

This class uses the Configuration feature and the Identity feature.  It uses the Identity feature for generating keys to its Set values.  Usually, you don’t access a Set like an array, that would defeat the purpose.  But there are times when you’d like to retrieve a reference you put into the set.  The Identity feature allows for the generation of unique keys on a class level to assign to set elements.  When you add an element it returns a key that you can use later to access the reference via ::retrieveReference().

Note that this only works for elements added by ::add() or ::addReference(). If you merge, change, add, remove, or perform any set operation this key may disappear or be overwritten.

I hope you can find the Set class to be useful in storing pools of values, or classifications of objects, and more.

View on GitHub
photo credit: Pentium-4/3.0GHz via photopin (license)

Liked it? Take a second to support kadar on Patreon!


I'm just a wunk, trying to enjoy life. I am a cofounder of http// and I like computers, code, creativity, and friends.

You may also like...

Leave a Reply

%d bloggers like this: