Commit d42cf15a by O'Reilly Media, Inc.

Initial commit

parents
## Example files for the title:
# 9780596510046
[![9780596510046](https://resources.oreilly.com/uploads/system/group/avatar/3/tarsier_hi_elbows.png)](https://www.safaribooksonline.com/)
The following applies to example files from material published by O’Reilly Media, Inc. Content from other publishers may include different rules of usage. Please refer to any additional usage rights explained in the actual example files or refer to the publisher’s website.
O'Reilly books are here to help you get your job done. In general, you may use the code in O'Reilly books in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from our books does not require permission. Answering a question by citing our books and quoting example code does not require permission. On the other hand, selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Incorporating a significant amount of example code from our books into your product's documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN.
If you think your use of code examples falls outside fair use or the permission given here, feel free to contact us at <permissions@oreilly.com>.
Please note that the examples are not production code and have not been carefully testing. They are provided "as-is" and come with no warranty of any kind.
File added
class BigHash
def initialize(file)
@hash = {}
lines = 0
File.open(file).each_line do |line|
s = line.split
article = s[2].intern
if @hash[s[0]]
@hash[s[0]] << [ s[1], article ]
else
@hash[s[0]] = [ s[1], article ]
end
lines += 1
STDERR.puts "Line: #{lines}" if (lines % 100000) == 0
end
end
def find(key)
@hash[key]
end
end
class BinarySearch
attr_reader :keys, :values
def initialize(file, size = 0)
last = ''
@keys = Array.new(size)
@values = Array.new(size)
lines = 0
hosts = -1
File.open(file).each_line do |line|
s = line.split
article = s[2].intern
if s[0] != last
hosts += 1
@keys[hosts] = s[0]
@values[hosts] = []
last = s[0]
end
@values[hosts] << [ s[1], article ]
lines += 1
STDERR.puts "Line: #{lines}" if (lines % 100000) == 0
end
end
def find(target)
low = -1
high = @keys.size
while (high - low) > 1 do
probe = (high + low) / 2
if (@keys[probe] > target)
high = probe
else
low = probe
end
end
if low == -1 || @keys[low] != target
return nil
else
return @values[low]
end
end
end
counts = {}
counts.default = 0
ARGF.each_line do |line|
if line =~ %r{GET /ongoing/When/\d\d\dx/(\d\d\d\d/\d\d/\d\d/[^ .]+) }
counts[$1] += 1
end
end
unique = {}
lines = 0
last = ''
ARGF.each_line do |line|
lines += 1
puts "Line: #{lines}, unique #{unique.size}" if (lines % 100000) == 0
line.gsub!(/ .*$/, '')
next if line == last
unique[line] = 1
last = line
end
puts "Unique fetchers: #{unique.size}"
ARGF.each_line do |line|
if line =~ %r{GET /ongoing/When/\d\d\dx/(\d\d\d\d/\d\d/\d\d/[^ .]+) }
puts $1
end
end
ARGF.each_line do |line|
if line =~ %r{GET /ongoing/When/\d\d\dx/\d\d\d\d/\d\d/\d\d/[^ .]+ }
puts line
end
end
require 'big-hash'
require 'binary-search'
# args are data file, test queries, and class-name to use
data_file = ARGV[0]
query_file = ARGV[1]
class_name = ARGV[2]
array_size = ARGV[3].to_i
t0 = Process::times
puts "#{t0.inspect}"
if class_name == 'Hash'
worker = BigHash.new(data_file)
elsif class_name == 'Binary'
worker = BinarySearch.new(data_file, array_size)
end
queries = 0
t1 = Process::times
puts "#{t1.inspect}"
File.open(query_file).each_line do |line|
line.chomp!
y = worker.find(line)
puts "ERROR, #{line} not found" unless y
n = worker.find(line.reverse)
puts "Oddly, #{line.reverse} was found" if n
queries += 1
end
t2 = Process::times
puts "#{t2.inspect}"
puts "Queries #{queries}, load time #{t1.utime - t0.utime}, " +
"run time #{t2.utime - t1.utime}"
require 'Time'
unique = {}
lines = 0
last = ''
# 81.27.128.143 - - [10/Dec/2006:07:00:03 -0800] "GET /ongoing/ongoing.atom HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0"
ARGF.each_line do |line|
if line =~ %r{^([^S]+) - - \[(..)/(...)/(....):(..):(..):(..) .*GET /ongoing/When/\d\d\dx/(\d\d\d\d/\d\d/\d\d/[^ .]+) }
who = $1
t = Time.local($4, $3, $2, $5, $6, $7)
puts "#{who} #{t.to_i} #{$8}"
lines += 1
STDERR.puts "Line: #{lines} @ #{t}" if (lines % 100000) == 0
end
end
"GET /ongoing/When/\d\d\dx/\d\d\d\d/\d\d/\d\d/[^ .]+ "
lines = 0
interval = ARGV[0].to_i || 1000
STDIN.each_line do |line|
lines += 1
puts line if (lines % interval) == 0
end
use strict;
my %counts = {};
while (<STDIN>) {
if (/GET \/ongoing\/When\/\d\d\dx\/(\d\d\d\d\/\d\d\/\d\d\/[^ .]+) /)
{
$counts{$1}++;
}
}
my @k = keys(%counts);
my @k = sort { $counts{$b} <=> $counts{$a} } @k;
my $i;
foreach $i (0 .. 9)
{
print "$counts{@k[$i]}: $k[$i]\n";
}
# line looks like:
# c80-216-32-218.cm-upc.chello.se - - [08/Oct/2006:06:37:48 -0700] "GET /ongoing/When/200x/2006/10/08/Grief-Lessons HTTP/1.1" 200 5945 "http://www.tbray.org/ongoing/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)
"
counts = {}
counts.default = 0
ARGF.each_line do |line|
if line =~ %r{GET /ongoing/When/\d\d\dx/(\d\d\d\d/\d\d/\d\d/[^ .]+) }
counts[$1] += 1
end
end
keys_by_count = counts.keys.sort { |a, b| counts[b] <=> counts[a] }
keys_by_count[0 .. 9].each do |key|
puts "#{counts[key]}: #{key}"
end
def count_one stream, counts
stream.each do |line|
# for example: From: Tim Bray <Tim.Bray@Sun.COM>
if line =~ /^From: .*<([^>]*)>\n$/
who = $1
if counts[who]
counts[who] += 1
else
counts[who] = 1
end
end
end
end
counts = {}
if ARGV.length == 0
count_one STDIN, counts
else
ARGV.each do |filename|
File.open(filename) { |file| count_one(file, counts) }
end
end
descending = counts.keys.sort { |a, b| counts[a] <=> counts[b] }
descending.reverse_each { |from| puts "From: #{from}: #{counts[from]}" }
counts = {}
counts.default = 0
ARGF.grep(/^From: .*<([^>]*)>\n$/) { counts[$1] += 1 }
descending = counts.keys.sort { |a, b| counts[b] <=> counts[a] }
descending.each { |from| puts "From: #{from}: #{counts[from]}" }
counts = {}
counts.default = 0
linecount = 0
ARGF.each do |line|
linecount += 1
STDERR.puts "line: #{linecount}" if linecount % 100000 == 0
counts[$1] += 1 if line =~ /^From: .*<([^>]*)>\n$/
end
descending = counts.keys.sort { |a, b| counts[b] <=> counts[a] }
descending.each { |from| puts "From: #{from}: #{counts[from]}" }
counts = {}
counts.default = 0
$<.grep(/^From: .*<([^>]*)>\n$/) { counts[$1] += 1 }
descending = counts.keys.sort { |a, b| counts[b] <=> counts[a] }
descending.each { |from| puts "From: #{from}: #{counts[from]}" }
counts = {}
counts.default = 0
t1 = Process::times
ARGF.each_line do |line|
if line =~ %r{GET /ongoing/When/\d\d\dx/(\d\d\d\d/\d\d/\d\d/[^ .]+) }
counts[$1] += 1
end
end
t2 = Process::times
keys_by_count = counts.keys.sort { |a, b| counts[b] <=> counts[a] }
keys_by_count[0 .. 9].each do |key|
puts "#{counts[key]}: #{key}"
end
t3 = Process::times
step1 = t2.utime - t1.utime
step2 = t3.utime - t2.utime
printf "Step 1: %g, Step 2: %g\n", step1, step2
#include "mapreduce/mapreduce.h"
// User's map function
class WordCounter : public Mapper {
public:
virtual void Map(const MapInput& input) {
const string& text = input.value();
const int n = text.size();
for (int i = 0; i < n; ) {
// Skip past leading whitespace
while ((i < n) && isspace(text[i]))
i++;
// Find word end
int start = i;
while ((i < n) && !isspace(text[i]))
i++;
if (start < i)
EmitIntermediate(text.substr(start,i-start),"1");
}
}
};
REGISTER_MAPPER(WordCounter);
// User's reduce function
class Adder : public Reducer {
virtual void Reduce(ReduceInput* input) {
// Iterate over all entries with the
// same key and add the values
int64 value = 0;
while (!input->done()) {
value += StringToInt(input->value());
input->NextValue();
}
// Emit sum for input->key()
Emit(IntToString(value));
}
};
REGISTER_REDUCER(Adder);
int main(int argc, char** argv) {
ParseCommandLineFlags(argc, argv);
MapReduceSpecification spec;
// Store list of input files into "spec"
for (int i = 1; i < argc; i++) {
MapReduceInput* input = spec.add_input();
input->set_format("text");
input->set_filepattern(argv[i]);
input->set_mapper_class("WordCounter");
}
// Specify the output files:
// /gfs/test/freq-00000-of-00100
// /gfs/test/freq-00001-of-00100
// ...
MapReduceOutput* out = spec.output();
out->set_filebase("/gfs/test/freq");
out->set_num_tasks(100);
out->set_format("text");
out->set_reducer_class("Adder");
// Optional: do partial sums within map
// tasks to save network bandwidth
out->set_combiner_class("Adder");
// Tuning parameters: use at most 2000
// machines and 100 MB of memory per task
spec.set_machines(2000);
spec.set_map_megabytes(100);
spec.set_reduce_megabytes(100);
// Now run it
MapReduceResult result;
if (!MapReduce(spec, &result)) abort();
// Done: 'result' structure contains info
// about counters, time taken, number of
// machines used, etc.
return 0;
}
//----------------------------------------
// ImageClip.cs 2001 by Charles Petzold
//----------------------------------------
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
class ImageClip: ImagePrint
{
MenuItem miCut, miCopy, miPaste, miDel;
[STAThread]
public new static void Main()
{
Application.Run(new ImageClip());
}
public ImageClip()
{
Text = strProgName = "Image Clip";
// Edit menu
MenuItem mi = new MenuItem("&Edit");
mi.Popup += new EventHandler(MenuEditOnPopup);
Menu.MenuItems.Add(mi);
int index = Menu.MenuItems.Count - 1;
// Edit Cut menu item
miCut = new MenuItem("Cu&t");
miCut.Click += new EventHandler(MenuEditCutOnClick);
miCut.Shortcut = Shortcut.CtrlX;
Menu.MenuItems[index].MenuItems.Add(miCut);
// Edit Copy menu item
miCopy = new MenuItem("&Copy");
miCopy.Click += new EventHandler(MenuEditCopyOnClick);
miCopy.Shortcut = Shortcut.CtrlC;
Menu.MenuItems[index].MenuItems.Add(miCopy);
// Edit Paste menu item
miPaste = new MenuItem("&Paste");
miPaste.Click += new EventHandler(MenuEditPasteOnClick);
miPaste.Shortcut = Shortcut.CtrlV;
Menu.MenuItems[index].MenuItems.Add(miPaste);
// Edit Delete menu item
miDel = new MenuItem("De&lete");
miDel.Click += new EventHandler(MenuEditDelOnClick);
miDel.Shortcut = Shortcut.Del;
Menu.MenuItems[index].MenuItems.Add(miDel);
}
void MenuEditOnPopup(object obj, EventArgs ea)
{
miCut.Enabled =
miCopy.Enabled =
miDel.Enabled = image != null;
IDataObject data = Clipboard.GetDataObject();
miPaste.Enabled = data.GetDataPresent(typeof(Bitmap)) ||
data.GetDataPresent(typeof(Metafile));
}
void MenuEditCutOnClick(object obj, EventArgs ea)
{
MenuEditCopyOnClick(obj, ea);
MenuEditDelOnClick(obj, ea);
}
void MenuEditCopyOnClick(object obj, EventArgs ea)
{
Clipboard.SetDataObject(image, true);
}
void MenuEditPasteOnClick(object obj, EventArgs ea)
{
IDataObject data = Clipboard.GetDataObject();
if (data.GetDataPresent(typeof(Metafile)))
image = (Image) data.GetData(typeof(Metafile));
else if (data.GetDataPresent(typeof(Bitmap)))
image = (Image) data.GetData(typeof(Bitmap));
strFileName = "Clipboard";
Text = strProgName + " - " + strFileName;
Invalidate();
}
void MenuEditDelOnClick(object obj, EventArgs ea)
{
image = null;
strFileName = null;
Text = strProgName;
Invalidate();
}
}
//---------------------------------------------------------------------
// ImageFilterTest.cs (c) 2007 by Charles Petzold for "Beautiful Code"
//---------------------------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;
class ImageFilterTest : ImageClip
{
MenuItem miScale;
MenuItem miGenerate;
[STAThread]
public new static void Main()
{
Application.Run(new ImageFilterTest());
}
public ImageFilterTest()
{
Text = strProgName = "Image Filter Test";
// View menu
MenuItem miView = new MenuItem("&View");
Menu.MenuItems.Add(miView);
// Scale menu item
miScale = new MenuItem("&Scale Isotropically to Window");
miScale.Click += new EventHandler(MenuViewIsotropicallyOnClick);
miScale.Checked = true;
miView.MenuItems.Add(miScale);
// Filter menu
MenuItem miFilter = new MenuItem("F&ilter");
Menu.MenuItems.Add(miFilter);
// Generate-Code menu item
miGenerate = new MenuItem("&Use method that generate Intermediate Language");
miGenerate.Click += new EventHandler(MenuFilterGenerateOnClick);
miFilter.MenuItems.Add(miGenerate);
// Filter items
MenuItem mi = new MenuItem("3 \x00D7 3 Blur Filter");
mi.Click += new EventHandler(FilterOnClick);
mi.Tag = new ImageFilter(3, new double[] {1, 1, 1, 1, 1, 1, 1, 1, 1});
miFilter.MenuItems.Add(mi);
mi = new MenuItem("5 \x00D7 5 Blur Filter");
mi.Click += new EventHandler(FilterOnClick);
mi.Tag = new ImageFilter(5, new double[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1 });
miFilter.MenuItems.Add(mi);
mi = new MenuItem("3 \x00D7 3 Sharpness Filter (0 -1 0, -1 5 -1, 0 -1 0)");
mi.Click += new EventHandler(FilterOnClick);
mi.Tag = new ImageFilter(3, new double[] { 0, -1, 0, -1, 5, -1, 0, -1, 0 });
miFilter.MenuItems.Add(mi);
mi = new MenuItem("3 \x00D7 3 Sharpness Filter (-1 -1 -1, -1 9 -1, -1 -1 -1)");
mi.Click += new EventHandler(FilterOnClick);
mi.Tag = new ImageFilter(3, new double[] {-1, -1, -1, -1, 9, -1, -1, -1, -1});
miFilter.MenuItems.Add(mi);
mi = new MenuItem("3 \x00D7 3 Edge-Detection Filter (-1 -1 -1, -1 8 -1, -1 -1 -1)");
mi.Click += new EventHandler(FilterOnClick);
mi.Tag = new ImageFilter(3, new double[] { -1, -1, -1, -1, 8, -1, -1, -1, -1 });
miFilter.MenuItems.Add(mi);
}
// OnPaint handler to display bitmap full size or window size
protected override void OnPaint(PaintEventArgs args)
{
if (miScale.Checked)
base.OnPaint(args);
else if (image != null)
{
Graphics grfx = args.Graphics;
grfx.DrawImage(image, 0, 0);
}
}
// Two handlers for checkable menu items
void MenuViewIsotropicallyOnClick(object sender, EventArgs args)
{
miScale.Checked = !miScale.Checked;
Invalidate();
}
void MenuFilterGenerateOnClick(object sender, EventArgs args)
{
miGenerate.Checked = !miGenerate.Checked;
}
// Menu event handler to apply the filter
void FilterOnClick(object sender, EventArgs args)
{
if (!(image is Bitmap))
{
MessageBox.Show("Must have color bitmap to apply filter!", strProgName);
return;
}
MenuItem mi = sender as MenuItem;
Bitmap bmp = image as Bitmap;
ImageFilter filter = mi.Tag as ImageFilter;
Cursor cursorSave = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
DateTime dt = DateTime.Now;
bool isSuccess = filter.ApplyFilter(bmp, miGenerate.Checked);
TimeSpan ts = DateTime.Now - dt;
Cursor.Current = cursorSave;
if (isSuccess)
{
Invalidate();
MessageBox.Show("Elapsed time = " + ts.TotalMilliseconds + " milliseconds", strProgName);
}
else
{
MessageBox.Show("Bitmap must have 24 or 32 bits per pixel!", strProgName);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{ECAAB904-92E3-4787-AA55-C2D22E68E04E}</ProjectGuid>
<OutputType>WinExe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ImageFilterTest</RootNamespace>
<AssemblyName>ImageFilterTest</AssemblyName>
<StartupObject>ImageFilterTest</StartupObject>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
</ItemGroup>
<ItemGroup>
<Compile Include="ImageFilter.cs" />
<Compile Include="ImageClip.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="ImageFilterTest.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="ImageIO.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="ImageOpen.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="ImagePrint.cs">
<SubType>Form</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<Content Include="ReadMe.txt" />
</ItemGroup>
</Project>
\ No newline at end of file

Microsoft Visual Studio Solution File, Format Version 9.00
# Visual Studio 2005
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImageFilterTest", "ImageFilterTest.csproj", "{ECAAB904-92E3-4787-AA55-C2D22E68E04E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU