# $WordDic: worddic/DicRecords.rb,v 1.5 2002/12/05 05:56:04 tshiozak Exp $ # Copyright (c) 2002 Takuya SHIOZAKI, # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. =begin Dictionary - dictionary data structure. TBD =end require 'md5' module WordDic class Dictionary # record structure class Record attr_reader :name attr_reader :id attr_accessor :contents # def initialize(n="", contents="") self.name = n @contents = contents end # def name= (newname) @name = newname @id = MD5.new(newname).hexdigest end end class DefaultSorter def each_sorted(byname, byid, byord, &blk) byord.each &blk end end # attr_accessor :sorter # def initialize @recordsByName = Hash.new @recordsByID = Hash.new @recordsByInsertOrder = Array.new @sorter = DefaultSorter.new end # get record by name def byname(name) @recordsByName[name] end # get record by ID def byid(id) @recordsByID[id] end # get names def names keys @recordsByName end # get IDs def ids keys @recordsByID end # insert a new def insert(name, contents=nil) rec = nil if name.is_a? Record then # record unless contents.nil? then raise TypeError end rec = name elsif name.is_a? String then # string if contents.nil? then contents = "" end rec = Record.new(name, contents) else raise TypeError end @recordsByName[rec.name] = rec @recordsByID[rec.id] = rec @recordsByInsertOrder.push rec rec end # each def each(&blk) @sorter.each_sorted(@recordsByName, @recordsByID, @recordsByInsertOrder, &blk) end end end