Example #1
0
    def insert(self, key, value):
        if key is None or value is None:
            raise Exception

        # resizing if more than load factor
        if (self.item_count / self.array_size) > self.load_factor:
            self._resize_array()

        index = cs5112_hash1(key) % self.array_size

        # if index is not empty, it is either x or real tuple
        while self._get_array().get(index) is not None:
            # if it is x, it is removed. add it here.
            if self._get_array().get(index) == 'x':
                self._get_array().set(index, (key, value))
                self.item_count += 1
                break
            # the key is the same, just update the value
            elif self._get_array().get(index)[0] == key:
                self._get_array().set(index, (key, value))
                break
            else:
                # there is a real, other tuple, move on
                # wrap-around logic
                if self.array_size - 1 == index:
                    index = 0
                # just probe the next cell
                else:
                    index += 1

        if self.array.get(index) is None:
            self._get_array().set(index, (key, value))
            self.item_count += 1
Example #2
0
    def remove(self, key):
        if key is None:
            raise Exception

        index = cs5112_hash1(key) % self.array_size
        end = index

        while self._get_array().get(index) is not None:
            if self._get_array().get(index) != 'x' and self._get_array().get(
                    index)[0] == key:
                value = self._get_array().get(index)[1]
                self._get_array().set(index, 'x')
                self.item_count -= 1
                return value

                # wrap-around logic
            if self.array_size - 1 == index:
                index = 0
            else:
                # just probe the next cell
                index += 1

            if index == end:
                return None

        return None
Example #3
0
    def get(self, key):
        if key is None:
            raise Exception

        index = cs5112_hash1(key) % self.array_size
        end = index

        while self._get_array().get(index) is not None:
            #check that not removed and is the key
            if self._get_array().get(index) != 'x' and self._get_array().get(
                    index)[0] == key:
                return self._get_array().get(index)[1]

            # wrap-around logic
            if self.array_size - 1 == index:
                index = 0
            else:
                # just probe the next cell
                index += 1

            if index == end:
                return None

        if self._get_array().get(index) is None:
            return None
Example #4
0
    def remove(self, key):
        # YOUR CODE HERE
        if (key == None):
            raise Exception("key cannot be none")

        hash_slot = cs5112_hash1(key) % self.array_size
        content_in_slot = self._get_array().get(hash_slot)

        if (content_in_slot == None):
            return content_in_slot
        else:
            cur_node = content_in_slot.head
            prev_node = None

            while (cur_node != None):
                if (cur_node.get_value()[0] == key):
                    if (prev_node != None):
                        prev_node.set_next(cur_node.get_next(
                        ))  ## HOW DO I ALTER THE ORIGINAL LINKED LIST?
                    else:
                        content_in_slot.head = cur_node.get_next()

                    self.item_count = self.item_count - 1
                    self._get_array().set(hash_slot, content_in_slot)
                    return cur_node.get_value()[1]

                prev_node = cur_node
                cur_node = cur_node.get_next()

            return None
Example #5
0
 def add_elem(self, elem):
     p1 = cs5112_hash1(elem) % self.size
     p2 = cs5112_hash2(elem) % self.size
     p3 = cs5112_hash3(elem) % self.size
     self.array.set(p1, True)
     self.array.set(p2, True)
     self.array.set(p3, True)
Example #6
0
  def insert(self, key, value):

    if key==None or value==None:
      raise Exception

    if (self.item_count/self.array_size)>=self.load_factor:
      self._resize_array()
      

    bucket= cs5112_hash1(key) % self.array_size
    new_node=SLLNode((key, value))
    pointer= self.array.get(bucket)

    if not pointer:
      self.array.set(bucket, new_node)
      self.item_count+=1
      return

    else:

      while pointer.get_next():

        if pointer.get_value()[0]== key:
          pointer.set_value=((key, value))
          return
        pointer=pointer.get_next()

      if pointer.get_value()[0]== key:
          pointer.set_value=((key, value))
          return

      else:
        pointer.set_next(new_node)
        self.item_count+=1
        return
Example #7
0
  def remove(self, key):

    if key== None:
      raise Exception

    bucket= cs5112_hash1(key) % self.array_size

    if not self.array.get(bucket):
      return None

    pointer= self.array.get(bucket)

    if pointer.get_value()[0]== key:
      val= pointer.get_value()[1]
      self.array.set(bucket, pointer.get_next())
      self.item_count-=1
      return val

    while pointer.get_next():

      if pointer.get_next().get_value()[0]== key:
        val= pointer.get_next().get_value()[1]
        pointer.set_next((pointer.get_next().get_next()))
        self.item_count-=1
        return val

      pointer=pointer.get_next()

    return None
