Jump to content

HashSet in php


AzeS

Recommended Posts

I am trying to imitate the hashset function of Visual Basic. For this of course, I must declare a variable globally and here it begins to fail already; I have tried it without the interpreter global but also here it does not work at all.You might have better suggestions as I could immit, or even improvements for the code?
I look forward to answers and suggestions for improvement.

<?php 
/**
* 
*/
global $Hashset = array();
class stimpackZ
{


	public function hash_add($NAME,$DATA) {
		
			
		if ($NAME = 0) {
			$name_of_hash = count($Hashset) + 1;
			global $Hashset[$name_of_hash] = $DATA;
			echo $name_of_hash;
			return 1;
		} else {
			$name_of_hash = $NAME;
			$Hashset[$name_of_hash] = $DATA;
			return 1;
		}

		

	}
	public function hash_create($NAME) {

	}

	
	public function hash_get() {
		return $Hashset;
	}
	public function hash_cls() {
		global $Hashset = null;
		global $Hashset = array();
		return 1;
	}
	public function hash_del($ARG,$DATA) {
		switch ($ARG) {
			case 0:
				$key = array_search($DATA, global $Hashset);
				unset(global $Hashset[$key]);
				return 1;
				break;
			case 1:
			unset(global $Hashset[$DATA]);
				return 1;
				break;
			default:
				return 0;
				break;
		}
		
	}
	public function hash_test() {
		var_dump(global $Hashset);
	}
}

?>

 

Link to comment
Share on other sites

The code makes no sense. It's generally a bad idea to imitate other languages. Instead, you should specifiy the goal and then implement it with the features of your current language.

 

So what is the goal? A mathematical set which can contain a value exactly once? Then the right implementation is an associative array where the keys are the set elements and the values are arbitrary:

<?php

$set = [];

// store 42 (the value is irrelevant)
$set[42] = true;

// store 42 again (this has no effect, just like you would expect from a set)
$set[42] = true;

// check if the set contains certain elements
$contains_42 = isset($set[42]);
$contains_43 = isset($set[43]);

var_dump($contains_42, $contains_43);

// delete element
unset($set[42]);

$contains_42 = isset($set[42]);

var_dump($contains_42);

Putting this into a class shouldn't be too hard now. I'm sure there are also existing libraries.

<?php

class Set
{
    private $elements = [];

    public function add($element)
    {
        $this->elements[$element] = true;
    }

    public function remove($element)
    {
        unset($this->elements[$element]);
    }

    public function contains($element)
    {
        return isset($this->elements[$element]);
    }
}
$set = new Set();

$set->add(42);
$set->add(42);

var_dump($set->contains(42));
var_dump($set->contains(43));

$set->remove(42);

var_dump($set->contains(42));
Edited by Jacques1
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.