Example #8
0
    def insert(self, key, value):

        if self.load_factor <= (self.item_count / self.array_size):
            self._resize_array()

        # create a new SLL node
        new_node = SLLNode((key, value))  # pass tuple as value

        hash_id = cs5112_hash1(key) % self.array_size
        curr_node = self.array.get(hash_id)

        # if hashed array elem is empty, set the elem to the new node
        if not curr_node:
            self.array.set(hash_id, new_node)
            self.item_count += 1
            return  # done, return

        override = False

        # otherwise, loop through linked list until the end
        while curr_node.get_next(
        ):  # when the next node is None, you've reached the end

            # if the key in curr node == key passed, override the value
            if curr_node.get_value()[0] == key:
                curr_node.set_value((key, value))
                override = True

            curr_node = curr_node.get_next()

        # if you're not overriding, you're adding the new node to the end
        if not override:
            curr_node.set_next(new_node)
            self.item_count += 1
    def remove(self, key):
        # If key is None, raise an Exception
        if (key == None):
            raise ValueError("Key can't be None")

        # Get hash value and mod it
        hashval = cs5112_hash1(key) % self.array_size

        node = self.array.get(hashval)
        # Can't remove it if it doesn't exist
        if (node == None):
            return None

        # If it is the first element of the linked list, set that pointer to the next
        if (node.get_value()[0] == key):
            self.array.set(hashval, node.get_next())
            self.item_count -= 1
            return node.get_value()[1]

        prevNode = node
        node = node.get_next()

        # While 'node' isn't None, search through
        while (node != None):
            # If it is found further down the list, set the previous node's
            # pointer to the next node and return the value
            if (node.get_value()[0] == key):
                prevNode.set_next(node.get_next())
                self.item_count -= 1
                return node.get_value()[1]
            prevNode = node
            node = node.get_next()

        # Else return None
        return None
Example #10
0
 def insert(self, key, value):
     # first search
     self.item_count += 1
     if (self.item_count * 1.0 / self.array_size) > self.load_factor:
         self._resize_array()
     hash_key = cs5112_hash1(key) % self.array_size
     #condtion: hash_key is not empty
     while self.array.get(hash_key) is not None:
         # condition 1: rewrite, same key and haven't been deleted
         if self.array.get(
                 hash_key)[0] == key and not self.array.get(hash_key)[2]:
             self.item_count -= 1
             self.array.set(hash_key, (key, value, False))
             return
         #condition 2: rewrite, same key but been deleted
         elif self.array.get(hash_key)[0] == key and self.array.get(
                 hash_key)[2]:
             self.array.set(hash_key, (key, value, False))
             return
         #condition 3: empty space, but still need to search for the rest
         elif self.array.get(hash_key)[2]:
             self.array.set(hash_key, (key, value, False))
             hash_key = (hash_key + 1) % self.array_size
             while self.array.get(hash_key) is not None:
                 if self.array.get(hash_key)[0] == key:
                     self.item_count -= 1
                     self.array.set(
                         hash_key, (key, self.array.get(hash_key)[1], True))
                 return
         hash_key = (hash_key + 1) % self.array_size
     self.array.set(hash_key, (key, value, False))
     return
Example #11
0
  def remove(self, key):

    try:
      if key is None:
        raise TypeError

      ind = cs5112_hash1(key) % self.array_size
      head = self.array.get(ind)
      prev = None

      print("HEAD = " + str(head))

      while head != None:
        if head.get_value()[0] == key:
          break

        prev = head
        head = head.get_next()

      if not head:
        return None

      self.item_count -= 1

      if prev != None:
        prev.set_next(head.get_next())
      else:
        self.array.set(ind, head.get_next())

      return head.get_value()[1]

    except TypeError:
      print('Key cannot be None!')
    def insert(self, key, value):

        # check loading factor, if over, resize

        if self.load_factor <= (self.item_count / self.array_size):
            self._resize_array()

        # hash the key
        hash_ind = cs5112_hash1(key) % self.array_size

        # loop until an empty index found
        while self.array.get(hash_ind) != None and self.array.get(
                hash_ind) != 'removed':

            # if you hash and get back the key, then update the value
            if self.array.get(
                    hash_ind)[0] == key:  # check the first entry in the tuple
                self.array.set(hash_ind, (key, value))

            # check hash of next index
            hash_ind = (hash_ind + 1) % self.array_size

        # loop broken, found None elem, so set it
        self.array.set(hash_ind, (key, value))
        self.item_count += 1
Example #13
0
  def check_membership(self, elem):

    h_val1 = cs5112_hash1(elem) % 10
    init_h_val2 = cs5112_hash2(elem)
    h_val2 = init_h_val2 % 10
    init_h_val3 = cs5112_hash3(elem)
    h_val3 = init_h_val3 % 10

    i = 1
    while h_val2 == h_val1 and i < len(str(init_h_val2)):
        # h_val2 = int(cs5112_hash2(elem) % (10 ** (i + 1)) / 10 ** i)
        h_val2 = init_h_val2 % (10 ** (i + 1)) // 10 ** i
        i += 1

    j = 1
    while (h_val3 == h_val1 or h_val3 == h_val2) and j < len(str(init_h_val3)):
        # h_val3 = int(cs5112_hash3(elem) % (10 ** (j + 1)) / 10 **j)
        h_val3 = init_h_val3 % (10 ** (j + 1)) // 10 ** j
        j += 1

    for ind in [h_val1, h_val2, h_val3]:
        if not self.array.get(ind):
            return False

    return True
Example #14
0
    def insert(self, key, value):
        # Raise exception if either key or value is None
        if (key == None or value == None):
            raise ValueError("Neither Key nor Value can be None")

        h = cs5112_hash1(key) % self.array_size

        # If nothing exists at this location, just insert, increment
        if self.array.get(h) == None:
            self.array.set(h, (key, value))
            self.item_count += 1
        else:
            # Iterate through the next elements until you reach a None, update if you see the same key
            while self.array.get(h) != None:
                if self.array.get(h)[0] == key:
                    self.array.set(h, (key, value))
                    return
                h = (h + 1) % self.array_size

            # Insert value at this None
            self.array.set(h, (key, value))
            self.item_count += 1

        # If the load factor now exceeds the limit, resize
        if ((float(self.item_count) / self.array_size) > self.load_factor):
            self._resize_array()
    def insert(self, key, value):
        # if key or value is None, raise an Exception
        if (key == None or value == None):
            raise ValueError("Neither Key nor Value can be None")

        # Get the hash value and take the modulus
        hashval = cs5112_hash1(key) % self.array_size

        # If there is nothing at that entry in the array, create the first node and inc count
        if (self.array.get(hashval) == None):
            newNode = SLLNode((key, value))
            self.array.set(hashval, newNode)
            self.item_count += 1
        else:
            # If something already exists, iterate through and see if any match the key
            node = self.array.get(hashval)
            prevNode = None

            while (node != None):
                # If so, update the value and don't change the count
                if (node.get_value()[0] == key):
                    node.set_value((key, value))
                    return
                prevNode = node
                node = node.get_next()

            # Else at the end add the node and inc the count
            newNode = SLLNode((key, value), None)
            prevNode.set_next(newNode)
            self.item_count += 1

        # If the load factor now exceeds the limit, resize
        if ((float(self.item_count) / self.array_size) > self.load_factor):
            self._resize_array()
Example #16
0
    def remove(self, key):

        hash_id = cs5112_hash1(key) % self.array_size
        front_node = self.array.get(hash_id)
        curr_node = front_node
        prev_node = None
        saved_value = None

        # if empty, return None
        if not curr_node:
            return None

        # otherwise, loop until elem is found, and set the prev next node to curr's next node
        while curr_node:

            node_key, node_value = curr_node.get_value()  # unpack the value

            # check if keys match
            if node_key == key:

                # if prev, then item not in the front
                if prev_node:
                    prev_node.set_next(curr_node.get_next())  #

                else:  # item is in the front, so set hash elem to the elem after curr node
                    self.array.set(hash_id, curr_node.get_next())

                self.item_count -= 1  # decrement count
                return node_value  # return saved value

            # update pointers
            prev_node = curr_node
            curr_node = curr_node.get_next()

        return None  # not found if reaches here
Example #17
0
 def add_elem(self, elem):
     # raise NotImplementedError
     hash1 = cs5112_hash1(elem) % self.size
     hash2 = cs5112_hash2(elem) % self.size
     hash3 = cs5112_hash3(elem) % self.size
     for j in [hash1, hash2, hash3]:
         self.array.set(j, True)
    def remove(self, key):
        if key is None:
            raise Exception("Key cannot be None")

        ind = cs5112_hash1(key) % self.array_size
        cur_list = self.array.get(ind)
        ret_val = None

        # if the list is None, we definitely don't have the value stored
        if cur_list is None:
            return None

        # first item in linked list
        if cur_list.get_value()[0] == key:
            ret_val = cur_list.get_value()[1]
            self.item_count -= 1
            self.array.set(ind, cur_list.get_next())
        else:
            # search through the list to see if the key is in it
            for n in self._iterate_list(cur_list):
                if n == None:  # it isn't in the list
                    return None
                next_node = n.get_next()

                if next_node == None:
                    return None
                k, v = next_node.get_value()
                if k == key:  # found the key, so store the value
                    ret_val = v
                    self.item_count -= 1
                    n.set_next(next_node.get_next())
                    return ret_val

        return ret_val
    def add_elem(self, elem):
        index1 = cs5112_hash1(elem) % self.size
        index2 = cs5112_hash2(elem) % self.size
        index3 = cs5112_hash3(elem) % self.size

        self.array.set(index1, True)
        self.array.set(index2, True)
        self.array.set(index3, True)
Example #20
0
 def get(self, key):
   index = cs5112_hash1(key)
   node = self.array.get(index)
   while node is not None:
     if node.get_value()[0] == key:
       return node.get_value()[1]
     node = node.get_next()
   return None
Example #21
0
    def add_elem(self, elem):
        h1 = cs5112_hash1(elem) % self.size
        h2 = cs5112_hash2(elem) % self.size
        h3 = cs5112_hash3(elem) % self.size

        self.array.set(h1, True)
        self.array.set(h2, True)
        self.array.set(h3, True)
Example #22
0
 def get(self, key):
     hash_key = cs5112_hash1(key) % self.array_size
     while self.array.get(hash_key) is not None:
         if not self.array.get(hash_key)[2] and self.array.get(
                 hash_key)[0] == key:
             return self.array.get(hash_key)[1]
         hash_key += 1
     return None
Example #23
0
 def check_membership(self, elem):
     #TODO: YOUR CODE HERE, delete the line below and implement accordingly
     if (self.array.get(cs5112_hash1(elem) % 10)
             and self.array.get(cs5112_hash2(elem) % 10)
             and self.array.get(cs5112_hash3(elem) % 10)):
         return True
     else:
         return False
Example #24
0
    def check_membership(self, elem):
        p1 = cs5112_hash1(elem) % self.size
        p2 = cs5112_hash2(elem) % self.size
        p3 = cs5112_hash3(elem) % self.size
        if not self.array.get(p1) or not self.array.get(
                p2) or not self.array.get(p3):
            return False

        return True
Example #25
0
 def insert(self, key, value):
     # YOUR CODE HERE
     if key == None:
         raise Exception('Key cannot be None!')
     if value == None:
         raise Exception('Value cannot be None!')
     h = cs5112_hash1(key) % self.array_size
     while self.array.get(h) != None:
         if self.array.get(h)[0] == key:
             self.array.set(h, (key, value))
             return
         h += 1
         if h == self.array_size - 1:
             self._resize_array()
             h = cs5112_hash1(key) % self.array_size
     self.array.set(h, (key, value))
     if self.item_count / self.array_size >= self.load_factor:
         self._resize_array()
Example #26
0
    def add_elem(self, elem):
        index1 = cs5112_hash1(elem) % self.size
        index2 = cs5112_hash2(elem) % self.size
        index3 = cs5112_hash3(elem) % self.size
        print(index1, index2, index3)

        # set indices to True
        self.array.set(index1, True)
        self.array.set(index2, True)
        self.array.set(index3, True)
Example #27
0
 def get(self, key):
     # YOUR CODE HERE
     if key == None:
         raise Exception('Key cannot be None!')
     h = cs5112_hash1(key) % self.array_size
     while self.array.get(h) != None:
         if self.array.get(h)[0] == key:
             return self.array.get(h)[1]
         else:
             h += 1
Example #28
0
    def check_membership(self, elem):
        h1 = cs5112_hash1(elem) % self.size
        h2 = cs5112_hash2(elem) % self.size
        h3 = cs5112_hash3(elem) % self.size

        r1 = self.array.get(h1)
        r2 = self.array.get(h2)
        r3 = self.array.get(h3)

        return r1 and r2 and r3
Example #29
0
    def check_membership(self, elem):

        hash1 = cs5112_hash1(elem) % self.size
        hash2 = cs5112_hash2(elem) % self.size
        hash3 = cs5112_hash3(elem) % self.size

        if self.array.get(hash1) and self.array.get(hash2) and self.array.get(
                hash3):
            return True

        return False
 def check_membership(self, elem):
   # If elem is None, raise an Exception
   if (elem == None):
     raise ValueError("Key can't be None")
   # Calculate the hashes for a given element.
   h1 = cs5112_hash1(elem) % self.size
   h2 = cs5112_hash2(elem) % self.size
   h3 = cs5112_hash3(elem) % self.size
   
   return (self.array.get(h1) and self.array.get(h2) and self.array.get(h3))